linux命令学习之:ls

 ls命令用来显示目标列表,在Linux中是使用率较高的命令。ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件。
语法

  ls(选项)(参数)

选项说明

-a:显示所有档案及目录(ls内定将档案名或目录名称为“.”的视为影藏,不会列出);

-A:显示除影藏文件“.”和“..”以外的所有文件列表;

-C:多列显示输出结果。这是默认选项;

-l:与“-C”选项功能相反,所有输出信息用单列格式输出,不输出为多列;

-F:在每个输出项后追加文件的类型标识符,具体含义:“*”表示具有可执行权限的普通文件,“/”表示目录,“@”表示符号链接,“|”表示命令管道FIFO,“=”表示sockets套接字。当文件为普通文件时,不输出任何标识符;

-b:将文件中的不可输出的字符以反斜线“”加字符编码的方式输出;

-c:与“-lt”选项连用时,按照文件状态时间排序输出目录内容,排序的依据是文件的索引节点中的ctime字段。与“-l”选项连用时,则排序的一句是文件的状态改变时间; 

-d:仅显示目录名,而不显示目录下的内容列表。显示符号链接文件本身,而不显示其所指向的目录列表;

-f:此参数的效果和同时指定“aU”参数相同,并关闭“lst”参数的效果; -i:显示文件索引节点号(inode)。一个索引节点代表一个文件;

--file-type:与“-F”选项的功能相同,但是不显示“*”;

-k:以KB(千字节)为单位显示文件大小;

-l:以长格式显示目录下的内容列表。输出的信息从左到右依次包括文件名,文件类型、权限模式、硬连接数、所有者、组、文件大小和文件的最后修改时间等;

-m:用“,”号区隔每个文件和目录的名称;

-n:以用户识别码和群组识别码替代其名称;

-r:以文件名反序排列并输出目录内容列表;

-s:显示文件和目录的大小,以区块为单位;

-t:用文件和目录的更改时间排序;

-L:如果遇到性质为符号链接的文件或目录,直接列出该链接所指向的原始文件或目录;

-R:递归处理,将指定目录下的所有文件及子目录一并处理;

--full-time:列出完整的日期与时间;

--color[=WHEN]:使用不同的颜色高亮显示不同类型的。

参数说明

  目录:指定要显示列表的目录,也可以是具体的文件。
实例

1. 不带参数运行ls

  不带参数运行ls会只列出文件或者目录。看不到其他信息输出(译注:有时候你发现无参数的ls命令和这里描述的不同,那有可能是你的ls命令实际上带参数的ls别名)。

