Linuxstar
Published on

cd command-switch directory

Jonas

Jonas

2 min read
The cd command is the acronym of the word in "change directory", and its English definition is to change the directory, so the function of this command is to switch from the current directory to the specified directory. The path of the directory can be divided into absolute path and relative path. If the name of the directory is omitted, it will switch to the user's user directory (that is, the directory where the user just logged in). In addition, "~" also means the user directory, "." means the current directory, and ".." means the upper directory of the current directory location. Syntax: cd [parameter] [directory name]
Command parameters
-P If the target directory to be switched is a symbolic link, then directly switch to the target directory pointed to by the symbolic link
-L If the target directory of the switch is a symbolic link, then directly switch to the directory where the symbolic link name is located
- When only using the "-" option, the current directory will be switched to the directory corresponding to the value of the environment variable "OLDPWD"
~ Switch to the current user directory
.. Switch to the upper directory of the current directory location
Example
Switch the current working directory to the dir directory, and use the pwd command to view the current directory:
[root@linuxstar ~]# cd dir
[root@linuxstar dir]# pwd /root/dir
Use the "cd ~" and "cd .." commands to switch directories, and use the pwd command to view the current directory: Note: Use the "cd ~" command to directly switch to the current user directory, and "cd .." to switch to the upper level directory.
[root@linuxstar dir]# pwd/root/dir
[root@linuxstar dir]# cd ~
[root@linuxstar ~]# pwd/root
[root@linuxstar dir]# pwd/root/dir
[root@linuxstar dir]# cd ..
[root@linuxstar dir]# pwd/root
Use the "cd ../.." command to return to the upper two levels of directories:
[root@linuxstar dir_2]# pwd/root/dir/dir_1/dir_2
[root@linuxstar dir_2]# cd ../..
[root@linuxstar dir]# pwd/root/dir
Use the "cd" command to return to the current user directory. The "cd — "command returns to the previous directory:
[root@linuxstar dir_2]# pwd/root/dir/dir_1/dir_2
[root@linuxstar dir_2]# cd
[root@linuxstar ~]# pwd/root
[root@linuxstar ~]# cd -/root/dir/dir_1/dir_2
[root@linuxstar dir_2]# pwd/root/dir/dir_1/dir_2