ls應該是我們最熟悉的指令之一,通常進入命令行,少不了就要ls一下。
雖然它是一個很基本很常用的命令,不過它的功能也很豐富,熟練使用它可以更加便捷我們看我們想要看到的文件信息。
本文ls介紹基於CentOS7.2環境,後面的實驗均在此環境下實現。
ls命令介紹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36[root@localhost ~]# man ls
LS(1) User Commands LS(1)
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 speci‐
fied.
Mandatory arguments to long options are mandatory for short options
too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
-d, --directory
list directories themselves, not their contents
-h, --human-readable
with -l, print sizes in human readable format (e.g., 1K 234M 2G)
-i, --inode
print the index number of each file
-l use a long listing format
-r, --reverse
reverse order while sorting
-R, --recursive
list subdirectories recursively
-S sort by file size
-t sort by modification time, newest first
-u with -lt: sort by, and show, access time; with -l: show access
time and sort by name; otherwise: sort by access time
以上的內容有所省略,本文只截取常用到的11個選項做詳細說明。
-a 顯示所有內容,包括以.開頭的隱藏文件
1 2 3 4 5[root@localhost ~]# ls -a /root
. .bash_logout .config .esd_auth .local 公共 文檔
.. .bash_profile .cshrc .ICEauthority .mozilla 模板 下載
anaconda-ks.cfg .bashrc CST initial-setup-ks.cfg .tcshrc 視頻 音樂
.bash_history .cache .dbus .lesshst .Xauthority 圖片 桌面
-A 顯示所有內容,但不包括.和..文件,顯示以.開頭的隱藏文件
1 2 3 4 5[root@localhost ~]# ls -A /root/
anaconda-ks.cfg .bashrc CST initial-setup-ks.cfg .tcshrc 視頻 音樂
.bash_history .cache .dbus .lesshst .Xauthority 圖片 桌面
.bash_logout .config .esd_auth .local 公共 文檔
.bash_profile .cshrc .ICEauthority .mozilla 模板 下載
-d 顯示目錄本身但不顯示目錄下的內容
1 2 3 4[root@localhost ~]# ls -d /root
/root
[root@localhost ~]# ls /root
anaconda-ks.cfg CST initial-setup-ks.cfg 公共 模板 視頻 圖片 文檔 下載 音樂 桌面
-h 以人易識別的方式展示文件大小必須與-l一起使用
-l 文件長格式,也就是展示文件詳細信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15[root@localhost ~]# ls /root
anaconda-ks.cfg CST initial-setup-ks.cfg 公共 模板 視頻 圖片 文檔 下載 音樂 桌面
[root@localhost ~]# ls -l /root
總用量 8
-rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 7月 21 18:52 CST
-rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 7月 22 09:52 公共
drwxr-xr-x. 2 root root 6 7月 22 09:52 模板
drwxr-xr-x. 2 root root 6 7月 22 09:52 視頻
drwxr-xr-x. 2 root root 6 7月 22 09:52 圖片
drwxr-xr-x. 2 root root 6 7月 22 09:52 文檔
drwxr-xr-x. 2 root root 6 7月 22 09:52 下載
drwxr-xr-x. 2 root root 6 7月 22 09:52 音樂
drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面
上面顯示了加-l與不加-l的卻別,下面看看加-h的好處
1 2 3 4 5 6 7 8 9 10 11 12 13[root@localhost ~]# ls -lh /root
總用量 8.0K
-rw-------. 1 root root 1.2K 7月 20 00:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 7月 21 18:52 CST
-rw-------. 1 root root 1.2K 7月 19 16:46 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 7月 22 09:52 公共
drwxr-xr-x. 2 root root 6 7月 22 09:52 模板
drwxr-xr-x. 2 root root 6 7月 22 09:52 視頻
drwxr-xr-x. 2 root root 6 7月 22 09:52 圖片
drwxr-xr-x. 2 root root 6 7月 22 09:52 文檔
drwxr-xr-x. 2 root root 6 7月 22 09:52 下載
drwxr-xr-x. 2 root root 6 7月 22 09:52 音樂
drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面
之前的字節都自動轉換為kb格式,上面的更方便與我們對於文件大小有更直觀的認識。
-i 顯示文件inode號,關於inode我還有一些困惑,等了解足夠清楚在後面會專門為inode寫一篇文字。
1 2 3 4 5 6 7 8 9 10 11 12 13[root@localhost ~]# ls -il /root
總用量 8
72664101 -rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg
73207816 drwxr-xr-x. 2 root root 6 7月 21 18:52 CST
72699695 -rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg
2536844 drwxr-xr-x. 2 root root 6 7月 22 09:52 公共
101603105 drwxr-xr-x. 2 root root 6 7月 22 09:52 模板
2536850 drwxr-xr-x. 2 root root 6 7月 22 09:52 視頻
101603106 drwxr-xr-x. 2 root root 6 7月 22 09:52 圖片
35973547 drwxr-xr-x. 2 root root 6 7月 22 09:52 文檔
73207845 drwxr-xr-x. 2 root root 6 7月 22 09:52 下載
73207846 drwxr-xr-x. 2 root root 6 7月 22 09:52 音樂
34020951 drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面
現在可以簡單的認為inode相當於大公司的員工號,大公司根據員工號精確定位某個人,而它則是准確定位文件存儲的位置。
-r 逆序顯示文件
1 2 3 4 5 6 7 8 9 10 11 12 13[root@localhost ~]# ls -rl /root
總用量 8
drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面
drwxr-xr-x. 2 root root 6 7月 22 09:52 音樂
drwxr-xr-x. 2 root root 6 7月 22 09:52 下載
drwxr-xr-x. 2 root root 6 7月 22 09:52 文檔
drwxr-xr-x. 2 root root 6 7月 22 09:52 圖片
drwxr-xr-x. 2 root root 6 7月 22 09:52 視頻
drwxr-xr-x. 2 root root 6 7月 22 09:52 模板
drwxr-xr-x. 2 root root 6 7月 22 09:52 公共
-rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 7月 21 18:52 CST
-rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg
-R 遞歸顯示目錄內容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41[root@localhost ~]# ls -Rl /root
/root:
總用量 8
-rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 7月 21 18:52 CST
-rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 7月 22 09:52 公共
drwxr-xr-x. 2 root root 6 7月 22 09:52 模板
drwxr-xr-x. 2 root root 6 7月 22 09:52 視頻
drwxr-xr-x. 2 root root 6 7月 22 09:52 圖片
drwxr-xr-x. 2 root root 6 7月 22 09:52 文檔
drwxr-xr-x. 2 root root 6 7月 22 09:52 下載
drwxr-xr-x. 2 root root 6 7月 22 09:52 音樂
drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面
/root/CST:
總用量 0
/root/公共:
總用量 0
/root/模板:
總用量 0
/root/視頻:
總用量 0
/root/圖片:
總用量 0
/root/文檔:
總用量 0
/root/下載:
總用量 0
/root/音樂:
總用量 0
/root/桌面:
總用量 0
加上遞歸後會在顯示主目錄下的內容後在分別顯示各個子目錄下的所有內容,如果子目錄下還有子目錄也依次展開。其目的與-d相反
-S 按文件大小排序
1 2 3 4 5 6 7 8 9 10 11 12 13[root@localhost ~]# ls -Sl /root
總用量 8
-rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg
-rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 7月 21 18:52 CST
drwxr-xr-x. 2 root root 6 7月 22 09:52 公共
drwxr-xr-x. 2 root root 6 7月 22 09:52 模板
drwxr-xr-x. 2 root root 6 7月 22 09:52 視頻
drwxr-xr-x. 2 root root 6 7月 22 09:52 圖片
drwxr-xr-x. 2 root root 6 7月 22 09:52 文檔
drwxr-xr-x. 2 root root 6 7月 22 09:52 下載
drwxr-xr-x. 2 root root 6 7月 22 09:52 音樂
drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面
-t 以mtime時間前後排序,mtime越新越靠前
1 2 3 4 5 6 7 8 9 10 11 12 13[root@localhost ~]# ls -tl /root
總用量 8
drwxr-xr-x. 2 root root 6 7月 22 09:52 公共
drwxr-xr-x. 2 root root 6 7月 22 09:52 模板
drwxr-xr-x. 2 root root 6 7月 22 09:52 視頻
drwxr-xr-x. 2 root root 6 7月 22 09:52 圖片
drwxr-xr-x. 2 root root 6 7月 22 09:52 文檔
drwxr-xr-x. 2 root root 6 7月 22 09:52 下載
drwxr-xr-x. 2 root root 6 7月 22 09:52 音樂
drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面
drwxr-xr-x. 2 root root 6 7月 21 18:52 CST
-rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg
-rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg
-u 必須與-t合用,表示以atime時間前後排序,atime越新越靠前
1 2 3 4 5 6 7 8 9 10 11 12 13[root@localhost ~]# ls -tul /root
總用量 8
drwxr-xr-x. 2 root root 6 7月 31 09:39 視頻
drwxr-xr-x. 2 root root 6 7月 31 09:39 圖片
drwxr-xr-x. 2 root root 6 7月 31 09:39 音樂
drwxr-xr-x. 2 root root 6 7月 31 09:39 下載
drwxr-xr-x. 2 root root 6 7月 31 09:39 文檔
drwxr-xr-x. 2 root root 6 7月 31 09:39 公共
drwxr-xr-x. 2 root root 6 7月 31 09:39 CST
drwxr-xr-x. 2 root root 6 7月 31 09:39 桌面
drwxr-xr-x. 2 root root 6 7月 31 09:39 模板
-rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg
-rw-------. 1 root root 1172 7月 19 16:46 anaconda-ks.cfg
文件名通配
下面簡要介紹下,文件名的通配符
*匹配零個或多個字符 ?匹配任何單個字符
~ 當前用戶家目錄 ~username 用戶家目錄
~-前一個工作目錄 ~+ 當前工作目錄
[0-9]匹配一個數字范圍 [a-z]:大寫和小寫字母
[A-Z]:大寫和小寫字母
[]匹配列表中的任何的一個字符 [^]匹配列表中的所有字符以外的字符
[:digit:]:任意數字,相當於0-9 [:lower:]:任意小寫字母
[:upper:]: 任意大寫字母 [:alpha:]: 任意大小寫字母
[:alnum:]:任意數字或字母 [:space:]:空格
[:punct:]:標點符號
下面通過一些實際例子,感受下文件名通配給我們帶來的一些好處。
1、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間出現至少一位數字的文件或目錄
1 2[root@localhost ~]# ls /var/l*[0-9]*[[:lower:]]
ls: 無法訪問/var/l*[0-9]*[[:lower:]]: 沒有那個文件或目錄
找不到,那我們自己創建一個符合條件的文件
1 2 3[root@localhost ~]# touch /var/lq2e
[root@localhost ~]# ls -l /var/l*[0-9]*[[:lower:]]
-rw-r--r--. 1 root root 0 7月 31 14:17 /var/lq2e
成功找到
2、顯示/etc目錄下以任意一位數字開頭,且以非數字結尾的文件或目錄
1 2 3 4 5[root@localhost ~]# ls /etc/[0-9]*[^0-9]
ls: 無法訪問/etc/[0-9]*[^0-9]: 沒有那個文件或目錄
[root@localhost ~]# touch /etc/3q
[root@localhost ~]# ls /etc/[0-9]*[^0-9]
/etc/3q
初始的/etc/下沒有符合條件的,通過創建一個符合條件的文件,成功檢索並顯示出來
3、顯示/etc/目錄下以非字母開頭,後面跟了一個字母及其它任意長度任意字符的文件或目錄
1 2[root@localhost ~]# ls /etc/[^[:alpha:]][[:alpha:]]*
/etc/3q
成功顯示
4、顯示/etc目錄下所有以m開頭以非數字結尾的文件或目錄
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16[root@localhost ~]# ls /etc/m*[^0-9]
/etc/machine-id /etc/makedumpfile.conf.sample /etc/motd /etc/my.cnf
/etc/magic /etc/man_db.conf /etc/mtab
/etc/mail.rc /etc/mke2fs.conf /etc/mtools.conf
/etc/maven:
/etc/modprobe.d:
mlx4.conf
/etc/modules-load.d:
/etc/multipath:
/etc/my.cnf.d:
mysql-clients.cnf
5、顯示/etc目錄下,所有以.d結尾的文件或目錄
1 2 3 4 5 6 7 8 9 10 11 12[root@localhost ~]# ls -d /etc/*.d
/etc/bash_completion.d /etc/ipsec.d /etc/profile.d /etc/rwtab.d
/etc/binfmt.d /etc/ld.so.conf.d /etc/rc0.d /etc/sane.d
/etc/chkconfig.d /etc/libibverbs.d /etc/rc1.d /etc/setuptool.d
/etc/cron.d /etc/logrotate.d /etc/rc2.d /etc/statetab.d
/etc/depmod.d /etc/modprobe.d /etc/rc3.d /etc/sudoers.d
/etc/dnsmasq.d /etc/modules-load.d /etc/rc4.d /etc/sysctl.d
/etc/dracut.conf.d /etc/my.cnf.d /etc/rc5.d /etc/tmpfiles.d
/etc/exports.d /etc/oddjobd.conf.d /etc/rc6.d /etc/usb_modeswitch.d
/etc/gdbinit.d /etc/pam.d /etc/rc.d /etc/xinetd.d
/etc/grub.d /etc/popt.d /etc/request-key.d /etc/yum.repos.d
/etc/init.d /etc/prelink.conf.d /etc/rsyslog.d
這裡我們得加上-d不顯示目錄內的內容,否則列出來的東西會非常多。因為我們想要的只是上述結果,所以不相關的內容還是不顯示的好。
6、顯示/etc目錄下,所有.conf結尾,且以m,n,r,p開頭的文件或目錄
1 2 3 4 5[root@localhost ~]# ls -d /etc/[mnrp]*.conf
/etc/man_db.conf /etc/nsswitch.conf /etc/radvd.conf /etc/rsyslog.conf
/etc/mke2fs.conf /etc/numad.conf /etc/request-key.conf
/etc/mtools.conf /etc/pbm2ppa.conf /etc/resolv.conf
/etc/nfsmount.conf /etc/pnm2ppa.conf /etc/rsyncd.conf
提示:我們在使用文件通配符查找自己想看到的內容是最好加上-d因為不加的話,你會看到很多非相關信息。
看到這相信大家對於ls及文件名通配內容已經有了初步的理解。
在大家看到這時不知有沒有疑惑,在上面的6個例子裡面,顯示字母我用的是[:alpha:]而不是[a-z]、[A-Z]為什麼呢,[a-z]、[A-Z]有什麼區別呢?
下面對這個問題做下說明
我們先創建一個實驗用的目錄及4個文件
/test1目錄
/test1/a文件 /test1/A文件 /test1/Z文件 /test1/z文件
1 2[root@localhost ~]# ls /test1/
a A z Z
我們先分別ls /test1目錄下的[:alpha:]、[a-z]、[A-Z]看看效果
1 2 3 4 5 6[root@localhost ~]# ls /test1/[[:alpha:]]
/test1/a /test1/A /test1/z /test1/Z
[root@localhost ~]# ls /test1/[a-z]
/test1/a /test1/A /test1/z
[root@localhost ~]# ls /test1/[A-Z]
/test1/A /test1/z /test1/Z
我們發現[:alpha:]包含所有大小寫字母,[a-z]則不包含Z,[A-Z]不包含a。雖然[a-z][A-Z]這兩個文件通配符不區分帶小寫,但是其所表示的范圍還是有細微差別,所以在我們不了解其差別的情況下,穩妥起見還是選擇使用[:alpha:]比較好。
本文永久更新鏈接地址:
http://xxxxxx/Linuxjc/1147459.html TechArticle