返回基礎命令目錄頁
ls命令用來顯示目標列表,在Linux中是使用率較高的命令。ls命令的輸出信息能夠進行彩色加亮顯示,以分區不一樣類型的文件。
語法
ls(選項)(參數)
經常使用組合
[1]查看文件詳情:ls -l 或 ll
[2]加強對文件大小易讀性,以人類可讀的形式顯示文件大小: ls -lh
[3]對文件或者目錄進行從大到小的排序: ls -lhs
[4]查看當前目錄下的全部文件或者目錄,包括隱藏文件: ls -la
[5]只查看當前目錄下的目錄文件: ls -d .
[6]按照時間順序查看,從上到倒下時間愈來愈近: ls -ltr
[7]查看文件在對應的inode信息:ls -lihtml
實例
-a:顯示全部檔案及目錄(ls內定將檔案名或目錄名稱爲「.」的視爲影藏,不會列出);
[root@lb02 ~]# ls -a . anaconda-ks.cfg .bash_logout .bashrc .cshrc .ssh .viminfo .. .bash_history .bash_profile bootime.svg .pki .tcshrc
-A:顯示除影藏文件「.」和「..」之外的全部文件列表;
[root@lb02 ~]# ls -A anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc bootime.svg .cshrc .pki .ssh .tcshrc .viminfo
-C:多列顯示輸出結果。這是默認選項; -l:與「-C」選項功能相反,全部輸出信息用單列格式輸出,不輸出爲多列;
[root@lb02 ~]# ls -1 anaconda-ks.cfg bootime.svg
-F:在每一個輸出項後追加文件的類型標識符,具體含義:「*」表示具備可執行權限的普通文件,「/」表示目錄,「@」表示符號連接,「|」表示命令管道FIFO,「=」表示sockets套接字。當文件爲普通文件時,不輸出任何標識符;
[root@lb02 ~]# ll total 8 -rwxr-xr-x 1 root root 9 Feb 21 06:43 1.sh -rw-------. 1 root root 1627 Nov 12 22:26 anaconda-ks.cfg drwxr-xr-x 2 root root 6 Feb 21 06:44 mydir [root@lb02 ~]# ls -F 1.sh* anaconda-ks.cfg mydir/
-b:將文件中的不可輸出的字符以反斜線「」加字符編碼的方式輸出;
-c:與「-lt」選項連用時,按照文件狀態時間排序輸出目錄內容,排序的依據是文件的索引節點中的ctime字段。與「-l」選項連用時,則排序的一句是文件的狀態改變時間;
[root@lb02 ~]# stat 1.sh mydir/ anaconda-ks.cfg File: ‘1.sh’ Change: 2018-02-21 06:44:09.172203419 -0500 File: ‘mydir/’ Change: 2018-02-21 06:44:46.476170254 -0500 File: ‘anaconda-ks.cfg’ Change: 2017-11-12 22:26:50.173018643 -0500 [root@lb02 ~]# ls -c mydir 1.sh anaconda-ks.cfg #按照ctime排序
-d:僅顯示目錄名,而不顯示目錄下的內容列表。顯示符號連接文件自己,而不顯示其所指向的目錄列表;
[root@lb02 ~]# ls -ld . dr-xr-x---. 5 root root 200 Feb 21 06:58 .
-f:此參數的效果和同時指定「aU」參數相同,並關閉「lst」參數的效果;
[root@lb02 ~]# ls -f . .bash_logout .bashrc .tcshrc .bash_history .pki .viminfo .. .bash_profile .cshrc anaconda-ks.cfg .ssh 1.sh mydir [root@lb02 ~]# ls -aU . .bash_logout .bashrc .tcshrc .bash_history .pki .viminfo .. .bash_profile .cshrc anaconda-ks.cfg .ssh 1.sh mydir [root@lb02 ~]# ls -lst total 8 0 drwxr-xr-x 2 root root 6 Feb 21 06:44 mydir 4 -rwxr-xr-x 1 root root 9 Feb 21 06:43 1.sh 4 -rw-------. 1 root root 1627 Nov 12 22:26 anaconda-ks.cfg
-i:顯示文件索引節點號(inode)。一個索引節點表明一個文件;
[root@lb02 ~]# ls -i * 134435243 1.sh 134318146 anaconda-ks.cfg mydir:
--file-type:與「-F」選項的功能相同,可是不顯示「*」;
[root@lb02 ~]# ls --file-type 1.sh anaconda-ks.cfg mydir/
-k:以KB(千字節)爲單位顯示文件大小; -l:以長格式顯示目錄下的內容列表。輸出的信息從左到右依次包括文件名,文件類型、權限模式、硬鏈接數、全部者、組、文件大小和文件的最後修改時間等;
[root@lb02 ~]# ls -l total 8 -rwxr-xr-x 1 root root 9 Feb 21 06:43 1.sh
-m:用「,」號區隔每一個文件和目錄的名稱;
[root@lb02 ~]# ls -m 1.sh, anaconda-ks.cfg, mydir
-n:以用戶識別碼和羣組識別碼替代其名稱;
[root@lb02 ~]# ls -n total 8 -rwxr-xr-x 1 0 0 9 Feb 21 06:43 1.sh ##uid 和gid顯示 -rw-------. 1 0 0 1627 Nov 12 22:26 anaconda-ks.cfg
-r:以文件名反序排列並輸出目錄內容列表;
[root@lb02 ~]# ls -lt total 8 drwxr-xr-x 2 root root 6 Feb 21 06:44 mydir -rwxr-xr-x 1 root root 9 Feb 21 06:43 1.sh -rw-------. 1 root root 1627 Nov 12 22:26 anaconda-ks.cfg [root@lb02 ~]# ls -ltr #-t默認按照修改時間Mtime,-r逆序 total 8 -rw-------. 1 root root 1627 Nov 12 22:26 anaconda-ks.cfg -rwxr-xr-x 1 root root 9 Feb 21 06:43 1.sh drwxr-xr-x 2 root root 6 Feb 21 06:44 mydir
-s:顯示文件和目錄的大小,以區塊爲單位;
[root@lb02 ~]# ls -s total 8 4 1.sh 4 anaconda-ks.cfg 0 mydir
-t:用文件和目錄的更改時間排序;
[root@lb02 ~]# ls -lt total 8 drwxr-xr-x 2 root root 6 Feb 21 06:44 mydir #默認顯示修改時間 -rwxr-xr-x 1 root root 9 Feb 21 06:43 1.sh -rw-------. 1 root root 1627 Nov 12 22:26 anaconda-ks.cfg
-L:若是遇到性質爲符號連接的文件或目錄,直接列出該連接所指向的原始文件或目錄; -R:遞歸處理,將指定目錄下的全部文件及子目錄一併處理;
[root@lb02 ~]# ls -R .: 1.sh anaconda-ks.cfg mydir ./mydir: childir myfile ./mydir/childir: my.txt
--full-time:列出完整的日期與時間;
[root@lb02 ~]# ls --full-time total 8 -rwxr-xr-x 1 root root 9 2018-02-21 06:43:41.248479699 -0500 1.sh -rw-------. 1 root root 1627 2017-11-12 22:26:50.173018643 -0500 anaconda-ks.cfg drwxr-xr-x 3 root root 35 2018-02-21 07:36:08.614123920 -0500 mydir
--color[=WHEN]:使用不一樣的顏色高亮顯示不一樣類型的。
[root@lb02 ~]# alias ls alias ls='ls --color=auto' #別名默認就有
參考:http://man.linuxde.net/lsnode