歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(2)cd命令

每天一個Linux命令(2)cd命令

日期:2017/3/3 14:05:10   编辑:Linux技術

cd命令用來切換工作目錄至dirname。 其中dirName表示法可為絕對路徑或相對路徑。若目錄名稱省略,則變換至使用者的home directory(也就是剛login時所在的目錄)。另外,~也表示為home directory的意思,.則是表示目前所在的目錄,..則表示目前目錄位置的上一層目錄。

[b] 1.用法:[/b] cd (選項) [目錄]

[b] 2.功能:[/b] 切換當前目錄至dirName

3.選項: (1) -p 如果要切換到的目標目錄是一個符號連接,直接切換到符號連接指向的目標目錄

(2) -L 如果要切換的目標目錄是一個符號的連接,直接切換到字符連接名代表的目錄,而非符號連接所指向的目標目錄。 (3) - 當僅實用"-"一個選項時,當前工作目錄將被切換到環境變量"OLDPWD"所表示的目錄。[b]4.實例:[/b]

(1) cd 進入用戶主目錄;cd ~ 進入用戶主目錄;

[sunjimeng@localhost ~]$ cd /        /*剛開啟terminal終端,是用戶的主目錄*/   /* “cd /” 進入根目錄*/
[sunjimeng@localhost /]$ cd          /*cd命令默認進入用戶的主目錄*/
[sunjimeng@localhost ~]$             /*默認的主目錄是這樣的“~”*/

[sunjimeng@localhost ~]$ cd /        /*"cd"命令和"cd ~"可以達到一樣的功能*/
[sunjimeng@localhost /]$ cd ~
[sunjimeng@localhost ~]$
(2) cd - 返回進入此目錄之前所在的目錄;

[sunjimeng@localhost /]$ cd /home/sunjimeng                  /*由根目錄進入用戶目錄*/
[sunjimeng@localhost ~]$ ll                                  //列出用戶目錄下的目錄及文件
total 0
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Desktop
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Documents
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Downloads
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Videos
[sunjimeng@localhost ~]$ cd Desktop                         //選擇進入Desktop文件夾
[sunjimeng@localhost Desktop]$ cd -                         //"cd -"命令功能:(1)輸出之前的目錄名稱;(2)返回之前的目錄。
/home/sunjimeng
(3) cd .. 返回上級目錄(若當前目錄為“/“,則執行完後還在“/";".."為上級目錄的意思);cd ../.. 返回上兩級目錄;

[sunjimeng@localhost /]$ cd ..                       //在根目錄下返回上一級目錄,依然是根目錄
[sunjimeng@localhost /]$ cd /home/sunjimeng/Desktop  //進入自定義文件夾
[sunjimeng@localhost Desktop]$ cd ..                 //返回上一級 /home/sunjimeng,即用戶主目錄
[sunjimeng@localhost ~]$ cd ../..                    //返回上兩級目錄
[sunjimeng@localhost /]$
(4) cd !$ 把上個命令的參數作為cd參數使用。

[sunjimeng@localhost ~]$ cd /home/sunjimeng/Desktop  //進入用戶主目錄的桌面文件夾
[sunjimeng@localhost Desktop]$ cd !$                 //cd !$功能:(1)打印這個命令的解釋;(2)執行這個命令
cd /home/sunjimeng/Desktop
[sunjimeng@localhost Desktop]$
[sunjimeng@localhost Desktop]$ cd ..
[sunjimeng@localhost ~]$ cd !$
cd ..
[sunjimeng@localhost home]$

Copyright © Linux教程網 All Rights Reserved