linux每日命令(2):cd命令

聽風。發表於2018-10-22

1.命令格式:

cd   [目錄名]

2.命令功能

切換當前目錄至 [目錄名]

3. 常用範例

1.進入系統根目錄

命令:

cd  / 

說明:進入系統根目錄,上面命令執行完後拿ls命令看一下,當前目錄已經到系統根目錄了

輸出:

hc@hc-virtual-machine:~$ pwd
/home/hc
hc@hc-virtual-machine:~$ cd /
hc@hc-virtual-machine:/$ 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  ..

輸出:

hc@hc-virtual-machine:~/PycharmProjects$ pwd
/home/hc/PycharmProjects
hc@hc-virtual-machine:~/PycharmProjects$ cd ..
hc@hc-virtual-machine:~$ pwd
/home/hc

3.進入當前目錄的父目錄的父目錄

命令:

cd  ../..

輸出:

hc@hc-virtual-machine:~/PycharmProjects$ pwd
/home/hc/PycharmProjects
hc@hc-virtual-machine:~/PycharmProjects$ cd ../..
hc@hc-virtual-machine:/home$ pwd
/home

4. 進入當前使用者主目錄

“當前使用者主目錄”和“系統根目錄”是兩個不同的概念。進入當前使用者主目錄有兩個方法。

命令1:

cd

直接輸入cd,然後回車

輸出:

hc@hc-virtual-machine:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
hc@hc-virtual-machine:~/PycharmProjects/my_test$ cd 
hc@hc-virtual-machine:~$ pwd
/home/hc

上面的命令是我在非root使用者下操作的,現在我切換到root使用者下再操作一遍

hc@hc-virtual-machine:~/PycharmProjects/my_test$ sudo su
root@hc-virtual-machine:/home/hc/PycharmProjects/my_test# pwd
/home/hc/PycharmProjects/my_test
root@hc-virtual-machine:/home/hc/PycharmProjects/my_test# cd 
root@hc-virtual-machine:~# pwd
/root

命令2:

cd ~

輸出

hc@hc-virtual-machine:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
hc@hc-virtual-machine:~/PycharmProjects/my_test$ cd ~
hc@hc-virtual-machine:~$ pwd
/home/hc

5. 跳轉到指定目錄

命令

 cd /home/hc/PycharmProjects/my_test/

輸出

hc@hc-virtual-machine:/$ pwd
/
hc@hc-virtual-machine:/$ cd /home/hc/PycharmProjects/my_test/
hc@hc-virtual-machine:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test

6.返回進入此目錄之前所在的目錄

命令:

cd -

說明:自動跳轉到進入此目錄之前所在的目錄,並輸出所在目錄名稱

輸出:

hc@hc-virtual-machine:/$ pwd
/
hc@hc-virtual-machine:/$ cd /home/hc/PycharmProjects/my_test/
hc@hc-virtual-machine:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
hc@hc-virtual-machine:~/PycharmProjects/my_test$ cd -
/
hc@hc-virtual-machine:/$ pwd
/

7. 把上個命令的引數作為cd引數使用

命令:

cd !$

輸出:

hc@hc-virtual-machine:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
hc@hc-virtual-machine:~/PycharmProjects/my_test$ ls /opt/
google
hc@hc-virtual-machine:~/PycharmProjects/my_test$ cd !$
cd /opt/
hc@hc-virtual-machine:/opt$ pwd
/opt

相關文章