linux命令详解之ls命令

àì夳堔傛蜴生んèń 2022-06-17 02:30 330阅读 0赞

ls命令概述

ls命令用于显示文件目录列表,和Windows系统下DOS命令dir类似。当执行ls命令时,默认显示的只有非隐藏文件的文件名、以文件名进行排序及文件名代表的颜色显示。当不加参数时,默认列出当前目录的列表信息。ls命令使用频率非常高,参数也非常多,多达四五十个,本文只介绍一部分常用的参数,其他参数可以通过man命令查看帮助手册。

说明:在linux下,文件名以点.开头表示该文件为隐藏文件,如.bashrc

ls命令语法

ls [选项] [目录或文件名]
参数:
-a--all的缩写,显示所有的文件,包括隐藏文件(以.开头的文件),参考示例1。(常用)
-A--almost-all的缩写,显示所有的文件,包括隐藏文件,但不包括表示当前目录.和上级目录..这两个文件,参考示例2。
-c:和-lt一起使用:显示列表并且以ctime(文件状态最后改变时间)排序。和-l一起使用:显示ctime并且以文件名排序。其他情况,以ctime排序。参考示例3。
-d--directory的缩写,仅列出目录本身,而不是列出目录里的内容列表,参考示例4。(常用)
-f:直接列出结果,而不进行排序(ls默认会以文件名排序)
--color[=WHEN]:是否根据文件类型显示颜色,WHEN可以为neveralways或者auto
--full-time:以完整的实际模式显示(包含年月日时分),类似与ls -l --time-style=full-iso,参考示例5。
-g:列表显示结果,和-l类似,但是不显示文件所属者。
-h:将文件内容大小以GB、KB等易读的方式显示,参考示例6。
-i:结合-l参数,列出每个文件的inode,参考示例7。
-l:列出长数据串,显示出文件的属性与权限等数据信息(常用)
-n:和-l类似,只是显示的所属用户和组不是名称而是对应的id,参考示例8。
-r--reverse,将排序结果以倒序方式显示,参考示例9。
-S:以文件大小排序,参考示例9。
-t:以修改时间排序
--help:显示帮助信息

ls命令示例

示例1:列出所有文件(注意和-A参数的区别,结果里面包括表示当前目录.和上级目录..这两个文件)。

  1. [root@localhost ~]# ls -a #列出所有文件
  2. . .. .bash_history .bash_logout .bash_profile .bashrc install.log install.log.syslog
  3. [root@localhost ~]# ls --all
  4. . .. .bash_history .bash_logout .bash_profile .bashrc install.log install.log.syslog

示例2:列出所有的文件,但不包括表示当前目录.和上级目录..这两个文件。

  1. [root@localhost ~]# ls -A
  2. .bash_history .bash_logout .bash_profile .bashrc install.log install.log.syslog
  3. [root@localhost ~]# ls --almost-all
  4. .bash_history .bash_logout .bash_profile .bashrc install.log install.log.syslog

