天天看點

Linux系統指令詳解之ls

一、ls為外部指令

0> type -a ls 
ls is aliased to `ls --color=auto'
ls is /bin/ls
           

二、常用使用方法

0> ls -l        #以詳細清單方式顯示目錄與檔案的狀态資訊
總用量 556      #顯示出目錄與檔案的總大小,機關預設為K
-rw-------. 1 root root  15187 11月 27 15:16 anaconda-ks.cfg
-rwxr--r--  1 root root    886 11月 27 15:51 cfagent_ck.sh
-rwxr-xr-x  1 root root   1430 7月  23 2013 client.sh
-rw-r--r--. 1 root root  45749 11月 27 15:16 install.log
-rw-r--r--. 1 root root  10037 11月 27 15:08 install.log.syslog
-rw-r--r--  1 root root 476103 4月  16 10:29 test

0> ls -lh /var/log/messages*       #以詳細清單方式與高可讀性的位元組機關(KB、K、MB、M、G、T、P、E、Z、Y)顯示目錄與檔案的狀态資訊
-rw------- 1 root root 416K 5月  27 18:05 /var/log/messages
-rw------- 1 root root 1.1M 5月   4 03:15 /var/log/messages-20140504
-rw------- 1 root root 1.2M 5月  11 03:05 /var/log/messages-20140511
-rw------- 1 root root 1.1M 5月  18 03:20 /var/log/messages-20140518
-rw------- 1 root root 1.1M 5月  25 03:19 /var/log/messages-20140525

0> ls -alh /root/    #顯示隐藏的目錄與檔案
總用量 772K
dr-xr-x---.  2 root root 4.0K 5月  27 18:14 .
dr-xr-xr-x. 27 root root 4.0K 11月 27 18:46 ..
-rw-------.  1 root root  15K 11月 27 15:16 anaconda-ks.cfg
-rw-------   1 root root 162K 5月  27 15:41 .bash_history
-rw-r--r--.  1 root root   18 5月  20 2009 .bash_logout
-rw-r--r--.  1 root root  176 5月  20 2009 .bash_profile

0> ls -alhS /var/log/messages*      #以位元組從大到小進行排序顯示
-rw------- 1 root root 1.2M 5月  11 03:05 /var/log/messages-20140511
-rw------- 1 root root 1.1M 5月  18 03:20 /var/log/messages-20140518
-rw------- 1 root root 1.1M 5月  25 03:19 /var/log/messages-20140525
-rw------- 1 root root 1.1M 5月   4 03:15 /var/log/messages-20140504
-rw------- 1 root root 417K 5月  27 18:15 /var/log/messages

0> ls -alh --full-time /var/log/messages*    #顯示目錄與檔案的詳細修改日期
-rw------- 1 root root 418K 2014-05-27 18:20:09.000000000 +0800 /var/log/messages
-rw------- 1 root root 1.1M 2014-05-04 03:15:27.000000000 +0800 /var/log/messages-20140504
-rw------- 1 root root 1.2M 2014-05-11 03:05:27.000000000 +0800 /var/log/messages-20140511
-rw------- 1 root root 1.1M 2014-05-18 03:20:04.000000000 +0800 /var/log/messages-20140518
-rw------- 1 root root 1.1M 2014-05-25 03:19:48.000000000 +0800 /var/log/messages-20140525

0> ls -alh --full-time --sort=time /var/log/messages*     #以目錄與檔案的修改日期從近到遠進行排序顯示
-rw------- 1 root root 418K 2014-05-27 18:24:30.000000000 +0800 /var/log/messages
-rw------- 1 root root 1.1M 2014-05-25 03:19:48.000000000 +0800 /var/log/messages-20140525
-rw------- 1 root root 1.1M 2014-05-18 03:20:04.000000000 +0800 /var/log/messages-20140518
-rw------- 1 root root 1.2M 2014-05-11 03:05:27.000000000 +0800 /var/log/messages-20140511
-rw------- 1 root root 1.1M 2014-05-04 03:15:27.000000000 +0800 /var/log/messages-20140504
           

三、指令參數全解

0> ls --help
用法:ls [選項]... [檔案]...
列出 FILE 的資訊(預設為目前目錄)。
如果不指定-cftuvSUX 或--sort 選項,則根據字母大小排序。

長選項必須使用的參數對于短選項時也是必需使用的。
  -a, --all                     不隐藏任何以. 開始的項目
  -A, --almost-all              列出除. 及.. 以外的任何項目
      --author                  與-l 同時使用時列出每個檔案的作者
  -b, --escape                  以八進制溢出序清單示不可列印的字元
      --block-size=大小         塊以指定大小的位元組為機關
  -B, --ignore-backups          不列出任何以"~"字元結束的項目
  -c                            配合-lt:根據ctime 排序并顯示ctime(檔案
                                狀态最後更改的時間)
                                配合-l:顯示ctime 但根據名稱排序                                其他情況:按ctime 排序
  -C                            每欄由上至下列出項目
      --color[=WHEN]            控制是否使用色彩分辨檔案。WHEN 可以是
                                "never"(預設)、"always"或"auto"其中之一
  -d, --directory               當遇到目錄時列出目錄本身而非目錄内的檔案            <span style="color:#FF0000;">#此參數在隻檢視目錄資訊時很有效</span>
  -D, --dired                   産生适合Emacs 的dired 模式使用的結果
  -f                            不進行排序,-aU 選項生效,-lst 選項失效
  -F, --classify                加上檔案類型的訓示符号(*/[email protected]| 其中一個)
      --format=關鍵字           交錯-x,逗号分隔-m,水準-x,長-l,
                                單欄-1,詳細-l,垂直-C
      --full-time               即-l --time-style=full-iso
  -g                            類似-l,但不列出所有者
      --group-directories-first
                        在檔案前分組目錄。此選項可與--sort 一起使用,
                        但是一旦使用--sort=none (-U)将禁用分組
  -G, --no-group                以一個長清單的形式,不輸出組名
  -h, --human-readable          與-l 一起,以易于閱讀的格式輸出檔案大小
                                (例如 1K 234M 2G)
      --si                      同上面類似,但是使用1000 為基底而非1024
  -H, --dereference-command-line
                                跟随指令行列出的符号連結
      --dereference-command-line-symlink-to-dir
                                跟随指令行列出的目錄的符号連結
      --hide=PATTERN            隐藏符合PATTERN 模式的項目
                                (-a 或 -A 将覆寫此選項)
      --indicator-style=方式    指定在每個項目名稱後加上訓示符号方式:
                                none (預設),classify (-F),file-type (-p)
  -i, --inode                   顯示每個檔案的inode 号
  -I, --ignore=PATTERN          不顯示任何符合指定shell PATTERN 的項目
  -k                            即--block-size=1K
  -l                            使用較長格式列出資訊
  -L, --dereference             當顯示符号連結的檔案資訊時,顯示符号連結所訓示
                                的對象而并非符号連結本身的資訊
  -m                            所有項目以逗号分隔,并填滿整行行寬
  -n, --numeric-uid-gid         類似 -l,但列出UID 及GID 号
  -N, --literal                 輸出未經處理的項目名稱 (如不特别處理控制字元)
  -o                            類似 -l,但不列出有關組的資訊
  -p,  --indicator-style=slash  對目錄加上表示符号"/"
  -q, --hide-control-chars      以"?"字元代替無法列印的字元
      --show-control-chars      直接顯示無法列印的字元 (這是預設方式,除非調用
                                的程式名稱是"ls"而且是在終端輸出結果)
  -Q, --quote-name              将條目名稱括上雙引号
      --quoting-style=方式      使用指定的quoting 方式顯示條目的名稱:
                                literal、locale、shell、shell-always、c、escape
  -r, --reverse                 排序時保留順序
  -R, --recursive               遞歸顯示子目錄
  -s, --size                    以塊數形式顯示每個檔案配置設定的尺寸
  -S                            根據檔案大小排序
      --sort=WORD               以下是可選用的WORD 和它們代表的相應選項:
                                extension -X       status   -c
                                none      -U       time     -t
                                size      -S       atime    -u
                                time      -t       access   -u
                                version   -v       use      -u
      --time=WORD               和-l 同時使用時顯示WORD 所代表的時間而非修改時
                                間:atime、access、use、ctime 或status;加上
                                --sort=time 選項時會以指定時間作為排序關鍵字
      --time-style=STYLE        和-l 同時使用時根據STYLE 代表的格式顯示時間:
                                full-iso、iso、locale、posix-iso、+FORMAT。
                                FORMAT 即是"date"所用的時間格式;如果FORMAT
                                是FORMAT1<換行>FORMAT2,FORMAT1 适用于較舊
                                的檔案而FORMAT2 适用于較新的檔案;如果STYLE
                                以"posix-"開頭,則STYLE 僅在POSIX 語系之外
                                生效。
  -t                            根據修改時間排序
  -T, --tabsize=寬度    指定制表符(Tab)的寬度,而非8 個字元
  -t                    按修改時間排序
  -T, --tabsize=COLS    指定制表符(Tab)的寬度,而非8個字元
  -u                    同-lt 一起使用:按照通路時間排序并顯示
                        同-l一起使用:顯示通路時間并按檔案名排序
                        其他:按照通路時間排序
  -U                    不進行排序;按照目錄順序列出項目
  -v                    在文本中進行數字(版本)的自然排序
  -w, --width=COLS           assume screen width instead of current value
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -1                         list one file per line

SELinux options:

  --lcontext                 Display security context.   Enable -l. Lines
                             will probably be too wide for most displays.
  -Z, --context              Display security context so it fits on most
                             displays.  Displays only mode, user, group,
                             security context and file name.
  --scontext                 Display only security context and file name.
      --help            顯示此幫助資訊并退出
      --version         顯示版本資訊并退出

SIZE 可以是一個可選的整數,後面跟着以下機關中的一個:
KB 1000,K 1024,MB 1000*1000,M 1024*1024,還有 G、T、P、E、Z、Y。

使用色彩來區分檔案類型的功能已被禁用,預設設定和 --color=never 同時禁用了它。
使用 --color=auto 選項,ls 隻在标準輸出被連至終端時才生成顔色代碼。
LS_COLORS 環境變量可改變此設定,可使用 dircolors 指令來設定。


退出狀态:
 0  正常
 1  一般問題 (例如:無法通路子檔案夾)
 2  嚴重問題 (例如:無法使用指令行參數)
           

繼續閱讀