在Linux運維的日常工作中ls是最常用的命令之一,ls有許多的選項,並且在bash腳本中也會經常看到它的身影,詳細學習ls的用法是每一個運維都應該做的事。
ls和windows的dir命令類似,可以列出指定目錄裏的內容
默認情況下,ls命令僅列出當前目錄的文件和目錄就好像使用了.這個參數,並不會遞歸地列出子目錄的內容,且不會列出以.開頭的隱藏內容,默認以字母順序排列,且僅僅列出條目名。
ls的三種退出狀態:
0:命令執行成功
1:輕微的錯誤,通常是指定的對象已經被刪除或重命名而導致找不到
2:嚴重錯誤,如:內存不足,無效的選項等
注意:如果使用一個非POSIX本地環境,如,設置‘LC_ALL’爲en_US,ls的輸出排序可能和我們所習慣的不同,這種情況請去設置‘LC_ALL’的值爲'C'。
ls是Linux裏的一個外部命令,我們使用的ls其實是一個別名
[root@localhost ~]# type ls ls is aliased to `ls --color=auto' [root@localhost ~]# unalias ls [root@localhost ~]# type ls ls is hashed (/usr/bin/ls)
ls的語法:
ls [OPTION]... [FILE]...
ls後面可以接多個選項和參數,默認參數是當前目錄,如果沒有-cftuvSUX選項並且--sort也沒有指定排序的話,默認按字母排序
[root@localhost testdir]# ls [root@localhost testdir]# touch a aa b AA C c D e z ZZ l LL [root@localhost testdir]# ls a aa AA b c C D e l LL z ZZ
注意:Linux裏字母的順序是aAbBcC...,且單個字母比多個字母的順序靠前,當前面的字母一樣時,按照接下來的字母順序排列,數字排在字母前面,和字母的排列規則相似。
[root@localhost testdir]# touch 1 22 2 21 22 9 91 [root@localhost testdir]# ls 1 2 21 22 9 91 a aa AA b c C D e l LL z ZZ
選項:
-a --all:列出所有文件,包括以.開頭的隱藏文件
-A --almost-all:與-a類似但不會列出. 和..,-a選項會覆蓋此選項
[root@localhost ~]# ls 1lksfk Documents f3.txt Pictures Videos anaconda-ks.cfg Downloads initial-setup-ks.cfg Public Desktop f2.txt Music Templates [root@localhost ~]# ls -a . .bash_profile Desktop .ICEauthority Public .. .bashrc Documents initial-setup-ks.cfg .tcshrc 1lksfk .cache Downloads .lesshst Templates anaconda-ks.cfg .config .esd_auth .local Videos .bash_history .cshrc f2.txt Music .viminfo .bash_logout .dbus f3.txt Pictures .Xauthority [root@localhost ~]# ls -A 1lksfk .bashrc Desktop f3.txt Music Videos anaconda-ks.cfg .cache Documents .ICEauthority Pictures .viminfo .bash_history .config Downloads initial-setup-ks.cfg Public .Xauthority .bash_logout .cshrc .esd_auth .lesshst .tcshrc .bash_profile .dbus f2.txt .local Templates
--author:和-l一起使用,列出文件的創始人
[root@localhost ~]# ls -l --author total 16 -rw-r--r--. 1 root root root 0 Jul 27 15:38 1lksfk -rw-------. 1 root root root 1490 Jul 27 02:03 anaconda-ks.cfg drwxr-xr-x. 2 root root root 6 Jul 28 08:06 Desktop drwxr-xr-x. 2 root root root 6 Jul 26 17:34 Documents
-b --escape:打印C風格的非圖形轉義字符,不是很常用
--block-size=SIZE:和-l結合使用,按指定的單位顯示文件大小,KB=1000bit,K=1024bit.........
通常使用-h --human-readable 按文件大小自動轉化爲相應的單位
[root@localhost ~]# ls -l total 16-rw-r--r--. 1 root root 0 Jul 27 15:38 1lksfk -rw-------. 1 root root 1490 Jul 27 02:03 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 Jul 28 08:06 Desktop
...........
[root@localhost ~]# ls -l --block-size=K total 16K-rw-r--r--. 1 root root 0K Jul 27 15:38 1lksfk -rw-------. 1 root root 2K Jul 27 02:03 anaconda-ks.cfg drwxr-xr-x. 2 root root 1K Jul 28 08:06 Desktop
.........
[root@localhost ~]# ls -l -h total 16K -rw-r--r--. 1 root root 0 Jul 27 15:38 1lksfk -rw-------. 1 root root 1.5K Jul 27 02:03 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 Jul 28 08:06 Desktop
-B --ignore-backups :不列出指定的以~結尾的文件,等效於--ignore='*~' --ignore='.*~'
[root@localhost testdir]# ls nieda nieda~ [root@localhost testdir]# ls -B nieda
-d --directory:只列出目錄名字,而不會列出目錄的內容,並且目錄名後不會列出軟連接的源路徑,除非加上-H,-L或者指定了--dereference-command-line-symlink-to-dir選項。
-H --dereference-command-line:使用命令列中的符號鏈接指示的真正目的地
--dereference-command-line-symlink-to-dir:不要引用符號鏈接,除非命令行參數是一個目錄的軟連接,將顯示目錄信息而不是軟連接信息。
--si :和-h類似,但是其單位是1000而不是1024
--hide=PATTERN :根據指定的shell模式隱藏條目
[root@localhost ~]# ls 1lksfk Desktop Downloads f3.txt Music Public Videos anaconda-ks.cfg Documents f2.txt initial-setup-ks.cfg Pictures Templates [root@localhost ~]# ls --hide=*.txt 1lksfk anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
--indicator-style=WORLD :給條目名添加一個指標方式,WORLD有:none(默認),slash(-p)給目錄加一個/的標識,classify(-F)用*/=>@|中的其中一個給條目增加標識,file-type(--file-type)和-F一樣除了*以外給條目加標識
其中:/ 表示目錄;@表示軟連接;*表示可執行文件;=表示sockets文件