linux ls命令详解


ls为list的缩写,列出文件或者目录信息。ls命令的常用格式如下所示:

              [root@initroot ~]# ls [-aAdfFhilnrRSt] 文件或目录名...
              [root@initroot ~]# ls [--color={never,auto,always}] 文件或目录名...
              [root@initroot ~]# ls [--full-time] 文件或目录名...
              可以看到,ls命令有非常多的选项和参数:
              -a: 列出包括隐藏文件(.开头的文件)在内的全部文件,为常用选项;
              -A: 列出包括隐藏文件(.开头的文件)在内的全部文件,但不包括.和..这两个目录;
              -d: 仅列出目录本身,而不是列出目录内的文件,该选项为常用选项;
              -f: ls默认会以文件名排序,-f选项可直接列出文件而不以文件名排序;
              -F: 在文件名后面附加类型信息,例如:*代表可执行文件; /代表目录; =代表socket文件; |代表FIFO文件;
              -h: ls默认以字节为单位显示文件大小,-h选项可以将文件大小以KB、MB、GB等用户较易读的单位方式显示;
              -i: 显示inode节点号,inode的意义下一章将会介绍;
              -l: 显示文件的详细属性信息,为常用选项;
              -n: -l选项以名称的形式显示文件的所有者和所属用户组,-n可以显示文件所有者和所属用户组的ID号,即UID和GID
              -r: 将排序按默认排序反向输出;
              -R: 递归选项,连同子目录内容一起列出来,该目录下的所有文件都会显示出来;
              -S: 以文件容量大小排序,而不是按文件名排序;
              -t: 依时间排序输出,而不是按文件名排序。
              --color=never: 不显示颜色信息,颜色可以用来标记文件类型特性;
              --color=always: 显示颜色信息;
              --color=auto: 让系统按照默认设置自行判断是否显示颜色信息;
              --full-time: 显示完整时间模式,包含年、月、日、时、分;
              --time={atime,ctime}: 输出access时间(atime)或改变权限属性时间(ctime)而非内容变更时间(modification time)。
              ls -l命令默认显示的时间是mtime,可以通过--time选项指定要显示的文件时间。--time=atime显示atime,--time=ctime显示ctime。
            
关于文件时间的概念可以参考:文件时间

ls选项看似很多,其实经常用的也就几个选项:-a、-l、-d、-h。 ls默认只显示文件名,文件名带有颜色信息,并且按照文件名排序。 加上-l或者-n选项就可以列出文件的详细信息了,当然我们经常用的还是-l选项了。加上-a就可以列出包括隐藏文件在内的所有文件了。 实际应用中,我们经常将-a和-l选项连在一起使用,例如:ls -al是最常用的命令形式:

              [root@initroot ~]# ls -al
              total 92
              dr-xr-x---. 10 root root  4096 Dec 17 19:52 .
              dr-xr-xr-x. 18 root root  4096 Aug  6 17:44 ..
              -rw-------   1 root root 14589 Jan  7 12:32 .bash_history
              -rw-r--r--.  1 root root    18 Dec 29  2013 .bash_logout
              -rw-r--r--   1 root root   213 Aug  6 19:16 .bash_profile
              -rw-r--r--.  1 root root   176 Dec 29  2013 .bashrc
              drwx------   3 root root  4096 Oct 15  2017 .cache
              drwx------   3 root root  4096 Aug  6 19:24 .config
              -rw-r--r--.  1 root root   100 Dec 29  2013 .cshrc
              drwxr-xr-x   3 root root  4096 Dec 17 19:52 kblogdepository
              -rw-------   1 root root  2430 Oct 30 22:32 nohup.out
              drwxr-xr-x   5 root root  4096 Nov 19 23:27 .npm
              drwxr-xr-x  29 root root  4096 Aug  6 19:32 .npminstall_tarball
              drwxr-xr-x   2 root root  4096 Oct 15  2017 .pip
              drwxr-----   3 root root  4096 Nov  2 23:09 .pki
              -rw-r--r--   1 root root    64 Oct 15  2017 .pydistutils.cfg
              drwx------   2 root root  4096 Aug  6 22:17 .ssh
              -rw-r--r--.  1 root root   129 Dec 29  2013 .tcshrc
              -rw-------   1 root root  5842 Nov 29 15:27 .viminfo
              [root@initroot ~]#
            
以上命令列出当前工作目录下的所有文件信息,输出的字段信息详细解释可参考 linux文件属性与权限

