1. 程式人生 > >linux命令詳解之ls命令

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可以為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引數的區別,結果裡面包括表示當前目錄.

和上級目錄..這兩個檔案)。

[[email protected] ~]# ls -a    #列出所有檔案
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc   install.log  install.log.syslog
[[email protected] ~]# ls --all
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc   install.log  install.log.syslog

示例2:列出所有的檔案,但不包括表示當前目錄.和上級目錄..這兩個檔案。

[[email protected] ~]# ls -A
.bash_history  .bash_logout  .bash_profile  .bashrc  install.log  install.log.syslog  
[[email protected] ~]# ls --almost-all
.bash_history  .bash_logout  .bash_profile  .bashrc  install.log  install.log.syslog

示例3:顯示列表並且以ctime排序

[[email protected] /]# ls -clt   #和 -lt引數一起使用,以時間排序
total 146
drwxrwxrwt   7 root root  4096 Apr 13 06:23 tmp
drwxr-xr-x  12 root root  4440 Apr 13 05:18 dev
drwxr-xr-x  93 root root 12288 Apr 13 05:18 etc
drwxr-xr-x   2 root root     0 Apr 13 05:18 net
drwxr-xr-x   2 root root     0 Apr 13 05:18 misc
drwxr-xr-x   4 root root     0 Apr 13 05:16 selinux
drwxr-xr-x  11 root root     0 Apr 13 05:16 sys
dr-xr-xr-x 150 root root     0 Apr 13 05:16 proc
drwxr-xr-x   2 root root 12288 Mar 28 06:34 sbin
drwxr-xr-x   2 root root  4096 Mar 28 06:33 bin
drwxr-xr-x   8 root root  4096 Mar 28 06:33 lib64
drwxr-x---   4 root root  4096 Nov 19 06:52 root
drwxr-xr-x   4 root root  4096 Nov 18 22:05 home
drwxr-xr-x  11 root root  4096 Nov 14 06:13 lib
drwxr-xr-x   3 root root  4096 Oct 17  2015 mnt
drwxr-xr-x  22 root root  4096 Oct 17  2015 var
drwxr-xr-x   4 root root  1024 Oct 17  2015 boot
drwxr-xr-x  15 root root  4096 Oct 17  2015 usr
drwxr-xr-x   2 root root  4096 Oct 17  2015 media
drwxr-xr-x   2 root root  4096 Oct 17  2015 srv
drwx------   2 root root 16384 Oct 17  2015 lost+found
drwxr-xr-x   3 root root  4096 Oct 17  2015 opt
[[email protected] /]# ls -cl   #和-l引數一起使用,以檔名排序並顯示時間
total 146
drwxr-xr-x   2 root root  4096 Mar 28 06:33 bin
drwxr-xr-x   4 root root  1024 Oct 17  2015 boot
drwxr-xr-x  12 root root  4440 Apr 13 05:18 dev
drwxr-xr-x  93 root root 12288 Apr 13 05:18 etc
drwxr-xr-x   4 root root  4096 Nov 18 22:05 home
drwxr-xr-x  11 root root  4096 Nov 14 06:13 lib
drwxr-xr-x   8 root root  4096 Mar 28 06:33 lib64
drwx------   2 root root 16384 Oct 17  2015 lost+found
drwxr-xr-x   2 root root  4096 Oct 17  2015 media
drwxr-xr-x   2 root root     0 Apr 13 05:18 misc
drwxr-xr-x   3 root root  4096 Oct 17  2015 mnt
drwxr-xr-x   2 root root     0 Apr 13 05:18 net
drwxr-xr-x   3 root root  4096 Oct 17  2015 opt
dr-xr-xr-x 150 root root     0 Apr 13 05:16 proc
drwxr-x---   4 root root  4096 Nov 19 06:52 root
drwxr-xr-x   2 root root 12288 Mar 28 06:34 sbin
drwxr-xr-x   4 root root     0 Apr 13 05:16 selinux
drwxr-xr-x   2 root root  4096 Oct 17  2015 srv
drwxr-xr-x  11 root root     0 Apr 13 05:16 sys
drwxrwxrwt   7 root root  4096 Apr 13 06:23 tmp
drwxr-xr-x  15 root root  4096 Oct 17  2015 usr
drwxr-xr-x  22 root root  4096 Oct 17  2015 var
[[email protected] /]# ls -c   #單獨使用,以時間排序,但不顯示時間
tmp  dev  etc  net  misc  selinux  sys  proc  sbin  bin  lib64  root  home  lib  mnt  var  boot  usr  media  srv  lost+found  opt

