不可錯過的cd命令,你知道cd -、cd ~和切換空格目錄等內(nèi)容嗎?
cd 命令詳細(xì)介紹
cd (change directory)改變目錄,即,切換目錄。用于從當(dāng)前目錄切換到另一個指定的目錄。
cd 后面的目錄路徑,可以是相對目錄,也可以是絕對目錄。
./ 相對路徑,表示當(dāng)前目錄;../ 相對路徑,表示上一級目錄。/xxx 反斜杠開頭的表示絕對路徑。
/表示根目錄,所有的內(nèi)容都從根目錄開始;/root表示root用戶的家目錄。對于家目錄(
Home Directory),除了root用戶之外,所有用戶的家目錄都位于/home/下(/home/)。
Absolute Path(絕對路徑):從根目錄開始的路徑。
Relative Path(相對路徑):相對于當(dāng)前位置開始的路徑,如
./、../。
cd切換命令演示
Linux中涉及到目錄的所有命令,都可以通過 tab 鍵對路徑補(bǔ)全。即輸入路徑名的一部分,按 tab 鍵補(bǔ)全其余名稱。
cd ../ 或 cd ../ 切換到相對當(dāng)前的上一級目錄
[root_test@VM_0_15_centos test]$ cd ../
[root_test@VM_0_15_centos ~]$
復(fù)制代碼./開頭,或直接以目錄名(文件名)開頭,表示當(dāng)前目錄。
如下切換到當(dāng)前目錄下的test目錄中。
[root_test@VM_0_15_centos ~]$ cd test/
[root_test@VM_0_15_centos test]$ cd ../
[root_test@VM_0_15_centos ~]$ cd ./test/
[root_test@VM_0_15_centos test]$
復(fù)制代碼切換到絕對目錄 /etc/my.cnf.d。
[root_test@VM_0_15_centos test]$ cd /etc/my.cnf.d
[root_test@VM_0_15_centos my.cnf.d]$
復(fù)制代碼help 查看 cd 命令的幫助
cd命令是shell的內(nèi)置命令,使用 man 查看時會顯示 shell 的幫助信息,而不是 cd 的。
$ man cd
BASH_BUILTINS(1) General Commands Manual BASH_BUILTINS(1)
NAME
bash, :, ., [, alias, bg, bind, break, builtin, caller, cd, command,
compgen, complete, compopt, continue, declare, dirs, disown, echo,
enable, eval, exec, exit, export, false, fc, fg, getopts, hash, help,
history, jobs, kill, let, local, logout, mapfile, popd, printf, pushd,
pwd, read, readonly, return, set, shift, shopt, source, suspend, test,
times, trap, true, type, typeset, ulimit, umask, unalias, unset, wait -
bash built-in commands, see bash(1)
BASH BUILTIN COMMANDS
......
復(fù)制代碼
help cd 查看幫助命令:
$ help cd
cd: cd [-L|[-P [-e]]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
......
復(fù)制代碼cd - 切換到上一次的目錄
cd - 命令用于直接切換到上一次的目錄,即兩個目錄之前來回切換,非常方便。
[root_test@VM_0_15_centos ~]$ cd /etc/sysconfig/network-scripts/
[root_test@VM_0_15_centos network-scripts]$ pwd
/etc/sysconfig/network-scripts
[root_test@VM_0_15_centos network-scripts]$ cd -
/home/root_test
[root_test@VM_0_15_centos ~]$ cd -
/etc/sysconfig/network-scripts
[root_test@VM_0_15_centos network-scripts]$
復(fù)制代碼cd ~ 或 cd $home 返回家目錄
cd ~ 命令用于快速回到用戶家目錄(Home Directory)。對于 root 用戶為/root;對于其他用戶為/home/user_name。
[root_test@VM_0_15_centos tmp]$ pwd
/var/tmp
[root_test@VM_0_15_centos tmp]$ cd ~
[root_test@VM_0_15_centos ~]$ pwd
/home/root_test
復(fù)制代碼
$或#前面的 ~ 即表示當(dāng)前是家目錄。~(波浪號——tilde)是家目錄的縮寫。
cd $home 同樣用于回到家目錄。
root 用戶使用 cd ~username 切換到指定用戶的家目錄
cd ~username 或 cd ~username/ 用于切換到指定用戶的家目錄,但是,只能root用戶執(zhí)行,具有管理員權(quán)限的用戶,使用 sudo 執(zhí)行也無效。
比如,root_test用戶,嘗試切換到 root_test1 和 root_test2 用戶,均失敗!
[root_test@VM_0_15_centos ~]$ cd ~root_test2
-bash: cd: /home/root_test2: Permission denied
[root_test@VM_0_15_centos ~]$ sudo cd ~root_test1
[root_test@VM_0_15_centos ~]$ pwd
/home/root_test
[root_test@VM_0_15_centos ~]$ sudo cd ~root_test2
[root_test@VM_0_15_centos ~]$ pwd
/home/root_test
復(fù)制代碼root用戶可以正常切換。
[root@VM_0_15_centos ~]# pwd
/root
[root@VM_0_15_centos ~]# cd ~root_test2
[root@VM_0_15_centos root_test2]# pwd
/home/root_test2
復(fù)制代碼三種方法切換到有空格的目錄名中
如下,存在一個有空格的目錄 my space dir:
$ ls -l
total 12
-rw-rw-r-- 1 root_test root_test 2 Oct 29 08:52 123
drwxrwxr-x 2 root_test root_test 4096 Oct 29 08:52 my
drwxrwxr-x 2 root_test root_test 4096 Oct 30 10:28 my space dir
-rw-rw-r-- 1 root_test root_test 0 Oct 26 21:52 my.txt
復(fù)制代碼一下三種方式均可使用 tab 鍵補(bǔ)全。
使用 \ 轉(zhuǎn)義字符
[root_test@VM_0_15_centos test]$ cd my\ space\ dir/
[root_test@VM_0_15_centos my space dir]$ pwd
/home/root_test/test/my space dir
復(fù)制代碼使用雙引號包含目錄名
[root_test@VM_0_15_centos test]$ cd "my space dir"/
[root_test@VM_0_15_centos my space dir]$
復(fù)制代碼使用單引號
[root_test@VM_0_15_centos test]$ cd 'my space dir'/
[root_test@VM_0_15_centos my space dir]$
復(fù)制代碼另,
cd !$,表示把上個命令的參數(shù)作為 cd 參數(shù)使用
作者:代碼迷途
鏈接:https://juejin.cn/post/7024685258851221511
來源:稀土掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。