示例3:显示列表并且以ctime排序

  1. [root@localhost /]# ls -clt #和 -lt参数一起使用,以时间排序
  2. total 146
  3. drwxrwxrwt 7 root root 4096 Apr 13 06:23 tmp
  4. drwxr-xr-x 12 root root 4440 Apr 13 05:18 dev
  5. drwxr-xr-x 93 root root 12288 Apr 13 05:18 etc
  6. drwxr-xr-x 2 root root 0 Apr 13 05:18 net
  7. drwxr-xr-x 2 root root 0 Apr 13 05:18 misc
  8. drwxr-xr-x 4 root root 0 Apr 13 05:16 selinux
  9. drwxr-xr-x 11 root root 0 Apr 13 05:16 sys
  10. dr-xr-xr-x 150 root root 0 Apr 13 05:16 proc
  11. drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin
  12. drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin
  13. drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64
  14. drwxr-x--- 4 root root 4096 Nov 19 06:52 root
  15. drwxr-xr-x 4 root root 4096 Nov 18 22:05 home
  16. drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib
  17. drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt
  18. drwxr-xr-x 22 root root 4096 Oct 17 2015 var
  19. drwxr-xr-x 4 root root 1024 Oct 17 2015 boot
  20. drwxr-xr-x 15 root root 4096 Oct 17 2015 usr
  21. drwxr-xr-x 2 root root 4096 Oct 17 2015 media
  22. drwxr-xr-x 2 root root 4096 Oct 17 2015 srv
  23. drwx------ 2 root root 16384 Oct 17 2015 lost+found
  24. drwxr-xr-x 3 root root 4096 Oct 17 2015 opt
  25. [root@localhost /]# ls -cl #和-l参数一起使用,以文件名排序并显示时间
  26. total 146
  27. drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin
  28. drwxr-xr-x 4 root root 1024 Oct 17 2015 boot
  29. drwxr-xr-x 12 root root 4440 Apr 13 05:18 dev
  30. drwxr-xr-x 93 root root 12288 Apr 13 05:18 etc
  31. drwxr-xr-x 4 root root 4096 Nov 18 22:05 home
  32. drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib
  33. drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64
  34. drwx------ 2 root root 16384 Oct 17 2015 lost+found
  35. drwxr-xr-x 2 root root 4096 Oct 17 2015 media
  36. drwxr-xr-x 2 root root 0 Apr 13 05:18 misc
  37. drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt
  38. drwxr-xr-x 2 root root 0 Apr 13 05:18 net
  39. drwxr-xr-x 3 root root 4096 Oct 17 2015 opt
  40. dr-xr-xr-x 150 root root 0 Apr 13 05:16 proc
  41. drwxr-x--- 4 root root 4096 Nov 19 06:52 root
  42. drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin
  43. drwxr-xr-x 4 root root 0 Apr 13 05:16 selinux
  44. drwxr-xr-x 2 root root 4096 Oct 17 2015 srv
  45. drwxr-xr-x 11 root root 0 Apr 13 05:16 sys
  46. drwxrwxrwt 7 root root 4096 Apr 13 06:23 tmp
  47. drwxr-xr-x 15 root root 4096 Oct 17 2015 usr
  48. drwxr-xr-x 22 root root 4096 Oct 17 2015 var
  49. [root@localhost /]# ls -c #单独使用,以时间排序,但不显示时间
  50. tmp dev etc net misc selinux sys proc sbin bin lib64 root home lib mnt var boot usr media srv lost+found opt

示例4:仅仅列出目录本身,不需要列出目录里的内容

  1. [root@localhost /]# ls -d /home #仅列出/home目录本身
  2. /home
  3. [root@localhost /]# ls /home #列出/home目录里的内容
  4. sgl software
  5. # 加上-l参数,比较的更清楚一些:
  6. [root@localhost /]# ls -ld /home
  7. drwxr-xr-x 4 root root 4096 Nov 18 22:05 /home
  8. [root@localhost /]# ls -l /home
  9. total 16
  10. drwx------ 16 sgl sgl 4096 Oct 17 2015 sgl
  11. drwxr-xr-x 3 root root 4096 Nov 14 05:13 software

示例5:显示完整时间

  1. [root@localhost ~]# ls --full-time /
  2. total 146
  3. drwxr-xr-x 2 root root 4096 2017-03-28 06:33:59.000000000 -0700 bin
  4. drwxr-xr-x 4 root root 1024 2015-10-17 08:08:24.000000000 -0700 boot
  5. drwxr-xr-x 12 root root 4440 2017-04-14 21:22:27.241239736 -0700 dev
  6. drwxr-xr-x 93 root root 12288 2017-04-14 21:22:24.000000000 -0700 etc
  7. drwxr-xr-x 4 root root 4096 2016-11-18 22:05:32.000000000 -0800 home
  8. drwxr-xr-x 11 root root 4096 2016-11-14 06:13:35.000000000 -0800 lib
  9. drwxr-xr-x 8 root root 4096 2017-03-28 06:33:33.000000000 -0700 lib64
  10. drwx------ 2 root root 16384 2015-10-17 08:01:52.000000000 -0700 lost+found
  11. drwxr-xr-x 2 root root 4096 2011-05-11 04:58:23.000000000 -0700 media
  12. drwxr-xr-x 2 root root 0 2017-04-14 21:22:23.789239736 -0700 misc
  13. drwxr-xr-x 3 root root 4096 2015-10-17 08:16:39.000000000 -0700 mnt
  14. drwxr-xr-x 2 root root 0 2017-04-14 21:22:23.839239736 -0700 net
  15. drwxr-xr-x 3 root root 4096 2015-10-17 00:17:25.000000000 -0700 opt
  16. dr-xr-xr-x 147 root root 0 2017-04-14 21:21:02.005999666 -0700 proc
  17. drwxr-x--- 4 root root 4096 2016-11-19 06:52:09.000000000 -0800 root
  18. drwxr-xr-x 2 root root 12288 2017-03-28 06:34:07.000000000 -0700 sbin
  19. drwxr-xr-x 4 root root 0 2017-04-14 21:21:02.671999847 -0700 selinux
  20. drwxr-xr-x 2 root root 4096 2011-05-11 04:58:23.000000000 -0700 srv
  21. drwxr-xr-x 11 root root 0 2017-04-14 21:21:02.657999847 -0700 sys
  22. drwxrwxrwt 7 root root 4096 2017-04-14 21:24:24.000000000 -0700 tmp
  23. drwxr-xr-x 15 root root 4096 2015-10-17 08:04:13.000000000 -0700 usr
  24. drwxr-xr-x 22 root root 4096 2015-10-17 08:11:05.000000000 -0700 var

