常用的 Linux 目錄處理命令總結

翁藝逢發表於2016-11-15

命令提示符

[root@localhost ~]#

root:當前登入使用者
localhost:主機名
~:當前所在的目錄,此處為“家”目錄
#:root超級使用者的提示符,如果是普通使用者,則為 $

命令格式

命令 [選項] [引數]

中括號 [] 表示可選

查詢目錄中的內容:ls

ls [選項] [檔案或目錄]

選項:

-a : 顯示所有檔案,包括隱藏檔案
-l : 顯示詳細資訊
-d : 檢視目錄屬性
-h : 人性化顯示檔案大小
-i : 顯示inode

根據以上選項,敲入命令,顯示結果分別如下:

[root@localhost ~]# ls
anaconda-ks.cfg  test
[root@localhost ~]# ls -a
.  ..  anaconda-ks.cfg  .bash_history  .bash_logout  .bash_profile  .bashrc  .cache  .config  .cshrc  .tcshrc  test
[root@localhost ~]# ls -l
總用量 4
-rw-------. 1 root root 2752 Nov 10 02:51 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Nov 12 19:26 test
[root@localhost ~]# ls -l anaconda-ks.cfg 
-rw-------. 1 root root 2752 Nov 10 02:51 anaconda-ks.cfg
[root@localhost ~]# ls -ld test/
drwxr-xr-x. 2 root root 6 Nov 12 19:26 test/
[root@localhost ~]# ls -lh
總用量 4.0K
-rw-------. 1 root root 2.7K Nov 10 02:51 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Nov 12 19:26 test
[root@localhost ~]# ls -i
71259104 anaconda-ks.cfg  36099565 test

請注意觀察 ls -l 與 ls -lh 命令的結果的區別

這裡需要解釋一下:

-rw-------. 1 root root 2.7K Nov 10 02:51 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Nov 12 19:26 test

首先第一個符號 “-”(引號內的-),表示檔案型別(常用的有三種,即-表示檔案,d表示目錄,l表示軟連結檔案),此外還有不常用的,為塊裝置檔案,字元裝置檔案、套接字檔案、管理檔案。

在上述中,我們可以看到 anaconda-ks.cfg 是一個檔案,而 test 是一個目錄(可理解為windows的資料夾的概念)。

其次,除去第一個符號,我們來看rw-------,一共有九個字元,需分為三組,分別為rw-,---,---,每個組按照順序分別表示u所有者,g所屬組,o其他人的許可權。在上述中,分別對應為 root,root。即第一個root表示所有者許可權為root許可權,第二個root表示所屬組的許可權也是root許可權,對於其他人,則無所謂的許可權可言。

其中,r表示可讀,w表示可寫,x表示執行的許可權。

為了更加明白,對於 anaconda-ks.cfg 這個檔案,這裡列一個表格:

前三個字元 中間三個字元 後三個字元
rw-
所有者u的許可權 所屬組g的許可權 o其他人的許可權
可讀可寫 無許可權 無許可權

那麼,對於 test 這個檔案 rwxr-xr-x,請讀者自行判斷它的許可權。

在九個字元之後的點 “.”,表示ACL許可權,之後的數字 1 表示引用計數,比如一個檔案有一個軟連結(類似windows快捷方式),那麼它的引用計數就是2。

root 後面的2.7k表示檔案的大小,再後面表示日期,最後是檔案的名稱。

目錄處理命令

建立目錄:mkdir

mkdir -p [目錄名]

-p : 遞迴建立

[root@localhost ~]# ls
anaconda-ks.cfg  test

[root@localhost ~]# mkdir otherFolder
[root@localhost ~]# ls
anaconda-ks.cfg  otherFolder  test

[root@localhost ~]# mkdir folder_2/test_2
mkdir: 無法建立目錄"folder_2/test_2": 沒有那個檔案或目錄

[root@localhost ~]# mkdir -p folder_2/test_2
[root@localhost ~]# ls
anaconda-ks.cfg  folder_2  otherFolder  test

[root@localhost ~]# ls folder_2/
test_2

如上所示,mkdir 不加選項 -p 時,可以建立一個空目錄,但是無法遞迴建立一個包含子目錄的目錄。加上 -p 即可遞迴建立。

切換所在目錄:cd

cd [目錄]

操作:

  • cd ~ : 進入當前使用者的家目錄
  • cd-: 進入上次目錄
  • cd.. : 進入上一級目錄
  • cd : 回到家目錄