示例4:僅僅列出目錄本身,不需要列出目錄裡的內容

[root@localhost /]# ls -d /home   #僅列出/home目錄本身
/home
[root@localhost /]# ls /home   #列出/home目錄裡的內容
sgl  software

# 加上-l引數,比較的更清楚一些:
[root@localhost /]# ls -ld /home
drwxr-xr-x 4 root root 4096 Nov 18 22:05 /home
[root@localhost /]# ls -l /home
total 16
drwx------ 16 sgl  sgl  4096 Oct 17  2015 sgl
drwxr-xr-x  3 root root 4096 Nov 14 05:13 software

示例5:顯示完整時間

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

示例6:以易讀方式顯示列表

[[email protected] ~]# ls -lh /   #注意列表容量大小列的單位
total 146K
drwxr-xr-x   2 root root 4.0K Mar 28 06:33 bin
drwxr-xr-x   4 root root 1.0K Oct 17  2015 boot
drwxr-xr-x  12 root root 4.4K Apr 14 21:22 dev
drwxr-xr-x  93 root root  12K Apr 14 21:22 etc
drwxr-xr-x   4 root root 4.0K Nov 18 22:05 home
drwxr-xr-x  11 root root 4.0K Nov 14 06:13 lib
drwxr-xr-x   8 root root 4.0K Mar 28 06:33 lib64
drwx------   2 root root  16K Oct 17  2015 lost+found
drwxr-xr-x   2 root root 4.0K May 11  2011 media
drwxr-xr-x   2 root root    0 Apr 14 21:22 misc
drwxr-xr-x   3 root root 4.0K Oct 17  2015 mnt
drwxr-xr-x   2 root root    0 Apr 14 21:22 net
drwxr-xr-x   3 root root 4.0K Oct 17  2015 opt
dr-xr-xr-x 147 root root    0 Apr 14 21:21 proc
drwxr-x---   4 root root 4.0K Nov 19 06:52 root
drwxr-xr-x   2 root root  12K Mar 28 06:34 sbin
drwxr-xr-x   4 root root    0 Apr 14 21:21 selinux
drwxr-xr-x   2 root root 4.0K May 11  2011 srv
drwxr-xr-x  11 root root    0 Apr 14 21:21 sys
drwxrwxrwt   7 root root 4.0K Apr 14 21:24 tmp
drwxr-xr-x  15 root root 4.0K Oct 17  2015 usr
drwxr-xr-x  22 root root 4.0K Oct 17  2015 var

[[email protected] ~]# ls -l /   #預設方式,以位元組為單位顯示
total 146
drwxr-xr-x   2 root root  4096 Mar 28 06:33 bin
drwxr-xr-x   4 root root  1024 Oct 17  2015 boot
drwxr-xr-x  12 root root  4440 Apr 14 21:22 dev
drwxr-xr-x  93 root root 12288 Apr 14 21:22 etc
drwxr-xr-x   4 root root  4096 Nov 18 22:05 home
drwxr-xr-x  11 root root  4096 Nov 14 06:13 lib
drwxr-xr-x   8 root root  4096 Mar 28 06:33 lib64
drwx------   2 root root 16384 Oct 17  2015 lost+found
drwxr-xr-x   2 root root  4096 May 11  2011 media
drwxr-xr-x   2 root root     0 Apr 14 21:22 misc
drwxr-xr-x   3 root root  4096 Oct 17  2015 mnt
drwxr-xr-x   2 root root     0 Apr 14 21:22 net
drwxr-xr-x   3 root root  4096 Oct 17  2015 opt
dr-xr-xr-x 147 root root     0 Apr 14 21:21 proc
drwxr-x---   4 root root  4096 Nov 19 06:52 root
drwxr-xr-x   2 root root 12288 Mar 28 06:34 sbin
drwxr-xr-x   4 root root     0 Apr 14 21:21 selinux
drwxr-xr-x   2 root root  4096 May 11  2011 srv
drwxr-xr-x  11 root root     0 Apr 14 21:21 sys
drwxrwxrwt   7 root root  4096 Apr 14 21:24 tmp
drwxr-xr-x  15 root root  4096 Oct 17  2015 usr
drwxr-xr-x  22 root root  4096 Oct 17  2015 var

