【Linux】一步一步学Linux——ls命令(29)
00. 目錄
文章目錄
- 00. 目錄
- 01. 命令概述
- 02. 命令格式
- 03. 常用選項
- 04. 參考示例
- 05. 附錄
01. 命令概述
ls 命令是Linux下最常用的指令之一。ls命令為英文單詞 list 的縮寫,正如英文單詞 list 的意思,其功能是列出指定目錄下的內容及其相關屬性信息。通過 ls 命令,不僅可以查看 Linux 文件夾包含的文件,而且可以查看文件的權限(包括:目錄、文件權限)、查看目錄信息等。
02. 命令格式
ls [選項]… [文件]…
03. 常用選項
選項 說明 -a 列出目錄下所有文件,包括以 . 開頭的隱藏文件 -b 把文件名中不可輸出的字符用反斜杠加字符編號(就像 C 語言一樣)的形式列出 -c 輸出文件的 i 節點的修改時間,并以此排序 -d 將目錄像文件一樣顯示,而不是顯示其下的文件 -e 輸出時間的全部信息,而不是輸出簡略信息 -f -U 對輸出的文件不排序 -i 輸出文件的 i 節點的索引信息 -k 以 k 字節的形式表示文件的大小 -l 列出文件的詳細信息 -m 橫向輸出文件名,并以 , 作為分隔符 -n 用數字 UID、GID 代替名稱 -o 顯示文件除組信息外的詳細信息 -r 對目錄反向排序 -s 對每個文件名后輸出該文件的大小 -t 以時間排序 -u 以文件上次被訪問的時間排序 -v 根據版本進行排序 -x 按列排序,橫向排序 -A 顯示除 . 和 .. 外的所有文件 -B 不輸出以 ~ 結尾的備份文件 -C 按列輸出,縱向排序 -G 列出文件的組的信息 -L 列出鏈接文件名,而不是鏈接到的文件 -N 不限制文件長度 -Q 把輸出的文件名用雙引號擴起來 -R 列出所有子目錄下的文件 -S 以文件大小排序 -X 以文件的擴展名(最后一個 . 后的字符)排序 -1 一行只輸出一個文件 -color=no 不顯示彩色文件名 --help 在標準輸出上顯示幫助信息并退出 --version 在標準輸出上顯示版本信息并退出04. 參考示例
4.1 查看當前目錄下內容
[itcast@localhost ~]$ ls 公共 模板 視頻 圖片 文檔 下載 音樂 桌面 [itcast@localhost ~]$只列出當前目錄下文件或者目錄,看不到其他詳細信息,例如:文件類型、大小、修改日期和時間、權限以及鏈接等。
4.2 查看當前目錄下文件以及其屬性信息
使用 -l 選項(小寫 L 字符),可以列出文件類型、大小、修改日期和時間、權限以及硬鏈接等詳細信息。
[itcast@localhost ~]$ ls -l 總用量 0 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 公共 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 模板 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 視頻 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 圖片 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 文檔 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 下載 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 音樂 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 桌面 [itcast@localhost ~]$4.3 以易于閱讀的格式輸出文件大小
將文件大小轉變為可閱讀的方式,例如:1G、20M、60K 等。(如果要以 K 為單位,可以使用 ls -lk)
[deng@localhost ~]$ ls -lh 總用量 55M drwxrwxr-x 7 deng deng 61 1月 22 09:47 bak drwxrwxr-x 8 deng deng 73 3月 28 11:14 bj34 drwxr-xr-x 4 deng deng 4.0K 2月 25 09:21 instantclient_11_2 -rwxrwxr-x 1 deng deng 55M 1月 22 15:55 oracle_client_11gR2.tar.gz drwxrwxr-x 3 deng deng 18 1月 22 16:27 oradiag_deng drwxrwxr-x 7 deng deng 109 7月 10 17:08 projects -rwxrwxr-x 1 deng deng 2.1K 6月 30 15:14 scott_data.sql drwxrwxr-x 4 deng deng 28 7月 11 09:47 share drwxrwxr-x 8 deng deng 73 3月 10 09:16 sz12 drwxrwxr-x 3 deng deng 15 7月 12 11:39 test -rwxrwxr-x 1 deng deng 599 6月 17 16:22 test.c -rwxrwxr-x 1 deng deng 1.7K 6月 28 17:16 test.cpp [deng@localhost ~]$**注意:**也可以使用選項 --si,和 -h 類似,但是 --si 以 1000 字節為單位,而 -h 以 1024 字節為單位(命令:ls -l --si)。
4.4 列出目錄下所有文件,包括隱藏文件
[itcast@localhost ~]$ ls -a . .bash_profile .dbus .local 視頻 音樂 .. .bashrc .esd_auth .mozilla 圖片 桌面 .bash_history .cache .ICEauthority 公共 文檔 .bash_logout .config .kshrc 模板 下載 [itcast@localhost ~]$4.5 顯示每個文件的inode 號
[itcast@localhost ~]$ ls -i 134524561 公共 134524568 視頻 204126153 文檔 3153173 音樂69071508 模板 69071517 圖片 3153172 下載 204126152 桌面 [itcast@localhost ~]$4.6 顯示完整的時間
[itcast@localhost ~]$ ls --full-time 總用量 0 drwxr-xr-x 2 itcast itcast 6 2019-07-12 17:34:24.229992803 +0800 公共 drwxr-xr-x 2 itcast itcast 6 2019-07-12 17:34:24.229992803 +0800 模板 drwxr-xr-x 2 itcast itcast 6 2019-07-12 17:34:24.229992803 +0800 視頻 drwxr-xr-x 2 itcast itcast 6 2019-07-12 17:34:24.229992803 +0800 圖片 drwxr-xr-x 2 itcast itcast 6 2019-07-12 17:34:24.229992803 +0800 文檔 drwxr-xr-x 2 itcast itcast 6 2019-07-12 17:34:24.229992803 +0800 下載 drwxr-xr-x 2 itcast itcast 6 2019-07-12 17:34:24.229992803 +0800 音樂 drwxr-xr-x 2 itcast itcast 6 2019-07-12 17:34:24.229992803 +0800 桌面 [itcast@localhost ~]$4.7 遞歸顯示子目錄
[itcast@localhost ~]$ ls -R .: 公共 模板 視頻 圖片 文檔 下載 音樂 桌面./公共:./模板:./視頻:./圖片:./文檔:./下載:./音樂:./桌面: [itcast@localhost ~]$4.8 列出文件并標記顏色分類
[itcast@localhost ~]$ ls --color=auto 公共 模板 視頻 圖片 文檔 下載 音樂 桌面 [itcast@localhost ~]$4.9 按照特殊字符對文件進行分類
[deng@localhost ~]$ ls -F bak/ oradiag_deng/ sz12/ 公共/ 文檔/ bj34/ projects/ test/ 模板/ 下載/ instantclient_11_2/ scott_data.sql* test.c* 視頻/ 音樂/ oracle_client_11gR2.tar.gz* share/ test.cpp* 圖片/ 桌面/ [deng@localhost ~]$4.10 按修改時間列出文件和文件夾詳細信息
[itcast@localhost ~]$ ls -ltr 總用量 0 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 桌面 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 音樂 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 下載 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 文檔 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 圖片 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 視頻 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 模板 drwxr-xr-x 2 itcast itcast 6 7月 12 17:34 公共 [itcast@localhost ~]$4.11 根據修改時間排序
[deng@localhost ~]$ ls -lt 總用量 56296 drwxrwxr-x 3 deng deng 15 7月 12 11:39 test drwxrwxr-x 4 deng deng 28 7月 11 09:47 share drwxrwxr-x 7 deng deng 109 7月 10 17:08 projects -rwxrwxr-x 1 deng deng 2052 6月 30 15:14 scott_data.sql -rwxrwxr-x 1 deng deng 1671 6月 28 17:16 test.cpp -rwxrwxr-x 1 deng deng 599 6月 17 16:22 test.c drwxrwxr-x 8 deng deng 73 3月 28 11:14 bj34 drwxrwxr-x 8 deng deng 73 3月 10 09:16 sz12 drwxr-xr-x 4 deng deng 4096 2月 25 09:21 instantclient_11_2 drwxr-xr-x. 2 deng deng 6 2月 21 09:27 桌面 drwxrwxr-x 3 deng deng 18 1月 22 16:27 oradiag_deng -rwxrwxr-x 1 deng deng 57628889 1月 22 15:55 oracle_client_11gR2.tar.gz [deng@localhost ~]$4.12 顯示目錄詳細信息
[deng@localhost ~]$ ls -ld /etc drwxr-xr-x. 141 root root 8192 7月 12 20:03 /etc [deng@localhost ~]$4.13 打印文件的UID和GID
[itcast@localhost ~]$ ls -n 總用量 0 drwxr-xr-x 2 1002 1003 6 7月 12 17:34 公共 drwxr-xr-x 2 1002 1003 6 7月 12 17:34 模板 drwxr-xr-x 2 1002 1003 6 7月 12 17:34 視頻 drwxr-xr-x 2 1002 1003 6 7月 12 17:34 圖片 drwxr-xr-x 2 1002 1003 6 7月 12 17:34 文檔 drwxr-xr-x 2 1002 1003 6 7月 12 17:34 下載 drwxr-xr-x 2 1002 1003 6 7月 12 17:34 音樂 drwxr-xr-x 2 1002 1003 6 7月 12 17:34 桌面 [itcast@localhost ~]$4.14 所有項目以逗號分隔,并填滿整行行寬
[itcast@localhost ~]$ ls -m 公共, 模板, 視頻, 圖片, 文檔, 下載, 音樂, 桌面 [itcast@localhost ~]$4.15 顯示指定文件的詳細信息
[deng@localhost ~]$ ls -l test.c -rwxrwxr-x 1 deng deng 599 6月 17 16:22 test.c [deng@localhost ~]$4.16 對目錄加上表示符號"/"
[deng@localhost ~]$ ls -p bak/ oradiag_deng/ sz12/ 公共/ 文檔/ bj34/ projects/ test/ 模板/ 下載/ instantclient_11_2/ scott_data.sql test.c 視頻/ 音樂/ oracle_client_11gR2.tar.gz share/ test.cpp 圖片/ 桌面/ [deng@localhost ~]$4.17 逆序排列
[deng@localhost ~]$ ls -lr 總用量 56296 -rwxrwxr-x 1 deng deng 1671 6月 28 17:16 test.cpp -rwxrwxr-x 1 deng deng 599 6月 17 16:22 test.c drwxrwxr-x 3 deng deng 15 7月 12 11:39 test drwxrwxr-x 8 deng deng 73 3月 10 09:16 sz12 drwxrwxr-x 4 deng deng 28 7月 11 09:47 share -rwxrwxr-x 1 deng deng 2052 6月 30 15:14 scott_data.sql drwxrwxr-x 7 deng deng 109 7月 10 17:08 projects drwxrwxr-x 3 deng deng 18 1月 22 16:27 oradiag_deng -rwxrwxr-x 1 deng deng 57628889 1月 22 15:55 oracle_client_11gR2.tar.gz drwxr-xr-x 4 deng deng 4096 2月 25 09:21 instantclient_11_2 drwxrwxr-x 8 deng deng 73 3月 28 11:14 bj34 drwxrwxr-x 7 deng deng 61 1月 22 09:47 bak [deng@localhost ~]$4.18 根據文件大小排序
[deng@localhost ~]$ ls -lS 總用量 56296 -rwxrwxr-x 1 deng deng 57628889 1月 22 15:55 oracle_client_11gR2.tar.gz drwxr-xr-x 4 deng deng 4096 2月 25 09:21 instantclient_11_2 -rwxrwxr-x 1 deng deng 2052 6月 30 15:14 scott_data.sql -rwxrwxr-x 1 deng deng 1671 6月 28 17:16 test.cpp -rwxrwxr-x 1 deng deng 599 6月 17 16:22 test.c drwxrwxr-x 7 deng deng 109 7月 10 17:08 projects drwxrwxr-x 8 deng deng 73 3月 28 11:14 bj34 drwxrwxr-x 8 deng deng 73 3月 10 09:16 sz12 drwxrwxr-x 7 deng deng 61 1月 22 09:47 bak drwxrwxr-x 4 deng deng 28 7月 11 09:47 share drwxrwxr-x 3 deng deng 18 1月 22 16:27 oradiag_deng drwxrwxr-x 3 deng deng 15 7月 12 11:39 test [deng@localhost ~]$05. 附錄
參考:【Linux】一步一步學Linux系列教程匯總
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的【Linux】一步一步学Linux——ls命令(29)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux】一步一步学Linux——L
- 下一篇: 【Linux】一步一步学Linux——m