[root@localhost ~]# ls
anaconda-ks.cfg  folder_2  otherFolder  test

[root@localhost ~]# cd /folder_2/test_2
[root@localhost test_2]# cd
[root@localhost ~]# cd -
/root/folder_2/test_2

[root@localhost test_2]# cd ../../otherFolder
[root@localhost otherFolder]# cd ..
[root@localhost ~]#

注意理清概念:相對路徑和絕對路徑

絕對路徑:從根目錄一級級找下去,需要寫全路徑

[root@localhost ~]# cd folder_2/test_2
[root@localhost test_2]#

相對路徑:參照當前所在目錄進行查詢

[root@localhost test_2]# cd ../../otherFolder
[root@localhost otherFolder]#

查詢所在目錄位置:pwd

pwd

可以說是最簡單的命令了,查詢所在目錄的位置

[root@localhost ~]# pwd
/root

[root@localhost ~]# ls
anaconda-ks.cfg  folder_2  otherFolder  test
[root@localhost ~]# cd folder_2/
[root@localhost folder_2]# ls
test_2
[root@localhost folder_2]# cd test_2/

[root@localhost test_2]# pwd
/root/folder_2/test_2

刪除空目錄:rmdir

rmdir [目錄名]

只能刪除空目錄,這個命令用得比較少。

[root@localhost ~]# ls
anaconda-ks.cfg  folder_2  otherFolder  test

[root@localhost ~]# rmdir otherFolder
[root@localhost ~]# ls
anaconda-ks.cfg  folder_2  test

[root@localhost ~]# rmdir folder_2
rmdir: 刪除 "folder_2" 失敗: 目錄非空
[root@localhost ~]#

刪除檔案或目錄:rm

rm -rf [檔案或目錄]

r 表示可以同時刪除檔案和目錄,f表示強制刪除

  • 如果不新增任何選項,那麼只可以刪除檔案,刪除時提示是否確認刪除
  • 如果只新增選項 -r,那麼可以刪除檔案也可以刪除目錄,刪除時提示是否確認刪除
  • 如果新增了選項 -rf,那麼將不做任何提示刪除檔案或目錄
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_2  test

[root@localhost ~]# rm abc.txt
rm:是否刪除普通空檔案 "abc.txt"?y

[root@localhost ~]# rm test
rm: 無法刪除"test": 是一個目錄
[root@localhost ~]# rm -r test
rm:是否刪除目錄 "test"?y

[root@localhost ~]# ls
anaconda-ks.cfg  folder_2

[root@localhost ~]# rm -rf folder_2
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]#

複製命令:cp

cp [選項] [原檔案或目錄] [目標目錄]

選項:

-r : 複製目錄
-p : 同時複製檔案屬性
-d : 若原始檔是連結檔案,則複製連結屬性
-a : 包含以上所有選項,相當於 -rpd

在[目標目錄]後面加上檔名,就是改名複製。

[root@localhost ~]# ls
anaconda-ks.cfg  bbc.txt  folder_a  folder_b

[root@localhost ~]# cp bbc.txt folder_a

[root@localhost ~]# ls folder_a/
bbc.txt

[root@localhost ~]# cp folder_a folder_b
cp: 略過目錄"folder_a"
[root@localhost ~]# cp -r folder_a folder_b
[root@localhost ~]# ls folder_b
folder_a  test_1

[root@localhost ~]# ll
總用量 4
-rw-------. 1 root root 2752 Nov 10 02:51 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Nov 13 17:21 bbc.txt
drwxr-xr-x. 2 root root   20 Nov 13 17:38 folder_a
drwxr-xr-x. 4 root root   34 Nov 13 17:39 folder_b

[root@localhost ~]# ll folder_a
總用量 0
-rw-r--r--. 1 root root 0 Nov 13 17:38 bbc.txt

[root@localhost ~]# cp -a bbc.txt folder_b

[root@localhost ~]# ll folder_b
總用量 0
-rw-r--r--. 1 root root  0 Nov 13 17:21 bbc.txt
drwxr-xr-x. 2 root root 20 Nov 13 17:39 folder_a
drwxr-xr-x. 2 root root  6 Nov 13 17:38 test_1
[root@localhost ~]#

這裡需要解釋一下的是,在原檔案 bbc.txt 中,其修改時間為 17:21,在普通複製下,它的時間這個屬性是不會被複制,我們可以看到複製後的bbc.txt的時間為17:38,如果需要連同屬性一起復制,那麼就加上 -pd 或者 直接 -a,如上所示,我們把bbc.txt複製到folder_b,這時我們檢視屬性的時候,時間屬性和原屬性是一致的。

