linux命令详解之ls命令

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可以为never、always或者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命令示例

[centos@s202 ~]$ ls
1.log      a.txt       coprocessor.log  filename.txt  for99.sh  hadoop-2.7.3.tar.gz  ini                         log        student.txt  test4.sh  tf.sh
1.txt      caseeg.sh   file             file.out      ha        hadoop.txt           jdk-8u131-linux-x64.tar.gz  logs       test2.sh     test5.sh  zookeeper
anaconda3  color99.sh  file.err         for99         hadoop    header.txt           kafka                       logZip.sh  test3.sh     test.sh   zookeeper.out

 

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

[centos@s202 ~]$ ls -a
.      anaconda3      .bash_profile          color99.sh       file.err      for99     hadoop-2.7.3.tar.gz  jdk-8u131-linux-x64.tar.gz  logZip.sh          student.txt  test5.sh   zookeeper.out
..     a.txt          .bashrc                .continuum       filename.txt  for99.sh  hadoop.txt           kafka                       .oracle_jre_usage  test2.sh     test.sh
1.log  .bash_history  .bashrc-anaconda3.bak  coprocessor.log  file.out      ha        header.txt           log                         .python_history    test3.sh     tf.sh
1.txt  .bash_logout   caseeg.sh              file             .flume        hadoop    ini                  logs                        .ssh               test4.sh     zookeeper

 

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

[centos@s202 ~]$ ls -A
1.log      .bash_history  .bashrc-anaconda3.bak  coprocessor.log  file.out  ha                   header.txt                  log                .python_history  test3.sh  tf.sh
1.txt      .bash_logout   caseeg.sh              file             .flume    hadoop               ini                         logs               .ssh             test4.sh  zookeeper
anaconda3  .bash_profile  color99.sh             file.err         for99     hadoop-2.7.3.tar.gz  jdk-8u131-linux-x64.tar.gz  logZip.sh          student.txt      test5.sh  zookeeper.out
a.txt      .bashrc        .continuum             filename.txt     for99.sh  hadoop.txt           kafka                       .oracle_jre_usage  test2.sh         test.sh
[centos@s202 ~]$ ls --almost-all
1.log      .bash_history  .bashrc-anaconda3.bak  coprocessor.log  file.out  ha                   header.txt                  log                .python_history  test3.sh  tf.sh
1.txt      .bash_logout   caseeg.sh              file             .flume    hadoop               ini                         logs               .ssh             test4.sh  zookeeper
anaconda3  .bash_profile  color99.sh             file.err         for99     hadoop-2.7.3.tar.gz  jdk-8u131-linux-x64.tar.gz  logZip.sh          student.txt      test5.sh  zookeeper.out
a.txt      .bashrc        .continuum             filename.txt     for99.sh  hadoop.txt           kafka                       .oracle_jre_usage  test2.sh         test.sh

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

