Linux cd command: change working directory path.
Linux cd command Function Description
Use the cd command to change the path to the user’s working directory. The working directory path can use either absolute or relative path names, with absolute paths starting at / (root) and then sequentially going to the desired directory, and relative paths starting at the current directory.
Linux cd Command Syntax
cd [Option] [Directory]
The meaning of each option in the command is shown in the table.
Option | Description |
---|---|
-P |
If it is a link path, enter the source physical path of the link path |
Linux cd command Examples
cd # Go to the user home directory.
cd / # Go to the root directory
cd ~ # Go to the user home directory.
cd .. # return to the parent directory (if the current directory is "/", it will still be in "/" after execution; "..." is the meaning of the parent directory).
cd ../.. # Return to the upper two levels of the directory.
cd !$ # Use the parameters of the previous command as cd parameters.
Change the user working directory path to /root/apidemos
cd /root/apidemos
Output:
Change the location of the user’s working directory path to the parent directory of the current directory
[root@rhel etc]# cd ..
[root@rhel /]# pwd/
// You can see that the path to the working directory has been changed to the parent directory "/" of the current directory
Change the location of the user’s working directory path to the user’s home directory
[root@rhel /]# cd ~
[root@rhel ~]# pwd
/root
Change the location of the user’s working directory path to user zhangsan’s home directory
[root@rhel ~]# cd ~zhangsan
[root@rhel zhangsan]# pwd
/home/zhangsan
// You can see that the path to the user's working directory has been changed to user zhangsan's home directory /home/zhangsan
Change the location of the user’s working directory path to /etc/init.d or, if it is a linked path, to the source physical path
[root@rhel ~]# cd -P /etc/init.d
[root@rhel init.d]# pwd
/etc/rc.d/init.d
// The current working directory path is /etc/rc.d/init.d
More
Note on switching to the previous working directory
cd -
# command will first display the target directory to switch to and then enter.
cd ${OLDPWD}
# command will switch directly to the previous working directory.
About CDPATH
# Set the desktop folder as the value of CDPATH.
CDPATH='~/Desktop'
# Assuming that there are no test3 folders under ~ and ~/Desktop, the paths we are going to demonstrate next, now create them.
mkdir ~/test3
mkdir ~/Desktop/test3
# Go to the ~ directory.
cd ~
# Go to the test3 directory.
cd test3
# When executed, it displays ~/Desktop/test3 and goes to that directory instead of the test3 directory in the ~ directory.
# If the CDPATH value exists, then priority is given to finding and entering the first successful match in the CDPATH, if all fail then the current directory is tried last.
About cdable_vars
# Open the option.
shopt -s cdable_vars
# Assume that the current path and the CDPATH do not have a directory named new_var.
new_var='~/Desktop'
# Try to enter.
cd new_var
# Close the option.
shopt -u cdable_vars