CentOS中文件夾基本操作命令


文章來源:http://www.centoscn.com/CentOS/help/2013/1024/1967.html


文件(夾)查看類命令

ls--顯示指定目錄下內容

145244955.png

說明:ls 顯示結果以不同的顏色來區分文件類別。藍色代表目錄灰色代表普通文件綠色代表可執行文件紅色代表壓縮文件淺藍色代表鏈接文件

-a---顯示所有內容,包括隱藏文件

144553371.png

說明:在Linux系統中,以“.”開頭的就是隱藏文件或隱藏目錄。

-l---以長格式(內容更詳細)顯示文件或目錄的詳細信息。150954105.png

說明:ls -l命令可以簡寫成ll,

輸出的信息共分為7組:
文件類別和文件權限、鏈接數或子目錄個數、文件所有者、文件所屬組、文件大小(單位為字節B)、文件創建或修改時間、文件名。

文件類別:第一組前1位表示文件類別,“-”代表普通文件,“d”代表目錄,“l”代表符號鏈接,“c”代表字符設備,“b”代表塊設備
文件權限:第一組后9位表示文件權限,前3位為user、中間3位為group、后三位為other的權限

-d---顯示目錄本身的屬性而不是目錄中的內容。

1
2
3
4
5
[root@localhost ~]# ls -ld /home
drwxr-xr-x. 4 root root 4096 9月  22 10:41/home
[root@localhost ~]# ls -d /home
/home
[root@localhost ~]#

-h---以K、M、G等單位顯示文件大小(默認為字節)

1
2
3
4
5
6
7
[root@localhost ~]# ls -h /home
justin  lost+found
[root@localhost ~]# ls -lh /home
總用量 20K
drwx------. 27 justin justin 4.0K 9月  22 13:19 justin
drwx------.  2 root   root    16K 9月  18 15:30 lost+found
[root@localhost ~]#

-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
[root@localhost ~]# ls -lR /home
/home:
總用量 20
drwx------. 27 justin justin  4096 9月  22 13:19 justin
drwx------.  2 root   root   16384 9月  18 15:30 lost+found
/home/justin:
總用量 32
drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 公共的
drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 模板
drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 視頻
drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 圖片
drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 文檔
drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 下載
drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 音樂
drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 桌面
/home/justin/公共的:
總用量 0
/home/justin/模板:
總用量 0
/home/justin/視頻:
總用量 0
/home/justin/圖片:
總用量 0
/home/justin/文檔:
總用量 0
/home/justin/下載:
總用量 0
/home/justin/音樂:
總用量 0
/home/justin/桌面:
總用量 0
/home/lost+found:
總用量 0
[root@localhost ~]#

-t---將檔案按照建立時間的先后次序列出

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# ls -l /home
總用量 20
drwx------. 27 justin justin  4096 9月  22 13:19 justin
drwx------.  2 root   root   16384 9月  18 15:30 lost+found
-rw-r--r--.  1 root   root       0 9月  22 15:21 t
[root@localhost ~]# ls -lt /home
總用量 20
-rw-r--r--.  1 root   root       0 9月  22 15:21 t
drwx------. 27 justin justin  4096 9月  22 13:19 justin
drwx------.  2 root   root   16384 9月  18 15:30 lost+found
[root@localhost ~]#

說明:ls命令還可以結合通配符“?”或“*”一起使用,問號“?”可以匹配文件名中的一個任意字符,而“*”可以匹配文件名中的任意多個字符。這兩個通配符同樣也適用於Shell環境中的其他大多數命令。

1
2
3
4
5
6
7
8
9
10
11
12
gssapi_mech.conf         popt.d                        xml
gtk-2.0                  portreserve                   yp.conf
hal                      postfix                       yum
host.conf                ppp                           yum.conf
hosts                    prelink.cache                 yum.repos.d
hosts.allow              prelink.conf
hosts.deny               prelink.conf.d
[root@localhost etc]# ll -d /etc/po*.d
drwxr-xr-x. 2 root root 4096 1月  11 2010/etc/popt.d
[root@localhost etc]# ll -d /etc/po?.d
ls: 無法訪問/etc/po?.d: 沒有那個文件或目錄
[root@localhost etc]#


du---顯示文件或目錄大小

-h或--human-readable---以K,M,G為單位,提高信息的可讀性

1
2
3
4
5
[root@localhost src]# du -h nagios-3.5.0.tar.gz
1.8M    nagios-3.5.0.tar.gz
[root@localhost src]# du nagios-3.5.0.tar.gz
1748    nagios-3.5.0.tar.gz
[root@localhost src]#

-a---顯示全部目錄和其次目錄下的每個檔案所占的磁盤空間

-b或-bytes---顯示目錄或文件大小時,以byte為單位

1
2
3
[root@localhostlocal]# du -b src/nagios-3.5.0.tar.gz
1789376 src/nagios-3.5.0.tar.gz
[root@localhostlocal]#

-c或--total---顯示每個目錄或文件的大小外,同時也顯示所有目錄或文件的總和


-m或--megabytes---以1MB為單位
-s---只顯示各檔案大小的總合

1
2
3
[root@localhostlocal]# du -sh src/
41M src/
[root@localhostlocal]#

-x---只計算同屬同一個檔案系統的檔案

-L---計算所有的檔案大小


df---顯示檔案系統的狀況;主要用來了解系統中已經掛載的各個文件系統的磁盤使用情況

-h 顯示更易讀的容量單位
-T 顯示文件系統的類型