[centos@s202 ~]$ ls -clt
total 390824
-rw-rw-r--.  1 centos centos        65 May 23 14:59 1.log
-rw-rw-r--.  1 centos centos       157 May 23 14:59 test5.sh
-rw-rw-r--.  1 centos centos       205 May 23 11:27 logZip.sh
-rw-rw-r--.  1 centos centos       108 May 23 11:15 test4.sh
-rw-rw-r--.  1 centos centos       139 May 23 09:15 ini
-rw-rw-r--.  1 centos centos       268 May 23 01:16 test3.sh
-rw-rw-r--.  1 centos centos        51 May 21 17:38 filename.txt
-rw-rw-r--.  1 centos centos         1 May 21 17:38 file.err
-rw-rw-r--.  1 centos centos         1 May 21 17:37 file.out
-rw-rw-r--.  1 centos centos        51 May 21 17:30 a.txt
-rw-rw-r--.  1 centos centos        56 May 21 16:33 test.sh
-rw-rw-r--.  1 centos centos         5 May 21 14:39 file
-rw-rw-r--.  1 centos centos         8 Apr  5 01:18 test2.sh
drwxrwxr-x.  3 centos centos        35 Apr  5 01:03 zookeeper
-rw-rw-r--.  1 centos centos    474970 Apr  5 01:03 zookeeper.out
drwxrwxr-x. 12 centos centos       150 Jan 26 17:06 anaconda3
drwxrwxr-x.  3 centos centos        18 Nov  2  2018 kafka
drwxrwxr-x.  4 centos centos        37 Oct 11  2018 ha
-rw-rw-r--.  1 centos centos         4 Aug 24  2018 student.txt
drwxrwxr-x.  2 centos centos        63 Aug 24  2018 log
drwxrwxr-x.  2 centos centos         6 Aug 24  2018 logs
-rw-rw-r--.  1 centos centos        10 Aug 24  2018 header.txt
-rw-rw-r--.  1 centos centos       219 Aug 18  2018 coprocessor.log
drwxrwxr-x.  4 centos centos        37 Jul 22  2018 hadoop
-rw-rw-r--.  1 centos centos      1060 Jul 20  2018 hadoop.txt
-rw-rw-r--.  1 centos centos 214092195 Jul 20  2018 hadoop-2.7.3.tar.gz
-rwxrwxr-x.  1 centos centos        86 Jul 17  2018 caseeg.sh
-rwxrw-r--.  1 centos centos        90 Jul 17  2018 color99.sh
-rwxrwxr-x.  1 centos centos        53 Jul 17  2018 tf.sh
-rwxrwxr-x.  1 centos centos       140 Jul 17  2018 for99.sh
-rwxrwxr-x.  1 centos centos       108 Jul 17  2018 for99
-rw-rw-r--.  1 centos centos        20 Jul 17  2018 1.txt
-rw-rw-r--.  1 centos centos 185540433 Jul 17  2018 jdk-8u131-linux-x64.tar.gz
[centos@s202 ~]$  ls -cl
total 390824
-rw-rw-r--.  1 centos centos        65 May 23 14:59 1.log
-rw-rw-r--.  1 centos centos        20 Jul 17  2018 1.txt
drwxrwxr-x. 12 centos centos       150 Jan 26 17:06 anaconda3
-rw-rw-r--.  1 centos centos        51 May 21 17:30 a.txt
-rwxrwxr-x.  1 centos centos        86 Jul 17  2018 caseeg.sh
-rwxrw-r--.  1 centos centos        90 Jul 17  2018 color99.sh
-rw-rw-r--.  1 centos centos       219 Aug 18  2018 coprocessor.log
-rw-rw-r--.  1 centos centos         5 May 21 14:39 file
-rw-rw-r--.  1 centos centos         1 May 21 17:38 file.err
-rw-rw-r--.  1 centos centos        51 May 21 17:38 filename.txt
-rw-rw-r--.  1 centos centos         1 May 21 17:37 file.out
-rwxrwxr-x.  1 centos centos       108 Jul 17  2018 for99
-rwxrwxr-x.  1 centos centos       140 Jul 17  2018 for99.sh
drwxrwxr-x.  4 centos centos        37 Oct 11  2018 ha
drwxrwxr-x.  4 centos centos        37 Jul 22  2018 hadoop
-rw-rw-r--.  1 centos centos 214092195 Jul 20  2018 hadoop-2.7.3.tar.gz
-rw-rw-r--.  1 centos centos      1060 Jul 20  2018 hadoop.txt
-rw-rw-r--.  1 centos centos        10 Aug 24  2018 header.txt
-rw-rw-r--.  1 centos centos       139 May 23 09:15 ini
-rw-rw-r--.  1 centos centos 185540433 Jul 17  2018 jdk-8u131-linux-x64.tar.gz
drwxrwxr-x.  3 centos centos        18 Nov  2  2018 kafka
drwxrwxr-x.  2 centos centos        63 Aug 24  2018 log
drwxrwxr-x.  2 centos centos         6 Aug 24  2018 logs
-rw-rw-r--.  1 centos centos       205 May 23 11:27 logZip.sh
-rw-rw-r--.  1 centos centos         4 Aug 24  2018 student.txt
-rw-rw-r--.  1 centos centos         8 Apr  5 01:18 test2.sh
-rw-rw-r--.  1 centos centos       268 May 23 01:16 test3.sh
-rw-rw-r--.  1 centos centos       108 May 23 11:15 test4.sh
-rw-rw-r--.  1 centos centos       157 May 23 14:59 test5.sh
-rw-rw-r--.  1 centos centos        56 May 21 16:33 test.sh
-rwxrwxr-x.  1 centos centos        53 Jul 17  2018 tf.sh
drwxrwxr-x.  3 centos centos        35 Apr  5 01:03 zookeeper
-rw-rw-r--.  1 centos centos    474970 Apr  5 01:03 zookeeper.out

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

[centos@s202 ~]$ ls -d /home   #仅列出/home目录本身
/home
[centos@s202 ~]$ ls /home   #列出/home目录里的内容
centos

示例5:显示完整时间