示例7:顯示inode

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

示例8:列出資料夾內容,並顯示出檔案所屬使用者和組的id

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

示例9:以檔案大小排序(升序和降序)

[[email protected] ~]# ls -lS /    #預設降序排序
total 146
drwx------   2 root root 16384 Oct 17  2015 lost+found
drwxr-xr-x  93 root root 12288 Apr 14 21:22 etc
drwxr-xr-x   2 root root 12288 Mar 28 06:34 sbin
drwxr-xr-x  12 root root  4440 Apr 14 21:22 dev
drwxr-xr-x   2 root root  4096 Mar 28 06:33 bin
drwxr-xr-x   4 root root  4096 Nov 18 22:05 home
drwxr-xr-x  11 root root  4096 Nov 14 06:13 lib
drwxr-xr-x   8 root root  4096 Mar 28 06:33 lib64
drwxr-xr-x   2 root root  4096 May 11  2011 media
drwxr-xr-x   3 root root  4096 Oct 17  2015 mnt
drwxr-xr-x   3 root root  4096 Oct 17  2015 opt
drwxr-x---   4 root root  4096 Nov 19 06:52 root
drwxr-xr-x   2 root root  4096 May 11  2011 srv
drwxrwxrwt   7 root root  4096 Apr 14 21:24 tmp
drwxr-xr-x  15 root root  4096 Oct 17  2015 usr
drwxr-xr-x  22 root root  4096 Oct 17  2015 var
drwxr-xr-x   4 root root  1024 Oct 17  2015 boot
drwxr-xr-x   2 root root     0 Apr 14 21:22 misc
drwxr-xr-x   2 root root     0 Apr 14 21:22 net
dr-xr-xr-x 147 root root     0 Apr 14 21:21 proc
drwxr-xr-x   4 root root     0 Apr 14 21:21 selinux
drwxr-xr-x  11 root root     0 Apr 14 21:21 sys
[[email protected] ~]# ls -lSr /  #通過-r引數實現升序排列
total 146
drwxr-xr-x  11 root root     0 Apr 14 21:21 sys
drwxr-xr-x   4 root root     0 Apr 14 21:21 selinux
dr-xr-xr-x 147 root root     0 Apr 14 21:21 proc
drwxr-xr-x   2 root root     0 Apr 14 21:22 net
drwxr-xr-x   2 root root     0 Apr 14 21:22 misc
drwxr-xr-x   4 root root  1024 Oct 17  2015 boot
drwxr-xr-x  22 root root  4096 Oct 17  2015 var
drwxr-xr-x  15 root root  4096 Oct 17  2015 usr
drwxrwxrwt   7 root root  4096 Apr 14 21:24 tmp
drwxr-xr-x   2 root root  4096 May 11  2011 srv
drwxr-x---   4 root root  4096 Nov 19 06:52 root
drwxr-xr-x   3 root root  4096 Oct 17  2015 opt
drwxr-xr-x   3 root root  4096 Oct 17  2015 mnt
drwxr-xr-x   2 root root  4096 May 11  2011 media
drwxr-xr-x   8 root root  4096 Mar 28 06:33 lib64
drwxr-xr-x  11 root root  4096 Nov 14 06:13 lib
drwxr-xr-x   4 root root  4096 Nov 18 22:05 home
drwxr-xr-x   2 root root  4096 Mar 28 06:33 bin
drwxr-xr-x  12 root root  4440 Apr 14 21:22 dev
drwxr-xr-x   2 root root 12288 Mar 28 06:34 sbin
drwxr-xr-x  93 root root 12288 Apr 14 21:22 etc
drwx------   2 root root 16384 Oct 17  2015 lost+found





注:本系列內容主要參考《鳥哥的linux私房菜》和CentOS系統自帶的幫助文件以及網上相關資料,示例都是基於CentOS。