【國產化】Linux 命令速學之 ls 命令

2024年2月6日 17点热度 0人点赞

#國產操作系統#

1. ls 命令簡介

ls,用於查看某個路徑中的文件和目錄信息,包括類型,權限,owner,大小,最後修改時間等,是最常用的命令之一。KylinUOS CentOS Linux 通用。

2. 語法

ls [options] [file|directory]

默認情況下,ls 列出當前路徑的內容,按文件和目錄名字母排序。排序相關的參數:

  • t : 按最後修改時間排序,Sort files and directories by their last modification time, displaying the most recently modified ones first.
  • S : 按大小排序,Sort files and directories by their sizes, listing the largest ones first.
  • r : 反向排序,known as reverse order which is used to reverse the default order of listing.

詳細的參數和用法如下:

用法:ls [選項] [文件]
列出給定文件(默認為當前目錄)的信息。
如果不指定 -cftuvSUX 中任意一個或--sort 選項,則根據字母排序。
必選參數對長短選項同時適用。
  -a, --all                  列出所有文件和目錄,包括 . 和 ..
  -A, --almost-all           列出除 . 及 .. 以外的所有文件和目錄
      --author               與 -l 同時使用時,列出每個文件的作者
  -b, --escape               以 C 風格的轉義序列表示不可打印的字符
      --block-size=SIZE      與 -l 同時使用時,將文件以給定的SIZE為單位顯示
  -B, --ignore-backups       不列出任何以 ~ 字符結束的項目
  -c                         與 -lt 共同使用時:根據 ctime 排序並顯示 ctime(最後更改時間);
                             與 -l 共同使用時:顯示 ctime 並按照名稱排序;
                             其它情況:按照 ctime 排序,最新的排最前
  -C                         分欄由上至下列出項目
      --color[=WHEN]         是否帶顏色輸出;WHEN 可以是 always(默認)、auto 或 never;
  -d, --directory            當遇到目錄時列出目錄本身而非目錄內的文件
  -D, --dired                產生適合 Emacs 的 dired 模式使用的結果
  -f                         不進行排序,-aU 選項生效,禁用 -ls --color
  -F, --classify             加上文件類型的指示符號(*/=@| 其中一個)
      --file-type            類似,但不包含‘*’後綴的情況
      --format=word          交錯 -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 和 -s 一起,以易於閱讀的格式輸出文件大小(如 1K 23M 2G等)
      --si                   同上面類似,但是使用 1000 為基底而非 1024
  -H, --dereference-command-line
                             跟隨命令行列出的符號鏈接
      --dereference-command-line-symlink-to-dir
                             跟隨命令行列出的指向目錄的符號鏈接
      --hide=PATTERN         隱藏符合 shell PATTERN 模式的項目(-a 或 -A 將覆蓋此選項)
      --hyperlink[=WHEN]     文件名使用超鏈接;WHEN 可以是 always(默認)、auto 或 never
      --indicator-style=方式  指定在每個項目名稱後加上指示符號方式:none(默認)、slash(-p)、
                               file-type(--file-type)以及 classify(-F)
  -i, --inode                顯示每個文件的索引編號(inode 號)
  -I, --ignore=模式          不顯示任何匹配指定 shell <模式>的項目
  -k, --kibibytes            默認使用 1024 字節的塊顯示磁盤使用量;隻用於 -s 和每個目錄的總計
  -l				    使用較長格式列出信息
  -L, --dereference		當顯示符號鏈接的文件信息時,顯示符號鏈接所指示的對象而非符號鏈接本身的信息
  -m				    所有項目以逗號分隔,並填滿整行行寬
  -n, --numeric-uid-gid      類似 -l,但列出用戶和組的 ID 號
  -N, --literal              輸出未經引號括起的項目名稱
  -o                         類似 -l,但不列出有關組的信息
  -p, --indicator-style=slash
                             對目錄附加“/”作為指示符號
  -q, --hide-control-chars   以“?”字符代替無法打印的字符
      --show-control-chars   原樣顯示無法打印的字符(這是默認行為,除非被調用
                               本程序的名稱是“ls”而且是在終端中進行輸出)
  -Q, --quote-name           將條目名稱括上雙引號
      --quoting-style=方式   使用指定引用的方式顯示條目的名稱:
                               literal, locale, shell, shell-always,
                               shell-escape, shell-escape-always, c, escape
                               (該選項優先於 QUOTING_STYLE 環境變量的配置)
  -r, --reverse			逆序排列
  -R, --recursive		遞歸顯示子目錄
  -s, --size			以塊數形式顯示每個文件分配的尺寸
  -S                         sort by file size, largest first
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                               time (-t), version (-v), extension (-X)
      --time=WORD            change the default of using modification times;
                               access time (-u): atime, access, use;
                               change time (-c): ctime, status;
                               birth time: birth, creation;
                             with -l, WORD determines which time to show;
                             with --sort=time, sort by WORD (newest first)
      --time-style=TIME_STYLE  使用 -l 時顯示的時間/日期格式;
  -t                         按時間排序,最新的排最前;參見 --time
  -T, --tabsize=COLS         指定制表符(Tab)的寬度,而非默認8字符
  -u                         同 -lt 一起使用:按照訪問時間排序並顯示;
                             同 -l 一起使用:顯示訪問時間並按文件名排序
                             其它:按照訪問時間排序,最新的最最前
  -U                         不進行排序;按照目錄順序列出項目
  -v                         在文本中進行數字(版本)的自然排序
  -w, --width=COLS           設置輸出寬度為 COLS 對應的值。0 代表無限制
  -x                         逐行列出項目而不是逐欄列出
  -X                         根據擴展名按字母順序排序
  -Z, --context              輸出每個文件的所有安全上下文信息
  -1                         每行隻列出一個文件。與 -q 或 -b 共用時避免 '\n'
      --help		顯示此幫助信息並退出
      --version		顯示版本信息並退出