[centos@s202 ~]$ ls --full-time /
total 20
lrwxrwxrwx.   1 root   root      7 2018-07-16 22:14:25.675333601 +0800 bin -> usr/bin
dr-xr-xr-x.   4 root   root   4096 2018-07-16 22:20:27.271001648 +0800 boot
drwxr-xr-x.  20 root   root   3180 2019-05-24 20:32:14.446999702 +0800 dev
drwxr-xr-x.  78 root   root   8192 2019-05-24 20:32:06.675999960 +0800 etc
drwxr-xr-x.   3 root   root     20 2018-07-16 22:18:42.624343276 +0800 home
lrwxrwxrwx.   1 root   root      7 2018-07-16 22:14:25.677333601 +0800 lib -> usr/lib
lrwxrwxrwx.   1 root   root      9 2018-07-16 22:14:25.677333601 +0800 lib64 -> usr/lib64
drwxr-xr-x.   2 root   root      6 2016-11-05 23:38:36.000000000 +0800 media
drwxr-xr-x.   2 root   root      6 2016-11-05 23:38:36.000000000 +0800 mnt
drwxr-xr-x.   2 root   root     45 2019-01-26 17:08:03.622231114 +0800 opt
dr-xr-xr-x. 156 root   root      0 2019-05-24 20:31:59.011000000 +0800 proc
dr-xr-x---.   7 root   root    209 2018-08-31 14:37:12.818765051 +0800 root
drwxr-xr-x.  21 root   root    600 2019-05-24 20:32:26.359999305 +0800 run
lrwxrwxrwx.   1 root   root      8 2018-07-16 22:14:25.677333601 +0800 sbin -> usr/sbin
drwxr-xr-x.  10 centos centos 4096 2018-10-09 15:46:23.455045955 +0800 soft
drwxr-xr-x.   2 root   root      6 2016-11-05 23:38:36.000000000 +0800 srv
dr-xr-xr-x.  13 root   root      0 2019-05-24 20:32:07.496999933 +0800 sys
drwxrwxrwt.   7 root   root     93 2019-05-24 21:31:02.252882289 +0800 tmp
drwxr-xr-x.  13 root   root    155 2018-07-16 22:14:25.839333607 +0800 usr
drwxr-xr-x.  19 root   root    267 2019-05-24 20:32:06.676999960 +0800 var

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

[centos@s203 ~]$ ls -l
total 524
drwxrwxr-x. 12 centos centos    150 Jan 26 17:17 anaconda3
drwxrwxr-x.  4 centos centos     37 Oct 11  2018 ha
drwxrwxr-x.  4 centos centos     37 Jul 22  2018 hadoop
drwxrwxr-x.  3 centos centos     18 Nov  2  2018 kafka
drwxrwxr-x.  2 centos centos     19 Aug 23  2018 log
drwxrwxr-x.  2 centos centos     21 Aug 24  2018 logs
drwxrwxr-x.  3 centos centos     56 Apr  5 01:03 zookeeper
-rw-rw-r--.  1 centos centos 534884 Apr  5 01:03 zookeeper.out
[centos@s203 ~]$ ls -lh
total 524K
drwxrwxr-x. 12 centos centos  150 Jan 26 17:17 anaconda3
drwxrwxr-x.  4 centos centos   37 Oct 11  2018 ha
drwxrwxr-x.  4 centos centos   37 Jul 22  2018 hadoop
drwxrwxr-x.  3 centos centos   18 Nov  2  2018 kafka
drwxrwxr-x.  2 centos centos   19 Aug 23  2018 log
drwxrwxr-x.  2 centos centos   21 Aug 24  2018 logs
drwxrwxr-x.  3 centos centos   56 Apr  5 01:03 zookeeper
-rw-rw-r--.  1 centos centos 523K Apr  5 01:03 zookeeper.out
[centos@s203 ~]$

示例7:显示inode

what is inode?

 

[centos@s203 ~]$ ls -li /
total 20
      95 lrwxrwxrwx.   1 root   root      7 Jul 16  2018 bin -> usr/bin
      64 dr-xr-xr-x.   4 root   root   4096 Jul 16  2018 boot
    1025 drwxr-xr-x.  20 root   root   3180 May 27 11:29 dev
16777281 drwxr-xr-x.  78 root   root   8192 May 27 11:29 etc
50331740 drwxr-xr-x.   3 root   root     20 Jul 16  2018 home
      98 lrwxrwxrwx.   1 root   root      7 Jul 16  2018 lib -> usr/lib
      82 lrwxrwxrwx.   1 root   root      9 Jul 16  2018 lib64 -> usr/lib64
      83 drwxr-xr-x.   2 root   root      6 Nov  5  2016 media
16777649 drwxr-xr-x.   2 root   root      6 Nov  5  2016 mnt
33575378 drwxr-xr-x.   2 root   root     45 Jan 26 16:48 opt
       1 dr-xr-xr-x. 154 root   root      0 May 27 11:29 proc
33574977 dr-xr-x---.   7 root   root    213 Jan 26 17:15 root
    9241 drwxr-xr-x.  21 root   root    600 May 27 11:30 run
      99 lrwxrwxrwx.   1 root   root      8 Jul 16  2018 sbin -> usr/sbin
33575338 drwxr-xr-x.  10 centos centos 4096 Oct  9  2018 soft
50331741 drwxr-xr-x.   2 root   root      6 Nov  5  2016 srv
       1 dr-xr-xr-x.  13 root   root      0 May 27 11:29 sys
16777288 drwxrwxrwt.   7 root   root     93 May 27 12:48 tmp
33575353 drwxr-xr-x.  13 root   root    155 Jul 16  2018 usr
50331713 drwxr-xr-x.  19 root   root    267 May 27 11:29 var

 

posted @ 2019-05-27 11:35  wqbin  阅读(26012)  评论(0编辑  收藏  举报