文件(夾)查看類命令
ls--顯示指定目錄下內(nèi)容
![](/d/20211018/d8ed133dc41224046e467e1b4200d5a4.gif)
說(shuō)明:ls 顯示結(jié)果以不同的顏色來(lái)區(qū)分文件類別。藍(lán)色代表目錄,灰色代表普通文件,綠色代表可執(zhí)行文件,紅色代表壓縮文件,淺藍(lán)色代表鏈接文件。
![](/d/20211018/aba0893810d8f1bf51ed767df5a4a7e4.gif)
-a---顯示所有內(nèi)容,包括隱藏文件
說(shuō)明:在Linux系統(tǒng)中,以“.”開(kāi)頭的就是隱藏文件或隱藏目錄。
-l---以長(zhǎng)格式(內(nèi)容更詳細(xì))顯示文件或目錄的詳細(xì)信息。
![](/d/20211018/19cac7b0df5b7101b056f9496eecb996.gif)
說(shuō)明:ls -l命令可以簡(jiǎn)寫(xiě)成ll,
輸出的信息共分為7組:
文件類別和文件權(quán)限、鏈接數(shù)或子目錄個(gè)數(shù)、文件所有者、文件所屬組、文件大小(單位為字節(jié)B)、文件創(chuàng)建或修改時(shí)間、文件名。
文件類別:第一組前1位表示文件類別,“-”代表普通文件,“d”代表目錄,“l”代表符號(hào)鏈接,“c”代表字符設(shè)備,“b”代表塊設(shè)備
文件權(quán)限:第一組后9位表示文件權(quán)限,前3位為user、中間3位為group、后三位為other的權(quán)限
-d---顯示目錄本身的屬性而不是目錄中的內(nèi)容。
1 [root@localhost ~]# ls -ld /home
2 drwxr-xr-x. 4 root root 4096 9月 22 10:41 /home
3 [root@localhost ~]# ls -d /home
4 /home
5 [root@localhost ~]#
-h---以K、M、G等單位顯示文件大?。J(rèn)為字節(jié))
1 [root@localhost ~]# ls -h /home
2 justin lost+found
3 [root@localhost ~]# ls -lh /home
4 總用量 20K
5 drwx------. 27 justin justin 4.0K 9月 22 13:19 justin
6 drwx------. 2 root root 16K 9月 18 15:30 lost+found
7 [root@localhost ~]#
-R---若目錄下有檔案,也將檔案依序列出
1 [root@localhost ~]# ls -lR /home
2 /home:
3 總用量 20
4 drwx------. 27 justin justin 4096 9月 22 13:19 justin
5 drwx------. 2 root root 16384 9月 18 15:30 lost+found
6 /home/justin:
7 總用量 32
8 drwxr-xr-x. 2 justin justin 4096 9月 22 10:49 公共的
9 drwxr-xr-x. 2 justin justin 4096 9月 22 10:49 模板
10 drwxr-xr-x. 2 justin justin 4096 9月 22 10:49 視頻
11 drwxr-xr-x. 2 justin justin 4096 9月 22 10:49 圖片
12 drwxr-xr-x. 2 justin justin 4096 9月 22 10:49 文檔
13 drwxr-xr-x. 2 justin justin 4096 9月 22 10:49 下載
14 drwxr-xr-x. 2 justin justin 4096 9月 22 10:49 音樂(lè)
15 drwxr-xr-x. 2 justin justin 4096 9月 22 10:49 桌面
16 /home/justin/公共的:
17 總用量 0
18 /home/justin/模板:
19 總用量 0
20 /home/justin/視頻:
21 總用量 0
22 /home/justin/圖片:
23 總用量 0
24 /home/justin/文檔:
25 總用量 0
26 /home/justin/下載:
27 總用量 0
28 /home/justin/音樂(lè):
29 總用量 0
30 /home/justin/桌面:
31 總用量 0
32 /home/lost+found:
33 總用量 0
34 [root@localhost ~]#
-t---將檔案按照建立時(shí)間的先后次序列出
1 [root@localhost ~]# ls -l /home
2 總用量 20
3 drwx------. 27 justin justin 4096 9月 22 13:19 justin
4 drwx------. 2 root root 16384 9月 18 15:30 lost+found
5 -rw-r--r--. 1 root root 0 9月 22 15:21 t
6 [root@localhost ~]# ls -lt /home
7 總用量 20
8 -rw-r--r--. 1 root root 0 9月 22 15:21 t
9 drwx------. 27 justin justin 4096 9月 22 13:19 justin
10 drwx------. 2 root root 16384 9月 18 15:30 lost+found
11 [root@localhost ~]#
說(shuō)明:ls命令還可以結(jié)合通配符“?”或“*”一起使用,問(wèn)號(hào)“?”可以匹配文件名中的一個(gè)任意字符,而“*”可以匹配文件名中的任意多個(gè)字符。這兩個(gè)通配符同樣也適用于Shell環(huán)境中的其他大多數(shù)命令。
1 gssapi_mech.conf popt.d xml
2 gtk-2.0 portreserve yp.conf
3 hal postfix yum
4 host.conf ppp yum.conf
5 hosts prelink.cache yum.repos.d
6 hosts.allow prelink.conf
7 hosts.deny prelink.conf.d
8 [root@localhost etc]# ll -d /etc/po*.d
9 drwxr-xr-x. 2 root root 4096 1月 11 2010 /etc/popt.d
10 [root@localhost etc]# ll -d /etc/po?.d
11 ls: 無(wú)法訪問(wèn)/etc/po?.d: 沒(méi)有那個(gè)文件或目錄
12 [root@localhost etc]#
du---顯示文件或目錄大小
-h或--human-readable---以K,M,G為單位,提高信息的可讀性
1 [root@localhost src]# du -h nagios-3.5.0.tar.gz
2 1.8M nagios-3.5.0.tar.gz
3 [root@localhost src]# du nagios-3.5.0.tar.gz
4 1748 nagios-3.5.0.tar.gz
5 [root@localhost src]#
-a---顯示全部目錄和其次目錄下的每個(gè)檔案所占的磁盤(pán)空間
-b或-bytes---顯示目錄或文件大小時(shí),以byte為單位
1 [root@localhost local]# du -b src/nagios-3.5.0.tar.gz
2 1789376 src/nagios-3.5.0.tar.gz
3 [root@localhost local]#
-c或--total---顯示每個(gè)目錄或文件的大小外,同時(shí)也顯示所有目錄或文件的總和
-m或--megabytes---以1MB為單位
-s---只顯示各檔案大小的總合
1 [root@localhost local]# du -sh src/
2 41M src/
3 [root@localhost local]#
-x---只計(jì)算同屬同一個(gè)檔案系統(tǒng)的檔案
-L---計(jì)算所有的檔案大小
df---顯示檔案系統(tǒng)的狀況;主要用來(lái)了解系統(tǒng)中已經(jīng)掛載的各個(gè)文件系統(tǒng)的磁盤(pán)使用情況
-h 顯示更易讀的容量單位
-T 顯示文件系統(tǒng)的類型
1 [root@localhost ~]# df -Th
2 文件系統(tǒng) 類型 容量 已用 可用 已用%% 掛載點(diǎn)
3 /dev/sda2ext49.9G 2.6G 6.9G 28% /
4 tmpfs tmpfs 504M 112K 504M 1% /dev/shm
5 /dev/sda1ext4194M 27M 158M 15% /boot
6 /dev/sda5ext47.7G 147M 7.2G 2% /home
7 /dev/sr0iso96602.9G 2.9G 0 100% /media/RHEL_6.3 i386 Disc 1
8 [root@localhost ~]#
file---查看文件類型
1 [root@localhost home]# file justin/
2 justin/: directory
3 [root@localhost home]# file justin1
4 justin1: empty
5 [root@localhost ~]# file install.log
6 install.log: UTF-8 Unicode text
說(shuō)明:file命令用于查看文件的類型,可以根據(jù)文件的內(nèi)部存儲(chǔ)結(jié)構(gòu)來(lái)進(jìn)行判別,而不根據(jù)文件的擴(kuò)展名來(lái)進(jìn)行判別。在Linux系統(tǒng)中,文件擴(kuò)展名與文件類型沒(méi)有絕對(duì)的關(guān)系。
文件內(nèi)容查看
cat---顯示文件內(nèi)容
1 [root@localhost log]# cat /var/log/messages
說(shuō)明:cat在顯示文本文件的內(nèi)容時(shí)不進(jìn)行停頓,對(duì)于內(nèi)容較長(zhǎng)的文件,在快速滾屏顯示之后,只有最后一頁(yè)的文件內(nèi)容保留在屏幕中顯示,因此cat不適合查看長(zhǎng)文件。
more---分頁(yè)顯示文件內(nèi)容
-num--- 一次顯示的行數(shù)
-s ---當(dāng)遇到有連續(xù)兩行以上的空白行,就代換為一行的空白行
+num ---從第 num 行開(kāi)始顯示
1 [root@localhost log]# more -5s /var/log/messages
2 Sep 22 10:36:06 localhost kernel: imklog 5.8.10, log source= /proc/kmsgstarted.
3 Sep 22 10:36:06 localhost rsyslogd: [origin software="rsyslogd"swVersion="5.8.10"x-pid
4 ="1323"x-info="http://www.rsyslog.com"] start
5 Sep 22 10:36:06 localhost kernel: Initializing cgroup subsys cpuset
6 Sep 22 10:36:06 localhost kernel: Initializing cgroup subsys cpu
7 --More--(0%)
說(shuō)明:類似 cat ,不過(guò)會(huì)以一頁(yè)一頁(yè)的顯示方便使用者逐頁(yè)閱讀,空白鍵(space):顯示下一頁(yè),b 鍵:顯示上一頁(yè),q 鍵 :退出
less----分頁(yè)顯示文件內(nèi)容
說(shuō)明:less命令的用法與more命令類似,它們之間的區(qū)別是當(dāng)文件內(nèi)容顯示到文件尾時(shí),more命令會(huì)自動(dòng)退出閱讀環(huán)境,而less命令不自動(dòng)退出,用戶仍然可以利用上下鍵來(lái)卷動(dòng)文件,這樣更加有利于對(duì)文件內(nèi)容的反復(fù)閱讀。當(dāng)要結(jié)束瀏覽時(shí),要在less命令的提示符“:”后按Q鍵退出
head---查看文件開(kāi)頭部分的內(nèi)容
-n 指定顯示的具體行數(shù)。
1 [root@localhost ~]# head -5 /etc/passwd
2 root:x:0:0:root:/root:/bin/bash
3 bin:x:1:1:bin:/bin:/sbin/nologin
4 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 adm:x:3:4:adm:/var/adm:/sbin/nologin
6 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 [root@localhost ~]#
說(shuō)明:默認(rèn)情況下,head顯示前10行內(nèi)容
tail---查看文件末尾部分的內(nèi)容
1 [root@localhost ~]# tail -5 /etc/passwd
2 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
3 abrt:x:173:173::/etc/abrt:/sbin/nologin
4 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
5 tcpdump:x:72:72::/:/sbin/nologin
6 justin:x:500:500:justin_peng:/home/justin:/bin/bash
7 [root@localhost ~]#
說(shuō)明:tail命令用法與head命令類似
創(chuàng)建文件(夾)命令
touch---改變檔案的時(shí)間記錄,檔案不存在則創(chuàng)建一個(gè)空文件
1 [root@localhost home]# touch file1 file2
2 [root@localhost home]# ls
3 file1 file2 justin lost+found t
4 [root@localhost home]#
-a 改變檔案的讀取時(shí)間記錄。
-m 改變檔案的修改時(shí)間記錄。
-c 假如目的檔案不存在,不會(huì)建立新的檔案
說(shuō)明:在實(shí)際使用中經(jīng)常用于創(chuàng)建新的測(cè)試文件。使用文件名作為參數(shù),可以同時(shí)創(chuàng)建多個(gè)文件。當(dāng)目標(biāo)文件已經(jīng)存在時(shí),將更新該文件的時(shí)間標(biāo)記,否則將創(chuàng)建指定名稱的空文件。
mkdir---Make Directory---創(chuàng)建新的目錄
-p 確保目錄名稱存在,不存在的就建一個(gè)。
1 [root@localhost home]# ls
2 justin lost+found t
3 [root@localhost home]# mkdir dir1
4 [root@localhost home]# mkdir dir2/dir
5 mkdir: 無(wú)法創(chuàng)建目錄"dir2/dir": 沒(méi)有那個(gè)文件或目錄
6 [root@localhost home]# mkdir -p dir2/dir
7 [root@localhost home]#
同時(shí)創(chuàng)建多級(jí)目錄
1 [root@localhost home]# ls
2 justin lost+found t
3 [root@localhost home]# mkdir -p {dir1,dir2/{dir3,dir4}}
4 [root@localhost home]# ls
5 dir1 dir2 justin lost+found t
6 [root@localhost home]# ls dir2
7 dir3 dir4
8 [root@localhost home]#
刪除文件(夾)命令
rmdir---刪除空目錄(若目錄非空刪除中會(huì)報(bào)錯(cuò))
-p 當(dāng)子目錄被刪除后也成為空目錄的話,則順便一并刪除
1 [root@localhost home]# mkdir -p {dir1,dir2/dir3}
2 [root@localhost home]# ls
3 dir1 dir2 justin lost+found t
4 [root@localhost home]# rmdir dir1
5 [root@localhost home]# rmdir dir2
6 rmdir: 刪除 "dir2"失敗: 目錄非空
7 [root@localhost home]# rmdir -p dir2/dir3/
8 [root@localhost home]# ls
9 justin lost+found t
10 [root@localhost home]#
rm---刪除檔案或目錄
-i 刪除前逐一詢問(wèn)確認(rèn)。
-f 即使原檔案屬性設(shè)為唯讀,亦直接刪除,無(wú)需逐一確認(rèn)。
-r 將目錄及以下檔案也逐一刪除。
1 [root@localhost home]# mkdir -p dir1/dir2
2 [root@localhost home]# rm -r dir1/
3 rm:是否進(jìn)入目錄"dir1"? y
4 rm:是否刪除目錄 "dir1/dir2"?y
5 rm:是否刪除目錄 "dir1"?y
6 [root@localhost home]#