The Linux cd command can be said to be the most basic command statement in Linux, and other command statements to be operated are based on the use of the cd command. Therefore, to learn common Linux commands, you must first learn how to use the cd command well.
1. Command format:
cd [ directory name ]
2. Command function:
Change the current directory to dirName
3. Common examples
3.1 Example 1: Enter the system root directory
Order:
cd /
output:
[root@localhost ~]# cd /
Description: Enter the system root directory. After the above command is executed, take a look with the ls command. The current directory has reached the system root directory.
Order:
cd .. or cd .. //
output :
2 /opt/soft
3 [root@localhost soft]# cd ..
4 [root@localhost opt]# cd ..//
5 [root@localhost /]# pwd
6 /
Description: Enter the root directory of the system, you can use " cd .. " to go back all the way, you can reach the root directory
Order:
cd ../.. //
output:
2 /opt/soft
3 [root@localhost soft]# cd ../.. //
4 [root@localhost /]# pwd
5 /
6 [root@localhost /]#
Description: Use the cd command to enter the parent directory of the parent directory of the current directory.
Example 2: Use the cd command to enter the current user's home directory
"Current user home directory" and "system root directory" are two different concepts. There are two ways to enter the current user's home directory.
Command 1:
cd
output:
2 /opt/soft
3 [root@localhost soft]# cd
4 [root@localhost ~]# pwd
5 /root
Command 2 :
cd ~
output:
2 [root@localhost soft]# pwd
3 /opt/soft
4 [root@localhost soft]# cd ~
5 [root@localhost ~]# pwd
6 /root
Example 3 : Jump to the specified directory
Order:
cd /opt/soft
output:
2 [root@localhost soft]# pwd
3 /opt/soft
4 [root@localhost soft]# cd jdk1.6.0_16/
5 [root@localhost jdk1.6.0_16]# pwd
6 /opt/soft/jdk1.6.0_16
7 [root@localhost jdk1.6.0_16]#
illustrate:
Jump to the specified directory, start from the root directory, add / before the directory name, and write the name of the subdirectory in the current directory directly
Example 4: Return to the directory where you were before entering this directory
Order:
cd -
output:
2 /opt/soft
3 [root@localhost soft]# cd -
4 /root
5 [root@localhost ~]# pwd
6 /root
7 [root@localhost ~]# cd -
8 /opt/soft
9 [root@localhost soft]#
Example 5: Use the parameter of the previous command as the cd parameter.
Order:
cd !$
output:
2 cd -
3 /root
4 [root@localhost ~]# cd !$
5 cd -
6 /opt/soft
7 [root@localhost soft]#