示例6:以易读方式显示列表

  1. [root@localhost ~]# ls -lh / #注意列表容量大小列的单位
  2. total 146K
  3. drwxr-xr-x 2 root root 4.0K Mar 28 06:33 bin
  4. drwxr-xr-x 4 root root 1.0K Oct 17 2015 boot
  5. drwxr-xr-x 12 root root 4.4K Apr 14 21:22 dev
  6. drwxr-xr-x 93 root root 12K Apr 14 21:22 etc
  7. drwxr-xr-x 4 root root 4.0K Nov 18 22:05 home
  8. drwxr-xr-x 11 root root 4.0K Nov 14 06:13 lib
  9. drwxr-xr-x 8 root root 4.0K Mar 28 06:33 lib64
  10. drwx------ 2 root root 16K Oct 17 2015 lost+found
  11. drwxr-xr-x 2 root root 4.0K May 11 2011 media
  12. drwxr-xr-x 2 root root 0 Apr 14 21:22 misc
  13. drwxr-xr-x 3 root root 4.0K Oct 17 2015 mnt
  14. drwxr-xr-x 2 root root 0 Apr 14 21:22 net
  15. drwxr-xr-x 3 root root 4.0K Oct 17 2015 opt
  16. dr-xr-xr-x 147 root root 0 Apr 14 21:21 proc
  17. drwxr-x--- 4 root root 4.0K Nov 19 06:52 root
  18. drwxr-xr-x 2 root root 12K Mar 28 06:34 sbin
  19. drwxr-xr-x 4 root root 0 Apr 14 21:21 selinux
  20. drwxr-xr-x 2 root root 4.0K May 11 2011 srv
  21. drwxr-xr-x 11 root root 0 Apr 14 21:21 sys
  22. drwxrwxrwt 7 root root 4.0K Apr 14 21:24 tmp
  23. drwxr-xr-x 15 root root 4.0K Oct 17 2015 usr
  24. drwxr-xr-x 22 root root 4.0K Oct 17 2015 var
  25. [root@localhost ~]# ls -l / #默认方式,以字节为单位显示
  26. total 146
  27. drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin
  28. drwxr-xr-x 4 root root 1024 Oct 17 2015 boot
  29. drwxr-xr-x 12 root root 4440 Apr 14 21:22 dev
  30. drwxr-xr-x 93 root root 12288 Apr 14 21:22 etc
  31. drwxr-xr-x 4 root root 4096 Nov 18 22:05 home
  32. drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib
  33. drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64
  34. drwx------ 2 root root 16384 Oct 17 2015 lost+found
  35. drwxr-xr-x 2 root root 4096 May 11 2011 media
  36. drwxr-xr-x 2 root root 0 Apr 14 21:22 misc
  37. drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt
  38. drwxr-xr-x 2 root root 0 Apr 14 21:22 net
  39. drwxr-xr-x 3 root root 4096 Oct 17 2015 opt
  40. dr-xr-xr-x 147 root root 0 Apr 14 21:21 proc
  41. drwxr-x--- 4 root root 4096 Nov 19 06:52 root
  42. drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin
  43. drwxr-xr-x 4 root root 0 Apr 14 21:21 selinux
  44. drwxr-xr-x 2 root root 4096 May 11 2011 srv
  45. drwxr-xr-x 11 root root 0 Apr 14 21:21 sys
  46. drwxrwxrwt 7 root root 4096 Apr 14 21:24 tmp
  47. drwxr-xr-x 15 root root 4096 Oct 17 2015 usr
  48. drwxr-xr-x 22 root root 4096 Oct 17 2015 var

