ls常用命令詳解
NAME(名稱)
ls - list directory contents
SYNOPSIS(語法)
ls [OPTION]… [FILE]…
DESCRIPTION(描述)
List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.Mandatory arguments to long options are mandatory for short options too.
列出關於檔案(預設當前目錄)的資訊。如果沒有-cftuvSUX 和 --sort沒有指定選項,就按找字母順序給條目排序。對長選項的強制引數對於短選項也是強制的。(感覺應該是長選項等價於短選項的意思 )
以下就不一一翻譯了,反正英語很重要,不懂的自己查字典吧。
-a, --all
do not ignore entries starting with .
-l use a long listing format
-d, --directory
list directories themselves, not their contents
-R, --recursive
list subdirectories recursively
-S sort by file size
–sort=WORD
sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), exten‐
sion (-X)
–time=WORD
with -l, show time as WORD instead of default modification time: atime or access or
use (-u) ctime or status (-c); also use specified time as sort key if --sort=time
–time-style=STYLE
with -l, show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT;
FORMAT is interpreted like in ‘date’; if FORMAT is FORMAT1FORMAT2, then
FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is pre‐
fixed with ‘posix-’, STYLE takes effect only outside the POSIX locale
-t sort by modification time, newest first
-U do not sort; list entries in directory order
以上是常用選項的man幫助內容,ls命令在日常使用頻繁,所以需要靈活掌握此命令。
ls命令使用語法為 ls [選項][檔案] ,其中[]內為可選項,下面介紹常用命令的使用方法。
1 不加選項和引數
[[email protected] ~]#ls
anaconda-ks.cfg Desktop Documents Downloads f3 Music Pictures Public Templates Videos
解析 沒有選項和引數時列出當前資料夾下的所有非隱藏內容。
2 選項-a
[[email protected] ~]#ls -a /root
. .bash_logout .config Documents .ICEauthority Pictures Videos
.. .bash_profile .cshrc Downloads .lesshst Public .viminfo
anaconda-ks.cfg .bashrc .dbus .esd_auth .local .tcshrc
.bash_history .cache Desktop f3 Music Templates
-a選項列出/root目錄下所有檔案和資料夾(包括隱藏檔案)。
3 選項-d
[[email protected] ~]# ls -d /root
/root
僅列出目錄本身
[[email protected] ~]#ls -d /root/
/root/
僅列出目錄本身
[[email protected] ~]#ls -d /root/*
/root/anaconda-ks.cfg /root/etc /root/ss.log
/root/core.2431 /root/etc2018-12-21 /root/Templates
/root/dead.letter /root/motd /root/test
/root/Desktop /root/Music /root/txt
/root/Documents /root/Pictures /root/txt2
/root/Downloads /root/Public /root/Videos
萬用字元*代表所有(但不包括.)
[[email protected] ~]#ls -d /root/.*
/root/. /root/.bashrc /root/.esd_auth /root/.tcshrc
/root/.. /root/.cache /root/.ICEauthority /root/.viminfo
/root/.bash_history /root/.config /root/.lesshst /root/.vimrc
/root/.bash_logout /root/.cshrc /root/.local
/root/.bash_profile /root/.dbus /root/.ssh
.表示root資料夾下所有隱藏檔案,包括上級目錄…(此處的包括了. 見上例"/root/…",再次補充一下,*表示第一個字元不能為.第二個字元隨意的意思,個人觀點··· )
4 選項-R
[[email protected] ~]#ls -R /root/
/root/:
anaconda-ks.cfg Desktop etc Music ss.log txt
core.2431 Documents etc2018-12-21 Pictures Templates txt2
dead.letter Downloads motd Public test Videos
/root/Desktop:
/root/Documents:
/root/Downloads:
/root/Music:
/root/Pictures:
/root/Public:
/root/Templates:
/root/Videos:
R為遞迴,表示列出root資料夾下以及其子資料夾子子資料夾(只要往上級資料夾數能數到root)下的所有非隱藏檔案
可以自己試試[[email protected] ~]#ls -aR /root/,觀察兩者不同之處。
5 選項-l和-S
[[email protected] ~]#ls -l --sort=size /root/ 等價於 ls -lS /root/
total 5148
-rw-------. 1 root root 5246976 Dec 18 11:17 core.2431
-rw-------. 1 root root 1945 Dec 11 14:20 anaconda-ks.cfg
-rw-r--r--. 1 root root 559 Dec 19 16:08 ss.log
-rw-r--r--. 1 root root 72 Dec 20 14:47 txt
-rw-r--r--. 1 root root 22 Dec 20 14:52 txt2
-rw-------. 1 root root 18 Dec 17 20:38 dead.letter
lrwxrwxrwx. 1 root root 7 Dec 15 15:25 test -> ../date
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Desktop
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Documents
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Downloads
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Music
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Pictures
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Public
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Templates
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Videos
-rw-r--r--. 1 root root 3 Dec 21 16:18 etc
-rw-r--r--. 1 root root 0 Dec 21 16:32 etc2018-12-21
-rw-r--r--. 1 root root 0 Dec 19 15:35 motd
l為列出條目的metadata(源屬性)S是按照大小排序
6 選項-t
[[email protected] ~]#ls -lt /root/
total 5148
-rw-r--r--. 1 root root 0 Dec 21 16:32 etc2018-12-21
-rw-r--r--. 1 root root 3 Dec 21 16:18 etc
-rw-r--r--. 1 root root 22 Dec 20 14:52 txt2
-rw-r--r--. 1 root root 72 Dec 20 14:47 txt
-rw-r--r--. 1 root root 559 Dec 19 16:08 ss.log
-rw-r--r--. 1 root root 0 Dec 19 15:35 motd
-rw-------. 1 root root 5246976 Dec 18 11:17 core.2431
-rw-------. 1 root root 18 Dec 17 20:38 dead.letter
lrwxrwxrwx. 1 root root 7 Dec 15 15:25 test -> ../date
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Desktop
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Documents
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Downloads
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Music
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Pictures
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Public
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Templates
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Videos
-rw-------. 1 root root 1945 Dec 11 14:20 anaconda-ks.cfg
-t選項為按照mtime排序 另外-u配合-t可以按照atime排序也常用 ls -ltu /root/
7選項數字1和-X
[[email protected] ~]#ls -1X /root/
Desktop
Documents
Downloads
etc
etc2018-12-21
motd
Music
Pictures
Public
Templates
test
txt
txt2
Videos
core.2431
anaconda-ks.cfg
dead.letter
ss.log
此處選項為數字1與X結合1表示內容分行顯示,X為按檔案字尾排序。
8 選項-U
[[email protected] ~]#ls -lU /root/
total 5148
-rw-------. 1 root root 1945 Dec 11 14:20 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Desktop
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Downloads
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Templates
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Public
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Documents
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Pictures
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Videos
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Music
lrwxrwxrwx. 1 root root 7 Dec 15 15:25 test -> ../date
-rw-------. 1 root root 18 Dec 17 20:38 dead.letter
-rw-------. 1 root root 5246976 Dec 18 11:17 core.2431
-rw-r--r--. 1 root root 0 Dec 19 15:35 motd
-rw-r--r--. 1 root root 559 Dec 19 16:08 ss.log
-rw-r--r--. 1 root root 72 Dec 20 14:47 txt
-rw-r--r--. 1 root root 22 Dec 20 14:52 txt2
-rw-r--r--. 1 root root 3 Dec 21 16:18 etc
-rw-r--r--. 1 root root 0 Dec 21 16:32 etc2018-12-21
-U按照目錄存放順序顯示