linux Daily Command (2): cd Command

Keywords: Linux sudo Google

1. Command format:

cd   [Directory name]

2. Command Function

Switch the current directory to [directory name]

3. Common examples

1. Enter the system root directory

Order:

cd  / 

Description: Enter the system root directory. After executing the above command, take a look at the ls command. The current directory has reached the system root directory.

Output:

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

The function of pwd is to query the current directory

2. Return to the parent directory

Order:

cd  ..

Output:

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

3. The parent directory of the parent directory entering the current directory

Order:

cd  ../..

Output:

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

4. Enter the current user home directory

"Current User Home Directory" and "System Root Directory" are two different concepts. There are two ways to access the current user's home directory.

Order 1:

cd

Enter cd directly and return

Output:

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

The above command was operated by me under the non-root user. Now I switch to the root user and do it again.

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

Order 2:

cd ~

output

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. Jump to the specified directory

command

 cd /home/hc/PycharmProjects/my_test/

output

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. Return to the directory where you were before entering this directory

Order:

cd -

Description: Automatically jump to the directory before entering this directory, and output the name of the directory.

Output:

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. Use the parameters of the previous command as cd parameters

Order:

cd !$

Output:

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

Posted by Mr_J on Sun, 27 Jan 2019 15:18:15 -0800