示例7:显示inode

  1. [root@localhost ~]# ls -li /
  2. total 146
  3. 3717313 drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin
  4. 2 drwxr-xr-x 4 root root 1024 Oct 17 2015 boot
  5. 1071 drwxr-xr-x 12 root root 4440 Apr 14 21:22 dev
  6. 1369537 drwxr-xr-x 93 root root 12288 Apr 14 21:22 etc
  7. 1402145 drwxr-xr-x 4 root root 4096 Nov 18 22:05 home
  8. 2380385 drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib
  9. 2184737 drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64
  10. 11 drwx------ 2 root root 16384 Oct 17 2015 lost+found
  11. 195649 drwxr-xr-x 2 root root 4096 May 11 2011 media
  12. 14244 drwxr-xr-x 2 root root 0 Apr 14 21:22 misc
  13. 1434753 drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt
  14. 14249 drwxr-xr-x 2 root root 0 Apr 14 21:22 net
  15. 391297 drwxr-xr-x 3 root root 4096 Oct 17 2015 opt
  16. 1 dr-xr-xr-x 147 root root 0 Apr 14 21:21 proc
  17. 65217 drwxr-x--- 4 root root 4096 Nov 19 06:52 root
  18. 3554273 drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin
  19. 591 drwxr-xr-x 4 root root 0 Apr 14 21:21 selinux
  20. 1956481 drwxr-xr-x 2 root root 4096 May 11 2011 srv
  21. 1 drwxr-xr-x 11 root root 0 Apr 14 21:21 sys
  22. 3815137 drwxrwxrwt 7 root root 4096 Apr 14 21:24 tmp
  23. 3456449 drwxr-xr-x 15 root root 4096 Oct 17 2015 usr
  24. 2576033 drwxr-xr-x 22 root root 4096 Oct 17 2015 var

示例8:列出文件夹内容,并显示出文件所属用户和组的id

  1. [root@localhost ~]# ls -ln /
  2. total 146
  3. drwxr-xr-x 2 0 0 4096 Mar 28 06:33 bin
  4. drwxr-xr-x 4 0 0 1024 Oct 17 2015 boot
  5. drwxr-xr-x 12 0 0 4440 Apr 14 21:22 dev
  6. drwxr-xr-x 93 0 0 12288 Apr 14 21:22 etc
  7. drwxr-xr-x 4 0 0 4096 Nov 18 22:05 home
  8. drwxr-xr-x 11 0 0 4096 Nov 14 06:13 lib
  9. drwxr-xr-x 8 0 0 4096 Mar 28 06:33 lib64
  10. drwx------ 2 0 0 16384 Oct 17 2015 lost+found
  11. drwxr-xr-x 2 0 0 4096 May 11 2011 media
  12. drwxr-xr-x 2 0 0 0 Apr 14 21:22 misc
  13. drwxr-xr-x 3 0 0 4096 Oct 17 2015 mnt
  14. drwxr-xr-x 2 0 0 0 Apr 14 21:22 net
  15. drwxr-xr-x 3 0 0 4096 Oct 17 2015 opt
  16. dr-xr-xr-x 147 0 0 0 Apr 14 21:21 proc
  17. drwxr-x--- 4 0 0 4096 Nov 19 06:52 root
  18. drwxr-xr-x 2 0 0 12288 Mar 28 06:34 sbin
  19. drwxr-xr-x 4 0 0 0 Apr 14 21:21 selinux
  20. drwxr-xr-x 2 0 0 4096 May 11 2011 srv
  21. drwxr-xr-x 11 0 0 0 Apr 14 21:21 sys
  22. drwxrwxrwt 7 0 0 4096 Apr 14 21:24 tmp
  23. drwxr-xr-x 15 0 0 4096 Oct 17 2015 usr
  24. drwxr-xr-x 22 0 0 4096 Oct 17 2015 var

