ls的详细用法

在Linux运维的日常工作中ls是最常用的命令之一,ls有许多的选项,并且在bash脚本中也会经常看到它的身影,详细学习ls的用法是每一个运维都应该做的事。

ls和windows的dir命令类似,可以列出指定目录里的内容

默认情况下,ls命令仅列出当前目录的文件和目录就好像使用了.这个参数,并不会递归地列出子目录的内容,且不会列出以.开头的隐藏内容,默认以字母顺序排列,且仅仅列出条目名。


ls的三种退出状态:

0:命令执行成功

1:轻微的错误,通常是指定的对象已经被删除或重命名而导致找不到

2:严重错误,如:内存不足,无效的选项等

注意:如果使用一个非POSIX本地环境,如,设置‘LC_ALL’为en_US,ls的输出排序可能和我们所习惯的不同,这种情况请去设置‘LC_ALL’的值为'C'。


ls是Linux里的一个外部命令,我们使用的ls其实是一个别名

[root@localhost ~]# type ls
ls is aliased to `ls --color=auto'
[root@localhost ~]# unalias ls
[root@localhost ~]# type ls
ls is hashed (/usr/bin/ls)

ls的语法:

ls [OPTION]... [FILE]...

ls后面可以接多个选项和参数,默认参数是当前目录,如果没有-cftuvSUX选项并且--sort也没有指定排序的话,默认按字母排序

[root@localhost testdir]# ls
[root@localhost testdir]# touch a aa b AA C c D e z ZZ l LL
[root@localhost testdir]# ls
a  aa  AA  b  c  C  D  e  l  LL  z  ZZ

注意:Linux里字母的顺序是aAbBcC...,且单个字母比多个字母的顺序靠前,当前面的字母一样时,按照接下来的字母顺序排列,数字排在字母前面,和字母的排列规则相似。

[root@localhost testdir]# touch 1 22 2 21 22 9 91
[root@localhost testdir]# ls
1  2  21  22  9  91  a  aa  AA  b  c  C  D  e  l  LL  z  ZZ

选项:

-a  --all:列出所有文件,包括以.开头的隐藏文件


-A --almost-all:与-a类似但不会列出. 和..,-a选项会覆盖此选项

[root@localhost ~]# ls
1lksfk           Documents  f3.txt                Pictures   Videos
anaconda-ks.cfg  Downloads  initial-setup-ks.cfg  Public
Desktop          f2.txt     Music                 Templates
[root@localhost ~]# ls -a
.                .bash_profile  Desktop    .ICEauthority         Public
..               .bashrc        Documents  initial-setup-ks.cfg  .tcshrc
1lksfk           .cache         Downloads  .lesshst              Templates
anaconda-ks.cfg  .config        .esd_auth  .local                Videos
.bash_history    .cshrc         f2.txt     Music                 .viminfo
.bash_logout     .dbus          f3.txt     Pictures              .Xauthority
[root@localhost ~]# ls -A
1lksfk           .bashrc  Desktop    f3.txt                Music      Videos
anaconda-ks.cfg  .cache   Documents  .ICEauthority         Pictures   .viminfo
.bash_history    .config  Downloads  initial-setup-ks.cfg  Public     .Xauthority
.bash_logout     .cshrc   .esd_auth  .lesshst              .tcshrc
.bash_profile    .dbus    f2.txt     .local                Templates


--author:和-l一起使用,列出文件的创始人

[root@localhost ~]# ls -l --author
total 16
-rw-r--r--. 1 root root root  0 Jul 27 15:38 1lksfk
-rw-------. 1 root root root 1490 Jul 27 02:03 anaconda-ks.cfg
drwxr-xr-x. 2 root root root  6 Jul 28 08:06 Desktop
drwxr-xr-x. 2 root root root  6 Jul 26 17:34 Documents


-b --escape:打印C风格的非图形转义字符,不是很常用


--block-size=SIZE:和-l结合使用,按指定的单位显示文件大小,KB=1000bit,K=1024bit.........

通常使用-h --human-readable 按文件大小自动转化为相应的单位

[root@localhost ~]# ls -l
total 16-rw-r--r--. 1 root root    0 Jul 27 15:38 1lksfk
-rw-------. 1 root root 1490 Jul 27 02:03 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Jul 28 08:06 Desktop

...........

[root@localhost ~]# ls -l --block-size=K
total 16K-rw-r--r--. 1 root root 0K Jul 27 15:38 1lksfk
-rw-------. 1 root root 2K Jul 27 02:03 anaconda-ks.cfg
drwxr-xr-x. 2 root root 1K Jul 28 08:06 Desktop

.........

[root@localhost ~]# ls -l -h
total 16K
-rw-r--r--. 1 root root    0 Jul 27 15:38 1lksfk
-rw-------. 1 root root 1.5K Jul 27 02:03 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Jul 28 08:06 Desktop


-B --ignore-backups :不列出指定的以~结尾的文件,等效于--ignore='*~' --ignore='.*~'

[root@localhost testdir]# ls
nieda  nieda~
[root@localhost testdir]# ls -B
nieda


-d --directory:只列出目录名字,而不会列出目录的内容,并且目录名后不会列出软连接的源路径,除非加上-H,-L或者指定了--dereference-command-line-symlink-to-dir选项。


-H --dereference-command-line:使用命令列中的符号链接指示的真正目的地


--dereference-command-line-symlink-to-dir:不要引用符号链接,除非命令行参数是一个目录的软连接,将显示目录信息而不是软连接信息。

--si :和-h类似,但是其单位是1000而不是1024

--hide=PATTERN :根据指定的shell模式隐藏条目

[root@localhost ~]# ls
1lksfk           Desktop    Downloads  f3.txt                Music     Public     Videos
anaconda-ks.cfg  Documents  f2.txt     initial-setup-ks.cfg  Pictures  Templates
[root@localhost ~]# ls --hide=*.txt
1lksfk  anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos

--indicator-style=WORLD :给条目名添加一个指标方式,WORLD有:none(默认),slash(-p)给目录加一个/的标识,classify(-F)用*/=>@|中的其中一个给条目增加标识,file-type(--file-type)和-F一样除了*以外给条目加标识

其中:/ 表示目录;@表示软连接;*表示可执行文件;=表示sockets文件













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章