退出狀態:
 0  正常
 1  一般問題 (例如:無法訪問子文件夾)
 2  嚴重問題 (例如:無法使用命令行參數)

3. 命令示例

3.1. ls -t, ls -lt 按最後修改時間排序

/etc 目錄中列出最近修改過的 3 個文件或目錄。

[root@kylin ~]# ls -t /etc | head -3
localtime_tmp
resolv.conf
audit
[root@kylin bin]# ls -ld /etc
drwxr-xr-x 104 root root 8192  2  1 21:08 /etc
[root@kylin bin]# ls -lt /etc
總用量 1128
-rw-------  1 root root        9  2  1 23:00 localtime_tmp
-rw-r--r--  1 root root       82  2  1 21:03 resolv.conf
drwxr-x---  4 root root       81  2  1 21:03 audit
...

ls -t

3.2. ls -1 一行顯示一個

註意不是字母 l,而是數字 1

[root@centos data]# ls -1
1.txt
2.md
demo1
demo2
My songs

ls -1

3.3. ls -l 長格式顯示文件/目錄信息

`ls -l` 簡寫為 `ll`。

[root@centos data]# ls -l
total 0
-rw-r--r--. 1 root root 10 Jan 30 21:46 1.txt
-rw-r--r--. 1 root root 20 Jan 30 21:46 2.md
drwxr-xr-x. 2 root root 44 Jan 31 16:39 demo1
drwxr-xr-x. 2 root root 40 Jan 30 21:57 demo2
drwxr-xr-x. 2 root root  6 Jan 31 21:52 My songs

ls -l

3.4. ls -lh 符合人類閱讀習慣方式顯示大小

h 表示 human readable,會以 K,M,G 為單位來顯示文件大小。

[root@kylin bin]# ls -lh
總用量 309M
-rwxr-xr-x 1 root root  2.7K  1  2  2019  anaconda-cleanup
-rwxr-xr-x 1 root root   102  6  8  2018  anaconda-disable-nm-ibft-plugin
-rwxr-xr-x 1 root root  439K 12  7 15:43  atril
-rwxr-xr-x 1 root root  1.2M 11  3  2022  bash
-rwxr-xr-x 1 root root  201K  7 28  2023  bccmd
-rwxr-xr-x 1 root root   65M 11 23 09:42  docker

3.5. ls -ld 顯示目錄信息

[root@kylin bin]# ls -ld /etc
drwxr-xr-x 104 root root 8192  2  1 21:08 /etc

3.6. ls -a, ls -A 顯示隱藏文件

[root@kylin ~]# ls -a

ls -a

3.7. ls -R 遞歸顯示各個目錄包含的文件和目錄

[root@centos /]# ls -R /data

ls -R

3.8. ls -i 顯示 file inode number

[root@centos /]# ls -i /data
[root@centos /]# ls -li /data

ls -i

3.9. ls -n 顯示 UID 和 GID

默認顯示的是 user namegroup name

[root@kylin /]# ls -n

ls -n

3.10. ls -F 用特定字符顯示不同類型的文件或目錄

[root@kylin /]# ls -F

ls -F

  • / 表示 directory.
  • @ 表示 link file.
  • * 表示 Executable file
  • nothing 表示 normal file.