1
2
3
4
5
6
7
8
[root@localhost ~]# df -Th
文件系統    類型      容量  已用  可用 已用%% 掛載點
/dev/sda2ext49.9G  2.6G  6.9G  28% /
tmpfs        tmpfs    504M  112K  504M   1%/dev/shm
/dev/sda1ext4194M   27M  158M  15%/boot
/dev/sda5ext47.7G  147M  7.2G   2%/home
/dev/sr0iso96602.9G  2.9G     0 100%/media/RHEL_6.3 i386 Disc 1
[root@localhost ~]#

file---查看文件類型

1
2
3
4
5
6
[root@localhost home]# file justin/
justin/: directory
[root@localhost home]# file justin1
justin1: empty
[root@localhost ~]# file install.log
install.log: UTF-8 Unicode text

說明:file命令用於查看文件的類型,可以根據文件的內部存儲結構來進行判別,而不根據文件的擴展名來進行判別。在Linux系統中,文件擴展名與文件類型沒有絕對的關系。


文件內容查看

cat---顯示文件內容

1
[root@localhost log]# cat /var/log/messages

說明:cat在顯示文本文件的內容時不進行停頓,對於內容較長的文件,在快速滾屏顯示之后,只有最后一頁的文件內容保留在屏幕中顯示,因此cat不適合查看長文件。

more---分頁顯示文件內容

-num--- 一次顯示的行數
-s ---當遇到有連續兩行以上的空白行,就代換為一行的空白行
+num ---從第 num 行開始顯示

1
2
3
4
5
6
7
[root@localhost log]# more -5s /var/log/messages
Sep 22 10:36:06 localhost kernel: imklog 5.8.10, logsource=/proc/kmsgstarted.
Sep 22 10:36:06 localhost rsyslogd: [origin software="rsyslogd"swVersion="5.8.10"x-pid
="1323"x-info="http://www.rsyslog.com"] start
Sep 22 10:36:06 localhost kernel: Initializing cgroup subsys cpuset
Sep 22 10:36:06 localhost kernel: Initializing cgroup subsys cpu
--More--(0%)

說明:類似 cat ,不過會以一頁一頁的顯示方便使用者逐頁閱讀,空白鍵(space):顯示下一頁,b 鍵:顯示上一頁,q 鍵 :退出

less----分頁顯示文件內容

說 明:less命令的用法與more命令類似,它們之間的區別是當文件內容顯示到文件尾時,more命令會自動退出閱讀環境,而less命令不自動退出,用 戶仍然可以利用上下鍵來卷動文件,這樣更加有利於對文件內容的反復閱讀。當要結束瀏覽時,要在less命令的提示符“:”后按Q鍵退出

head---查看文件開頭部分的內容

-n 指定顯示的具體行數。

1
2
3
4
5
6
7
[root@localhost ~]# head -5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost ~]#

說明:默認情況下,head顯示前10行內容

tail---查看文件末尾部分的內容

1
2
3
4
5
6
7
[root@localhost ~]# tail -5 /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
justin:x:500:500:justin_peng:/home/justin:/bin/bash
[root@localhost ~]#
說明:tail命令用法與head命令類似



創建文件(夾)命令

touch---改變檔案的時間記錄,檔案不存在則創建一個空文件

1
2
3
4
[root@localhost home]# touch file1 file2
[root@localhost home]# ls
file1  file2  justin  lost+found  t
[root@localhost home]#

-a 改變檔案的讀取時間記錄。
-m 改變檔案的修改時間記錄。
-c 假如目的檔案不存在,不會建立新的檔案

說明:在實際使用中經常用於創建新的測試文件。使用文件名作為參數,可以同時創建多個文件。當目標文件已經存在時,將更新該文件的時間標記,否則將創建指定名稱的空文件。

mkdir---Make Directory---創建新的目錄

-p 確保目錄名稱存在,不存在的就建一個。

1
2
3
4
5
6
7
[root@localhost home]# ls
justin  lost+found  t
[root@localhost home]# mkdir dir1
[root@localhost home]# mkdir dir2/dir
mkdir: 無法創建目錄"dir2/dir": 沒有那個文件或目錄
[root@localhost home]# mkdir -p dir2/dir
[root@localhost home]#

同時創建多級目錄

1
2
3
4
5
6
7
8
[root@localhost home]# ls
justin  lost+found  t
[root@localhost home]# mkdir -p {dir1,dir2/{dir3,dir4}}
[root@localhost home]# ls
dir1  dir2  justin  lost+found  t
[root@localhost home]# ls dir2
dir3  dir4
[root@localhost home]#


刪除文件(夾)命令

rmdir---刪除空目錄(若目錄非空刪除中會報錯)

-p 當子目錄被刪除后也成為空目錄的話,則順便一並刪除

1
2
3
4
5
6
7
8
9
10
[root@localhost home]# mkdir -p {dir1,dir2/dir3}
[root@localhost home]# ls
dir1  dir2  justin  lost+found  t
[root@localhost home]# rmdir dir1
[root@localhost home]# rmdir dir2
rmdir: 刪除"dir2"失敗: 目錄非空
[root@localhost home]# rmdir -p dir2/dir3/
[root@localhost home]# ls
justin  lost+found  t
[root@localhost home]#

rm---刪除檔案或目錄

-i 刪除前逐一詢問確認。
-f 即使原檔案屬性設為唯讀,亦直接刪除,無需逐一確認。
-r 將目錄及以下檔案也逐一刪除。

1
2
3
4
5
6
[root@localhost home]# mkdir -p dir1/dir2
[root@localhost home]# rm -r dir1/
rm:是否進入目錄"dir1"? y
rm:是否刪除目錄"dir1/dir2"?y
rm:是否刪除目錄"dir1"?y
[root@localhost home]#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM