linux每日命令(2):cd命令
阿新 • • 發佈:2018-12-16
1.命令格式:
cd [目錄名]
2.命令功能
切換當前目錄至 [目錄名]
3. 常用範例
1.進入系統根目錄
命令:
cd /
說明:進入系統根目錄,上面命令執行完後拿ls命令看一下,當前目錄已經到系統根目錄了
輸出:
[email protected]:~$ pwd /home/hc [email protected]:~$ cd / [email protected]:/$ ls bin dev initrd.img lib64 mnt root snap sys var boot etc initrd.img.old lost+found opt run srv tmp vmlinuz cdrom home lib media proc sbin swapfile usr
pwd的作用是查詢當前所在目錄
2. 返回父級目錄
命令:
cd ..
輸出:
[email protected]:~/PycharmProjects$ pwd
/home/hc/PycharmProjects
[email protected]:~/PycharmProjects$ cd ..
[email protected]:~$ pwd
/home/hc
3.進入當前目錄的父目錄的父目錄
命令:
cd ../..
輸出:
[email protected]:~/PycharmProjects$ pwd
/home/hc/PycharmProjects
[email protected] :~/PycharmProjects$ cd ../..
[email protected]:/home$ pwd
/home
4. 進入當前使用者主目錄
“當前使用者主目錄”和“系統根目錄”是兩個不同的概念。進入當前使用者主目錄有兩個方法。
命令1:
cd
直接輸入cd,然後回車
輸出:
[email protected]:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
[email protected]:~/PycharmProjects/my_test$ cd
[email protected] :~$ pwd
/home/hc
上面的命令是我在非root使用者下操作的,現在我切換到root使用者下再操作一遍
[email protected]:~/PycharmProjects/my_test$ sudo su
[email protected]:/home/hc/PycharmProjects/my_test# pwd
/home/hc/PycharmProjects/my_test
[email protected]:/home/hc/PycharmProjects/my_test# cd
[email protected]:~# pwd
/root
命令2:
cd ~
輸出
[email protected]:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
[email protected]:~/PycharmProjects/my_test$ cd ~
[email protected]:~$ pwd
/home/hc
5. 跳轉到指定目錄
命令
cd /home/hc/PycharmProjects/my_test/
輸出
[email protected]:/$ pwd
/
[email protected]:/$ cd /home/hc/PycharmProjects/my_test/
[email protected]:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
6.返回進入此目錄之前所在的目錄
命令:
cd -
說明:自動跳轉到進入此目錄之前所在的目錄,並輸出所在目錄名稱
輸出:
[email protected]:/$ pwd
/
[email protected]:/$ cd /home/hc/PycharmProjects/my_test/
[email protected]:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
[email protected]:~/PycharmProjects/my_test$ cd -
/
[email protected]:/$ pwd
/
7. 把上個命令的引數作為cd引數使用
命令:
cd !$
輸出:
[email protected]:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
[email protected]:~/PycharmProjects/my_test$ ls /opt/
google
[email protected]:~/PycharmProjects/my_test$ cd !$
cd /opt/
[email protected]:/opt$ pwd
/opt