在上述命令中,ll 是 ls -l 的簡寫。

剪下或改名命令:mv

mv [原檔案或目錄] [目標目錄]

  • 如果原檔案或者目錄 與 目標目錄在同一個目錄下,那麼就是重新命名
  • 如果不在同一個目錄下,那麼就是剪下

通過以下實踐理解:

[root@localhost ~]# ls
anaconda-ks.cfg  bbc.txt

[root@localhost ~]# mv bbc.txt abc.txt
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg

[root@localhost ~]# mkdir test
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  test

[root@localhost ~]# mv abc.txt test/
[root@localhost ~]# ls
anaconda-ks.cfg  test

[root@localhost ~]# ls test/
abc.txt
[root@localhost ~]#

連結命令:ln

ln -s [原檔案] [目標檔案]

生成連結檔案
-s : 建立軟連線

硬連結的特徵:

  • 擁有相同 i 節點和儲存block塊,可以看做是同一個檔案
  • 可通過i節點識別,i節點是相同的
  • 不能跨分割槽
  • 不能針對目錄使用

通過上述命令,可以理解為為某個內容新增一個標籤,通過開啟這個標籤就可以進入這個內容,硬連線,即再生成一個標籤,同樣可以通過這個標籤進入這個內容。

如果內容被修改,那麼不管從硬連結的哪個檔案進入,都是被修改的。

軟連結的特徵:

  • 類似windows的快捷方式
  • 軟連結擁有自己的i節點和block塊,但是資料塊只儲存原檔案的檔名和I節點號,並沒有實際的檔案資料
  • lrwxrwxrwx l為軟連結(軟連結的許可權都為rwxrwxrwx,這只是軟連結本身的許可權)
  • 修改任意檔案,另一個都改變
  • 刪除原檔案,軟連結不能用(和windows的快捷方式一樣)

硬連結:

[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# mkdir folder
[root@localhost ~]# ls
anaconda-ks.cfg  folder
[root@localhost ~]# touch bbb.txt
[root@localhost ~]# ls
anaconda-ks.cfg  bbb.txt  folder
[root@localhost ~]# ln bbb.txt folder/ccc.txt
[root@localhost ~]# ll folder/
總用量 0
-rw-r--r--. 2 root root 0 Nov 13 18:08 ccc.txt
[root@localhost ~]# ll bbb.txt 
-rw-r--r--. 2 root root 0 Nov 13 18:08 bbb.txt

軟連結:

[root@localhost ~]# mkdir folder_b
[root@localhost ~]# ln -s bbb.txt folder_b/eee.txt
[root@localhost ~]# ll 
總用量 4
-rw-------. 1 root root 2752 Nov 10 02:51 anaconda-ks.cfg
-rw-r--r--. 2 root root    0 Nov 13 18:10 bbb.txt
drwxr-xr-x. 2 root root   20 Nov 13 18:09 folder
drwxr-xr-x. 2 root root   20 Nov 13 18:11 folder_b
[root@localhost ~]# ll folder_b
總用量 0
lrwxrwxrwx. 1 root root 7 Nov 13 18:11 eee.txt -> bbb.txt
[root@localhost ~]# rm -rf bbb.txt 
[root@localhost ~]# ll folder_b
總用量 0
lrwxrwxrwx. 1 root root 7 Nov 13 18:11 eee.txt -> bbb.txt

刪除了原檔案,軟連結的箭頭目標為紅色一閃一閃,表示找不到目標檔案。

常用目錄作用

[root@localhost ~]# ls /
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  temp  tmp  usr  var

說明:

  • / 根目錄
  • /bin 命令儲存目錄(普通使用者許可權)
  • /sbin 命令儲存目錄(root許可權)
  • /boot 啟動目錄,包含啟動相關檔案,和開機有關
  • /dev 裝置檔案儲存目錄
  • /etc 配置檔案儲存目錄
  • /home 普通使用者家目錄
  • /lib 系統庫儲存目錄
  • /mnt 系統掛載目錄
  • /media 掛載目錄(常用於光碟掛載)
  • /root 超級使用者家目錄
  • /tmp 臨時目錄
  • /proc 直接寫入記憶體的
  • /sys 直接寫入記憶體的
  • /usr 系統軟體資源目錄
  • /var 系統相關文件內容

相關文章