# 使用命令行工具管理文件
## 目標
能夠在各種目錄中創建、復制、鏈接、移動和刪除文件與子目錄。
## 命令行文件管理
文件管理涉及創建、刪除、復制和移動文件。此外,也可以創建、刪除、復制和移動目錄,以幫助有條理地整理文件。在命令行執行文件管理時,要求知道當前的工作目錄,這樣才能為直接任務選用絕對或相對路徑語法以實現最該效率。
### 文件管理器命令
| 文件管理命令 | 單一來源 | 多來源 |
| --- | --- | --- |
| 復制文件 | cp file1 file2 | cp file1 file2 file3 dir④ |
| 移動文件 | mv file1 file2① | mv file1 file2 file3 dir④ |
| 刪除文件 | rm file1 | rm -f file1 file2 file3⑤ |
| 創建目錄 | mkdir dir | mkdir -p part1/part2/dir⑥ |
| 復制目錄 | cp -r dir1 dir2② | cp -r dir1 dir2 dir3 dir4④ |
| 移動目錄 | mv dir1 dir2③ | mv dir1 dir2 dir3 dir4④ |
| 刪除目錄 | rm -r dir1② | rm -rf dir1 dir2 dir3⑤ |
---
| 注: | ①結果為重命名。<br>②需要使用“遞歸”選項來處理來源目錄。<br>③如果 dir2 存在,則結果為移動。如果 dir2 不存在,則結果為重命名。<br>④最后一個參數必須是目錄。<br>⑤請謹慎使用“force”選項,系統將不會提示您確認操作。<br>⑥使用“創建父級”選項時應小心;無法捕獲鍵入錯誤。 |
| --- | --- |
#### 創建目錄
mkdir 命令創建一個或多個目錄或子目錄;如果文件名已存在,或者嘗試在不存在的父目錄中創建目錄,將生成錯誤。-p 父級選項將為請求的目標位置創建缺失的父目錄。使用 mkdir -p 時應小心,因為意外拼寫錯誤將創建不需要的目錄,而不會生成錯誤信息。
在以下示例中,用戶嘗試使用 mkdir 在現有的 Videos 目錄中創建名為 Watched 的子目錄,但是卻寫錯目錄名稱。
~~~
[student@desktopX~]$ mkdir Video/Watched
mkdir: cannot create directory 'Video/Watched': No such file or directory
~~~
mkdir 運行失敗,因為將 Videos 拼寫錯誤,而且目錄 Video 也不存在。假如用戶是將 mkdir 與 -p 選項結合使用,則不會出現錯誤,而且用戶會得到兩個目錄:Videos 和 Video,同時會在錯誤的位置中創建子目錄 Watched。
~~~
[student@desktopX~]$ mkdir Videos/Watched
[student@desktopX~]$ cd Documents
[student@desktopX Documents]$ mkdir projectX ProjectY
[student@desktopX Documents]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3
[student@desktopX Documents]$ cd
[student@desktopX~]$ ls -R Videos Documents
Documents:
ProjectX ProjectY Thesis thesis_chapter1.odf thesis_chapter2.odf
Documents/ProjectX:
Documents/ProjectY:
Documents/Thesis:
Chapter1 Chapter2 Chapter3
Documents/Thesis/Chapter1:
Documents/Thesis/Chapter2:
Documents/Thesis/Chapter3:
Videos:
blockbusterl.ogg blockbuster2.ogg Watched
Videos/Watched:
[student@desktopX~]$
~~~
最后一個 mkdir 通過一個命令創建了三個 ChapterN 子目錄。-p 父級消息創建缺少的父目錄 Thesis。
#### 復制文件
cp 命令復制一個或多個文件,成為新的獨立文件。其語法允許將一個現有文件復制為當前或另一目錄中的一個新文件,或者將多個文件復制到另一目錄中。在任何目標位置上,新文件名都必須唯一。如果新文件名不唯一,那么復制命令將覆蓋現有文件。
~~~
[student@desktopX~]$ cd Videos
[student@desktopX Videos]$ cp blockbusterl.ogg blockbuster3.ogg
[student@desktopX Videos]$ ls -l
total 0
-rw-rw-r--. student student 0 Feb 8 16:23 blockbuster1.ogg
-rw-rw-r--. student student 0 Feb 8 16:24 blockbuster2.ogg
-rw-rw-r--. student student 0 Feb 8 19:02 blockbuster3.ogg
drwxrwxr-x. student student 4096 Feb 8 23:35 Watched
[student@desktopX Videos]$
~~~
在通過一個命令復制多個文件時,最后一個參數必須為目錄。復制的文件在新的目錄中保留其原有名稱。目標位置上存在的沖突文件名可能會被覆蓋。為防止用戶意外覆蓋帶有內容的目錄,多文件 cp 命令將忽略指定為來源的目錄。復制帶有內容的非空目錄要求使用 -r 遞歸選項。
~~~
[student@desktopX Videos]$ cd ../Documents
[student@desktopX Documents]$ cp thesis_chapter1.odf thesis_chapter2.odf Thesis ProjectX
cp: omitting directory 'Thesis'
[student@desktopX Documents]$ cp -r Thesis ProjectX
[student@desktopX Documents]$ cp thesis_chapter2.odf Thesis/Chapter2/
[student@desktopX Documents]$ ls -R
.:
ProjectX ProjectY Thesis thesis_Chapter1.odf thesis_chapter2.odf
./ProjectX:
Thesis thesis_Chapter1.odf thesis_Chapter2.odf
./ProjectX/Thesis:
./ProjectY:
./Thesis:
Chapter1 Chapter2 Chapter3
./Thesis/Chapter1:
./Thesis/Chapter2:
thesis_Chapter2.odf
./Thesis/Chapter3:
[student@desktopX Documents]$
~~~
在第一個 cp 命令中,Thesis 未能被復制,但 thesis_chapter1.odf 和 thesis_chapter2.odf 復制成功。使用 -r 遞歸選項時,復制 Thesis 成功。
#### 移動文件
mv 命令在同一目錄中重命名文件,或者將文件重新放到新的目錄中。文件內容保持不變。將文件移動另一文件系統時要求通過復制源文件創建新的文件,然后再刪除源文件。盡管通常對用戶透明,但移動大型文件所需時間可能會明顯較長。
~~~
[student@desktopX Videos]$ cd ../Documents
[student@desktopX Documents]$ ls -l
total 0
-rw-rw-r--. student student 0 Feb 8 16:24 thesis_chapter1.odf
-rw-rw-r--. student student 0 Feb 8 16:14 thesis_chapter2.odf
[student@desktopX Documents]$ mv thesis_chapter2.odf thesis_chapter2_reviewed.odf
[student@desktopX Documents]$ mv thesis_chapter1.odf Thesis/Chapter1
[student@desktopX Documents]$ ls -lR
.:
total 16
drwxrwxr-x. 2 student student 4096 Feb 11 11:58 ProjectX
drwxrwxr-x. 2 student student 4096 Feb 11 11:55 ProjectY
drwxrwxr-x. 5 student student 4096 Feb 11 11:56 Thesis
-rw-rw-r--. 1 student student 0 Feb 11 11:54 thesis_chapter2_reviewed.odf
./ProjectX:
total 0
-rw-rw-r--. 1 student student 0 Feb 11 11:58 thesis_chapter1.odf
-rw-rw-r--. 1 student student 0 Feb 11 11:58 thesis_chapter2.odf
./ProjectX/Thesis:
total 0
./ProjectY:
total 0
./Thesis:
total 12
drwxrwxr-x. 2 student student 4096 Feb 11 11:59 Chapter1
drwxrwxr-x. 2 student student 4096 Feb 11 11:56 Chapter2
drwxrwxr-x. 2 student student 4096 Feb 11 11:56 Chapter3
./Thesis/Chapter1:
total 0
-rw-rw-r--. 1 student student 0 Feb 11 11:54 thesis_chapter1.odf
./Thesis/Chapter2:
total 0
-rw-rw-r--. 1 student student 0 Feb 11 11:54 thesis_chapter2.odf
./Thesis/Chapter3:
total 0
[student@desktopX Documents]$
~~~
第一個 mv 命令時重命名文件的示例。第二個命令會導致文件重新放置到另外目錄中。
#### 刪除文件和目錄
rm 的默認語法將刪除文件,而不是目錄。要刪除目錄以及其下可能存在的許多子目錄和文件,需要使用 -r 遞歸選項。沒有命令行取消刪除功能,也沒有可以從中恢復的垃圾箱。
~~~
[student@desktopX Documents]$ pwd
/home/student/Documents
[student@desktopX Documents]$ rm thesis_chapter2_reviewed.odf
[student@desktopX Documents]$ rm Thesis/Chapter1
rm: cannot remove 'Thesis/Chapter1': Is a directory
[student@desktopX Documents]$ rm -r Thesis/Chapter1
[student@desktopX Documents]$ ls -l Thesis
total 8
drwxrwxr-x. 2 student student 4096 Feb 11 12:47 Chapter2
drwxrwxr-x. 2 student student 4096 Feb 11 12:48 Chapter3
[student@desktopX Documents]$ rm -ri Thesis
rm: descend into directory 'Thesis'? y
rm: descend into directory 'Thesis/Chapter2'? y
rm: remove regular empty file 'Thesis/Chapter2/thesis_chapter2.opf' y
rm: remove directory 'Thesis/Chapter2'? y
rm: remove directory 'Thesis/Chapter3'? y
rm: remove directory 'Thesis'? y
[student@desktopX Documents]$
~~~
在 rm 未能刪除 Chapter1 目錄后,-r 遞歸選項成功執行了刪除。最后一個 rm 命令首先解析到各個子目錄,逐一刪除其中包含的文件,然后刪除各個現在為空的目錄。如果使用 -i,則將以交互方式提示每個刪除操作。這基本上與 -f 是相反的,后者將強制進行刪除而不會提示用戶。
rmdir 命令僅刪除空的目錄。刪除的目錄無法取消刪除。
~~~
[student@desktopX Documents]$ pwd
/home/student/Documents
[student@desktopX Documents]$ rmdir ProjectY
[student@desktopX Documents]$ rmdir ProjectX
rmdir: failed to remove 'ProjectX': Directory not empty
[student@desktopX Documents]$ rm -r ProjectX
[student@desktopX Documents]$ ls -lR
.:
total 0
[student@desktopX Documents]$
~~~
rmdir 命令未能刪除非空目錄 ProjectX,但 rm -r 成功執行了刪除。
### 參考
cp、mkdir、mv 和 rmdir man page