目錄文件命令
- 一、概述
- 二、文件目錄
- 1. 目錄信息
- 1. pwd:顯示當前目錄
- 2. cd :切換目錄
- 3. ls:顯示目錄中的文件信息
- 2. 操作目錄
- 1. mkdir : 創建目錄
- 2. rm : 刪除目錄
- 3. mv : 移動、重命名目錄
- 4. cp :拷貝命令
一、概述
這里記錄一下操作工作目錄所涉及到的相關命令,工作目錄指的是用戶當前在系統中所處的位置。
常用的包括創建目錄、刪除目錄、重命名目錄等等,同樣的是對文件也是一樣。
二、文件目錄
1. 目錄信息
記錄對目錄信息的顯示查看命令
1. pwd:顯示當前目錄
pwd 命令用于顯示用戶當前所處的工作目錄,輸出的目錄是以絕對目錄來確定的。
zhy@zhy-Uos:~$ pwd
/home/zhy
2. cd :切換目錄
cd 命令用于切換工作路徑,格式為“cd /path”。
這個命令應該是最常用的一個 Linux 命令了。我們可以通過 cd 命令切換到不同的工作目錄。支持相對路徑和絕對路徑。
幾個特殊的 路徑有:
cd - # 切換到上一次所在的目錄
cd .. # 切換到上級目錄
cd ~ # 切換到 /home 目錄
下面給出常見的用法
zhy@zhy-Uos:~$ cd /etc/apt/ # 切換絕對路徑
zhy@zhy-Uos:/etc/apt$ cd ../ppp/ # 切換相對路徑,與apt相同層級的ppp目錄
zhy@zhy-Uos:/etc/ppp$
3. ls:顯示目錄中的文件信息
ls 命令用于顯示目錄中的文件信息,格式為“ls [選項] [文件] ”。
使用 ls 命令的“-a”參數看到全部文件(包括隱藏文件),使用“-l”參數可以查看文件的屬性、大小等詳細信息。
合并兩個參數整合之后,再執行 ls 命令即可查看當前目錄中的所有文件并輸出這些文件的屬性信息:
下面這個命令也是最常見的查看文件夾的目錄信息
zhy@zhy-Uos:/etc$ ls -al
總用量 1604
drwxr-xr-x 168 root root 12288 5月 20 09:02 .
drwxr-xr-x 24 root root 4096 5月 16 16:19 ..
drwxr-xr-x 4 root root 4096 2月 5 21:49 acpi
-rw-r--r-- 1 root root 2981 2月 5 21:47 adduser.conf
-rw-r--r-- 1 root root 46 3月 20 05:36 adjtime
-rw-r--r-- 1 root root 185 4月 23 13:16 aliases
drwxr-xr-x 3 root root 4096 2月 5 21:49 alsa
drwxr-xr-x 2 root root 12288 5月 16 16:20 alternatives
drwxr-xr-x 3 root root 4096 3月 21 09:05 apache2
drwxr-xr-x 3 root root 4096 2月 5 21:50 apm
.....
2. 操作目錄
1. mkdir : 創建目錄
創建目錄可以一次性創建多個,還可以嵌套創建【以最后一個目錄名,如果父目錄不存在也會創建】
mkdir -p Debug/f1 # 嵌套創建
cd Debug
mkdir -p f2 f2/d1 f3 # 創建多個文件夾
2. rm : 刪除目錄
查看對應的幫助就知道常用的有幾個了
rm -rf Debug # 遞歸刪除整個文件夾,且不提示是否確認刪除rm 1.txt # 刪除單個文件
zhy@zhy-Uos:~/Debug/f2$ rm --help
用法:rm [選項]... [文件]...
Remove (unlink) the FILE(s).-f, --force ignore nonexistent files and arguments, never prompt-i prompt before every removal-I prompt once before removing more than three files, orwhen removing recursively; less intrusive than -i,while still giving protection against most mistakes--interactive[=WHEN] prompt according to WHEN: never, once (-I), oralways (-i); without WHEN, prompt always--one-file-system 遞歸刪除一個層級時,跳過所有不符合命令行參數的文件系統上的文件--no-preserve-root do not treat '/' specially--preserve-root[=all] do not remove '/' (default);with 'all', reject any command line argumenton a separate device from its parent-r, -R, --recursive remove directories and their contents recursively-d, --dir remove empty directories-v, --verbose explain what is being done--help 顯示此幫助信息并退出--version 顯示版本信息并退出
3. mv : 移動、重命名目錄
Linux 中沒有重命名的命令,用的是 mv命令,如果移動的終點目錄,就是移動文件,如果是文件,那就是重命名
# 移動目錄 到 新目錄
mv f3 f1 # 移動 f3 目錄到 f1 目錄# 移動文件 到 目錄
mv f1/1.txt f1/f3/ # 移動 f1 中的1.txt 目錄到 f1 下的 f3 目錄# 重命名文件
mv f1/1.txt f1/f3/2.txt # 移動 f1 中的1.txt 目錄到 f1 下的 f3 目錄
在使用這個命令的時候,我們也可以帶上常用的參數來指定一些強制覆蓋等操作
下面是mv 的詳細說明
zhy@zhy-Uos:~$ mv --help
用法:mv [選項]... [-T] 源文件 目標文件或:mv [選項]... 源文件... 目錄或:mv [選項]... -t 目錄 源文件...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.必選參數對長短選項同時適用。--backup[=CONTROL] 為每個已存在的目標文件創建備份-b 類似--backup 但不接受參數-f, --force 覆蓋前不詢問-i, --interactive 覆蓋前詢問-n, --no-clobber 不覆蓋已存在文件
如果您指定了-i、-f、-n 中的多個,僅最后一個生效。--strip-trailing-slashes 去掉每個源文件參數尾部的斜線-S, --suffix=SUFFIX 替換常用的備份文件后綴-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY-T, --no-target-directory treat DEST as a normal file-u, --update move only when the SOURCE file is newerthan the destination file or when thedestination file is missing-v, --verbose explain what is being done-Z, --context set SELinux security context of destinationfile to default type--help 顯示此幫助信息并退出--version 顯示版本信息并退出
....