列出所有文件的详细信息,但是不显示颜色信息,并且在文件名后显示文件类型信息:

              [root@initroot ~]# ls -alF --color=never
              total 92
              dr-xr-x---. 10 root root  4096 Dec 17 19:52 ./
              dr-xr-xr-x. 18 root root  4096 Aug  6 17:44 ../
              -rw-------   1 root root 14589 Jan  7 12:32 .bash_history
              -rw-r--r--.  1 root root    18 Dec 29  2013 .bash_logout
              -rw-r--r--   1 root root   213 Aug  6 19:16 .bash_profile
              -rw-r--r--.  1 root root   176 Dec 29  2013 .bashrc
              drwx------   3 root root  4096 Oct 15  2017 .cache/
              drwx------   3 root root  4096 Aug  6 19:24 .config/
              -rw-r--r--.  1 root root   100 Dec 29  2013 .cshrc
              drwxr-xr-x   3 root root  4096 Dec 17 19:52 kblogdepository/
              -rw-------   1 root root  2430 Oct 30 22:32 nohup.out
              drwxr-xr-x   5 root root  4096 Nov 19 23:27 .npm/
              drwxr-xr-x  29 root root  4096 Aug  6 19:32 .npminstall_tarball/
              drwxr-xr-x   2 root root  4096 Oct 15  2017 .pip/
              drwxr-----   3 root root  4096 Nov  2 23:09 .pki/
              -rw-r--r--   1 root root    64 Oct 15  2017 .pydistutils.cfg
              drwx------   2 root root  4096 Aug  6 22:17 .ssh/
              -rw-r--r--.  1 root root   129 Dec 29  2013 .tcshrc
              -rw-------   1 root root  5842 Nov 29 15:27 .viminfo
            
可以看到目录文件的后面加上了/,*代表可执行文件; /代表目录; =代表socket文件; |代表FIFO文件。关于文件类型的详细信息可以参考: linux文件和目录树结构
找出/etc下面不含子目录容量最大的文件:
              [root@initroot ~]# ls -ldSr /etc/*
              ...省略...
              -rw-r--r--  1 root root    10550 Jan 25  2018 /etc/login.defs
              drwxr-xr-x  2 root root    12288 Oct  3 13:00 /etc/alternatives
              -rw-r--r--  1 root root    12288 Jul 28  2019 /etc/aliases.db
              -rw-r--r--  1 root root    12636 May 22  2018 /etc/drirc
              -rw-r--r--  1 root root    14867 Oct 13  2016 /etc/ltrace.conf
              -rw-r--r--  1 root root    19183 Dec 26  2016 /etc/services
              -rw-r--r--  1 root root    24301 Jul 15  2016 /etc/mime.types
              -rw-r--r--  1 root root    25341 Aug 29  2018 /etc/brltty.conf
              -rw-r--r--  1 root root    42192 Oct 25 08:50 /etc/mailcap
              -rw-r--r--  1 root root   105060 Feb  8 22:42 /etc/ld.so.cache
            
显示文件的完整时间信息:
              [root@initroot ~]# ls -al --full-time
              total 92
              dr-xr-x---. 10 root root  4096 2019-12-17 19:52:02.249260369 +0800 .
              dr-xr-xr-x. 18 root root  4096 2019-08-06 17:44:27.696855703 +0800 ..
              -rw-------   1 root root 14589 2020-01-07 12:32:47.777317545 +0800 .bash_history
              -rw-r--r--.  1 root root    18 2013-12-29 10:26:31.000000000 +0800 .bash_logout
              -rw-r--r--   1 root root   213 2019-08-06 19:16:02.342574926 +0800 .bash_profile
              -rw-r--r--.  1 root root   176 2013-12-29 10:26:31.000000000 +0800 .bashrc
              drwx------   3 root root  4096 2017-10-15 23:24:07.543094665 +0800 .cache
              drwx------   3 root root  4096 2019-08-06 19:24:03.275758535 +0800 .config
              -rw-r--r--.  1 root root   100 2013-12-29 10:26:31.000000000 +0800 .cshrc
              drwxr-xr-x   3 root root  4096 2019-12-17 19:52:31.045727535 +0800 kblogdepository
              -rw-------   1 root root  2430 2019-10-30 22:32:48.557570646 +0800 nohup.out
              drwxr-xr-x   5 root root  4096 2019-11-19 23:27:34.141307214 +0800 .npm
              drwxr-xr-x  29 root root  4096 2019-08-06 19:32:20.167798791 +0800 .npminstall_tarball
              drwxr-xr-x   2 root root  4096 2017-10-15 23:24:59.173081672 +0800 .pip
              drwxr-----   3 root root  4096 2019-11-02 23:09:20.312375617 +0800 .pki
              -rw-r--r--   1 root root    64 2017-10-15 23:24:59.171081785 +0800 .pydistutils.cfg
              drwx------   2 root root  4096 2019-08-06 22:17:42.331936356 +0800 .ssh
              -rw-r--r--.  1 root root   129 2013-12-29 10:26:31.000000000 +0800 .tcshrc
              -rw-------   1 root root  5842 2019-11-29 15:27:56.136328392 +0800 .viminfo
            
可以看到完整的文件时间显示。
更多关于ls命令的选项我们就不在这里列出来,大家可以在自己的电脑上每个选项都自己动手试试。 值得注意的是,这些文件属性信息记录在哪里呢?这就是我们下一章节 linux磁盘与文件系统管理 会讲到的inode节点了。可以用-i选项显示inode节点号。
另外,很多linux发行版默认为用户提供了几个和ls命令有关的命令别名,通过alias命令可以查看系统中所有的命令别名:
              [root@initroot ~]# alias
              alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
              alias egrep='egrep --color=auto'
              alias fgrep='fgrep --color=auto'
              alias grep='grep --color=auto'
              alias l='ls -CF'
              alias la='ls -A'
              alias ll='ls -alF'
              alias ls='ls --color=auto'
            
后面四个都是和ls命令相关的命令别名l、la、ll、ls,其中最常用的是ll了。
关于命令别名参考: linux命令详解

initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读