[root@CTU1000094955 ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos

2. 使用长清单模式

  使用-l字符(小写L字符),会显示当前目录内容的长列表。在接下来的例子中,我们会结合-l参数(这个参数经常使用)来得到更好的结果。

$ ls -l

        

  这里是如何读取输出 :

  • 第1列

    • 第一个字母d意味着内容是目录或者文件。在上面的截图中,Desktop、 Documents、 Downloads 和 lynis-1.3.8是目录。如果是'-'(减号),这意味着它的内容是文件。当它是l(小写l字符),意味这内容是链接文件。
    • 下面的9个字符是关于文件权限。前3个rwx字符是文件的拥有者的权限,第二组3rwx是文件的所有组的权限,最后的rwx是对其他人访问文件的权限。
  • 第2列 这行告诉我们有多少链接指向这个文件。

  • 第3列 这行告诉我们谁是这个文件/文件夹的所有者。

  • 第4列 这行告诉我们谁是这个文件/文件夹的所有组。

  • 第5列 这行告诉我们这个文件/文件夹的以字节为单位的大小。 目录的大小总是4096字节。

  • 第6列 这告诉我们文件最后的修改时间。

  • 第7列 这告诉我们文件名或者目录名。

3. 显示文件大小

  以字节为单位看大小可能会不方便。6.5M读起来比6727680字节更简单。要这么做,我们可以使用-h与-l结合的参数。-h参数意味着便于人识别。

[root@CTU1000094955 ~]# ls -lh
total 40K
-rw-------. 1 root root 1.7K Jan 16  2017 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Desktop
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Documents
drwxr-xr-x. 5 root root 4.0K Nov  1 18:16 Downloads
-rw-------. 1 root root 1.7K Jan 16  2017 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Music
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Pictures
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Public
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Templates
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Videos

  另外一个可以这么做的参数是--si。这个参数和-h参数类似,但是-si以1000为单位,而-h以1024为单位。

[root@CTU1000094955 ~]# ls -si
total 40
114695 4 anaconda-ks.cfg  114696 4 initial-setup-ks.cfg  114704 4 Templates
114702 4 Desktop          114707 4 Music                 114709 4 Videos
114706 4 Documents        114708 4 Pictures
114703 4 Downloads        114705 4 Public

4. 排序文件大小

  在我们可以显示文件大小之后,我们希望以文件大小排序。我们可以使用-S参数来这么做。这列表会从大到校排序。

[root@CTU1000094955 ~]# ls -lhS
total 40K
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Desktop
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Documents
drwxr-xr-x. 5 root root 4.0K Nov  1 18:16 Downloads
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Music
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Pictures
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Public
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Templates
drwxr-xr-x. 2 root root 4.0K Jan 16  2017 Videos
-rw-------. 1 root root 1.7K Jan 16  2017 initial-setup-ks.cfg
-rw-------. 1 root root 1.7K Jan 16  2017 anaconda-ks.cfg

5. 测量大小

  ls可以通过使用-block-size=SIZE改单位大小。这里的SIZE是:

K = Kilobyte
M = Megabyte
G = Gigabyte
T = Terabyte
P = Petabyte
E = Exabyte
Z = Zettabyte
Y = Yottabyte

  比如,我们希望使用MB作为单位大小。所以语法就会像这样:

[root@CTU1000094955 ~]# ls -l --block-size=M
total 1M
-rw-------. 1 root root 1M Jan 16  2017 anaconda-ks.cfg
drwxr-xr-x. 2 root root 1M Jan 16  2017 Desktop
drwxr-xr-x. 2 root root 1M Jan 16  2017 Documents
drwxr-xr-x. 5 root root 1M Nov  1 18:16 Downloads
-rw-------. 1 root root 1M Jan 16  2017 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 1M Jan 16  2017 Music
drwxr-xr-x. 2 root root 1M Jan 16  2017 Pictures
drwxr-xr-x. 2 root root 1M Jan 16  2017 Public
drwxr-xr-x. 2 root root 1M Jan 16  2017 Templates
drwxr-xr-x. 2 root root 1M Jan 16  2017 Videos

6. 显示隐藏文件

  在Linux中,以"."(点号)开头的文件是隐藏文件。为了在ls命令中显示它,我们可以使用-a选项。

[root@CTU1000094955 ~]# ls -a
.                .bashrc    Downloads             .mysql_history     .tcshrc
..               .cache     .esd_auth             .mysql_secret      Templates
anaconda-ks.cfg  .config    .ICEauthority         .oracle_jre_usage  Videos
.bash_history    .cshrc     initial-setup-ks.cfg  Pictures           .viminfo
.bash_logout     Desktop    .local                Public
.bash_profile    Documents  Music                 .ssh

7. 只列出目录条目

  如果我们希望只列出目录,我们可以使用-d选项。

[root@CTU1000094955 ~]# ls -d */
Desktop/    Downloads/  Pictures/  Templates/
Documents/  Music/      Public/    Videos/

8. 不打印所有者信息

  要这么做,我们使用-g选项。

[root@CTU1000094955 cmdp]# ll
total 121696
drwxrwxrwx. 8 root root      4096 Nov 16 21:07 attachment
drwxr-xr-x. 6   10  143      4096 Dec 13  2016 jre
-rw-r--r--. 1 root root 124471608 Nov  1 19:07 myfile.zip
drwxr-xr-x. 2 root root      4096 Oct 28 18:04 mysql
drwxr-xr-x. 9 root root      4096 Nov  1 19:08 tomcat
[root@CTU1000094955 cmdp]# ll -g
total 121696
drwxrwxrwx. 8 root      4096 Nov 16 21:07 attachment
drwxr-xr-x. 6  143      4096 Dec 13  2016 jre
-rw-r--r--. 1 root 124471608 Nov  1 19:07 myfile.zip
drwxr-xr-x. 2 root      4096 Oct 28 18:04 mysql
drwxr-xr-x. 9 root      4096 Nov  1 19:08 tomcat

9. 不打印组信息

  -g隐藏了拥有者信息,—G会隐藏组信息。

[root@CTU1000094955 cmdp]# ll
total 121696
drwxrwxrwx. 8 root root      4096 Nov 16 21:07 attachment
drwxr-xr-x. 6   10  143      4096 Dec 13  2016 jre
-rw-r--r--. 1 root root 124471608 Nov  1 19:07 myfile.zip
drwxr-xr-x. 2 root root      4096 Oct 28 18:04 mysql
drwxr-xr-x. 9 root root      4096 Nov  1 19:08 tomcat
[root@CTU1000094955 cmdp]# ls -lG
total 121696
drwxrwxrwx. 8 root      4096 Nov 16 21:07 attachment
drwxr-xr-x. 6   10      4096 Dec 13  2016 jre
-rw-r--r--. 1 root 124471608 Nov  1 19:07 myfile.zip
drwxr-xr-x. 2 root      4096 Oct 28 18:04 mysql
drwxr-xr-x. 9 root      4096 Nov  1 19:08 tomcat

10. 打印UID和GID

  如果你想以数字方式列出项的所有者和所有组(即UID和GID),我们可以带-n选项使用ls命令。这里是个例子。

[root@CTU1000094955 cmdp]# ll
total 121696
drwxrwxrwx. 8 root root      4096 Nov 16 21:07 attachment
drwxr-xr-x. 6   10  143      4096 Dec 13  2016 jre
-rw-r--r--. 1 root root 124471608 Nov  1 19:07 myfile.zip
drwxr-xr-x. 2 root root      4096 Oct 28 18:04 mysql
drwxr-xr-x. 9 root root      4096 Nov  1 19:08 tomcat
[root@CTU1000094955 cmdp]# ls -n
total 121696
drwxrwxrwx. 8  0   0      4096 Nov 16 21:07 attachment
drwxr-xr-x. 6 10 143      4096 Dec 13  2016 jre
-rw-r--r--. 1  0   0 124471608 Nov  1 19:07 myfile.zip
drwxr-xr-x. 2  0   0      4096 Oct 28 18:04 mysql
drwxr-xr-x. 9  0   0      4096 Nov  1 19:08 tomcat

  从上面的例子中,我们知道用户pungki的UID是100,GID是1000,而root组的GID是0。

11. 不带颜色打印

  一些Linux发行版已经对ls命令启用彩色。这会使ls以各种颜色打印列表。如果你不想要这样,你可以使用 --color=never 参数。

[root@CTU1000094955 cmdp]# ls
attachment  jre  myfile.zip  mysql  tomcat
[root@CTU1000094955 cmdp]# ls --color=never
attachment  jre  myfile.zip  mysql  tomcat

12. 打印每个文件的索引号

  为了打印索引或者大家俗称的inode号,我们可以使用-i选项。索引号会显示在第一列。

[root@CTU1000094955 cmdp]# ls -li
total 121696
9584645 drwxrwxrwx. 8 root root      4096 Nov 16 21:07 attachment
9585775 drwxr-xr-x. 6   10  143      4096 Dec 13  2016 jre
9584646 -rw-r--r--. 1 root root 124471608 Nov  1 19:07 myfile.zip
9584644 drwxr-xr-x. 2 root root      4096 Oct 28 18:04 mysql
9584647 drwxr-xr-x. 9 root root      4096 Nov  1 19:08 tomcat

13. 增加 / (斜线) 标记目录

  要这么做,使用-p选项。

[root@CTU1000094955 cmdp]# ls -p
attachment/  jre/  myfile.zip  mysql/  tomcat/

14. 排序时反转顺序

  你或许需要在列出条目时反转顺序。要这么做,你可以使用-r选项。

[root@CTU1000094955 cmdp]# ls -p
attachment/  jre/  myfile.zip  mysql/  tomcat/
[root@CTU1000094955 cmdp]# ls -r
tomcat  mysql  myfile.zip  jre  attachment

15. 递归列出子目录

  带-R参数后,你可以列出包含它子目录的目录。

[root@CTU1000094955 Downloads]# ls -R
.:
apache-tomcat-8.5.8.zip               RUNNING.txt
MySQL-client-5.6.36-1.el7.x86_64.rpm  secure
MySQL-server-5.6.36-1.el7.x86_64.rpm  temp
MySQL-shared-5.6.36-1.el7.x86_64.rpm  webapps
NOTICE

./secure:
cer  maas  maas_s

./secure/cer:
resttruststore.keystore

./secure/maas:
content.properties

./secure/maas_s:
xwork.properties

16. 扩展名排序

  你可以使用-X参数或者--sort=extension来通过扩展名来排序(译注:这样对于筛选不同类型的文件很有用)。

[root@CTU1000094955 Downloads]# ls -lX
total 90664
-rw-r--r--. 1 root root     1774 Nov  3  2016 NOTICE
drwxr-xr-x. 5 root root     4096 Dec 22  2016 secure
drwxr-xr-x. 2 root root     4096 Nov  3  2016 temp
drwxr-xr-x. 2 root root     4096 Nov  3  2016 webapps
-rw-r--r--. 1 root root 20314880 Oct 28 16:29 MySQL-client-5.6.36-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 62378644 Oct 28 16:30 MySQL-server-5.6.36-1.el7.x86_64.rpm
-rw-r--r--. 1 root root  2103908 Oct 28 16:30 MySQL-shared-5.6.36-1.el7.x86_64.rpm
-rw-r--r--. 1 root root    16416 Nov  3  2016 RUNNING.txt
-rw-r--r--. 1 root root  7889101 Oct 28 17:36 apache-tomcat-8.5.8.zip
[root@CTU1000094955 Downloads]# ls --sort=extension
NOTICE  webapps                               MySQL-shared-5.6.36-1.el7.x86_64.rpm
secure  MySQL-client-5.6.36-1.el7.x86_64.rpm  RUNNING.txt
temp    MySQL-server-5.6.36-1.el7.x86_64.rpm  apache-tomcat-8.5.8.zip

17. 通过修改时间列出

  使用-t选项会按修改时间排序,新的文件在前。

[root@CTU1000094955 Downloads]# ls -lt
total 90664
-rw-r--r--. 1 root root  7889101 Oct 28 17:36 apache-tomcat-8.5.8.zip
-rw-r--r--. 1 root root 62378644 Oct 28 16:30 MySQL-server-5.6.36-1.el7.x86_64.rpm
-rw-r--r--. 1 root root  2103908 Oct 28 16:30 MySQL-shared-5.6.36-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 20314880 Oct 28 16:29 MySQL-client-5.6.36-1.el7.x86_64.rpm
drwxr-xr-x. 5 root root     4096 Dec 22  2016 secure
drwxr-xr-x. 2 root root     4096 Nov  3  2016 temp
drwxr-xr-x. 2 root root     4096 Nov  3  2016 webapps
-rw-r--r--. 1 root root     1774 Nov  3  2016 NOTICE
-rw-r--r--. 1 root root    16416 Nov  3  2016 RUNNING.txt

18. 列出你的主目录

  要列出你的主目录,你可以用"~"(波浪号)来代表它。这样你就不必输入完整的目录名。让我们假设家文件名为/home/pungki,那么波浪号就对/home/pungki有意义了。

[root@CTU1000094955 Downloads]# ls ~
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos
[root@CTU1000094955 Downloads]# ls /root
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos

19. 列出父目录

  无论你在那个目录,你可以列出父目录而不必输入完整路径。这是个例子。

$ ls ../     这回列出1层之上的目录内容。

$ ls ../../  这回列出2层之上的目录内容(译注:可不支持“...”来代表2层之上)。

[root@CTU1000094955 webapps]# ls ../
bin  conf  lib  LICENSE  logs  NOTICE  ps  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@CTU1000094955 webapps]# ls ../..
attachment  jre  myfile.zip  mysql  tomcat  

20. 打印ls命令版本

  使用--version参数打印它。

[root@CTU1000094955 webapps]# ls --version
ls (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.

总结

  这些是在日常操作中会使用到的参数。当然你总可以输入man ls 或者 ls --help 来查询ls的手册页。

[root@CTU1000094955 webapps]# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; see SIZE format below
... ...
原文地址:https://www.cnblogs.com/jing99/p/7876082.html