示例9:以文件大小排序(升序和降序)

  1. [root@localhost ~]# ls -lS / #默认降序排序
  2. total 146
  3. drwx------ 2 root root 16384 Oct 17 2015 lost+found
  4. drwxr-xr-x 93 root root 12288 Apr 14 21:22 etc
  5. drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin
  6. drwxr-xr-x 12 root root 4440 Apr 14 21:22 dev
  7. drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin
  8. drwxr-xr-x 4 root root 4096 Nov 18 22:05 home
  9. drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib
  10. drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64
  11. drwxr-xr-x 2 root root 4096 May 11 2011 media
  12. drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt
  13. drwxr-xr-x 3 root root 4096 Oct 17 2015 opt
  14. drwxr-x--- 4 root root 4096 Nov 19 06:52 root
  15. drwxr-xr-x 2 root root 4096 May 11 2011 srv
  16. drwxrwxrwt 7 root root 4096 Apr 14 21:24 tmp
  17. drwxr-xr-x 15 root root 4096 Oct 17 2015 usr
  18. drwxr-xr-x 22 root root 4096 Oct 17 2015 var
  19. drwxr-xr-x 4 root root 1024 Oct 17 2015 boot
  20. drwxr-xr-x 2 root root 0 Apr 14 21:22 misc
  21. drwxr-xr-x 2 root root 0 Apr 14 21:22 net
  22. dr-xr-xr-x 147 root root 0 Apr 14 21:21 proc
  23. drwxr-xr-x 4 root root 0 Apr 14 21:21 selinux
  24. drwxr-xr-x 11 root root 0 Apr 14 21:21 sys
  25. [root@localhost ~]# ls -lSr / #通过-r参数实现升序排列
  26. total 146
  27. drwxr-xr-x 11 root root 0 Apr 14 21:21 sys
  28. drwxr-xr-x 4 root root 0 Apr 14 21:21 selinux
  29. dr-xr-xr-x 147 root root 0 Apr 14 21:21 proc
  30. drwxr-xr-x 2 root root 0 Apr 14 21:22 net
  31. drwxr-xr-x 2 root root 0 Apr 14 21:22 misc
  32. drwxr-xr-x 4 root root 1024 Oct 17 2015 boot
  33. drwxr-xr-x 22 root root 4096 Oct 17 2015 var
  34. drwxr-xr-x 15 root root 4096 Oct 17 2015 usr
  35. drwxrwxrwt 7 root root 4096 Apr 14 21:24 tmp
  36. drwxr-xr-x 2 root root 4096 May 11 2011 srv
  37. drwxr-x--- 4 root root 4096 Nov 19 06:52 root
  38. drwxr-xr-x 3 root root 4096 Oct 17 2015 opt
  39. drwxr-xr-x 3 root root 4096 Oct 17 2015 mnt
  40. drwxr-xr-x 2 root root 4096 May 11 2011 media
  41. drwxr-xr-x 8 root root 4096 Mar 28 06:33 lib64
  42. drwxr-xr-x 11 root root 4096 Nov 14 06:13 lib
  43. drwxr-xr-x 4 root root 4096 Nov 18 22:05 home
  44. drwxr-xr-x 2 root root 4096 Mar 28 06:33 bin
  45. drwxr-xr-x 12 root root 4440 Apr 14 21:22 dev
  46. drwxr-xr-x 2 root root 12288 Mar 28 06:34 sbin
  47. drwxr-xr-x 93 root root 12288 Apr 14 21:22 etc
  48. drwx------ 2 root root 16384 Oct 17 2015 lost+found

注:本系列内容主要参考《鸟哥的linux私房菜》和CentOS系统自带的帮助文档以及网上相关资料,示例都是基于CentOS。

发表评论

表情:
评论列表 (有 0 条评论,330人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ls命令详解

    用法:ls \[选项\]… \[文件\]… 必选参数对长短选项同时适用。 \-a, --all 不隐藏任何以. 开始的项目 \-A, --almost-all 列出除

    相关 linux命令 ls

    命令的常用格式: 命令 【-选项】【-参数】 当有多个选项的时候可以将选项写在一起,没有顺序之分 如 : ls -l -a 可以写成 ls -la ls 查看目录列表

    相关 Linux ls 命令

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