TrumanWong

cd

Switch the user's current working directory.

Summary

cd [-L|[-P [-e]]] [dir]

The main purpose

  • Change working directory to dir. The representation of dir can be an absolute path or a relative path.
  • If the parameter dir is omitted, it defaults to the user's shell variable HOME.
  • If dir is specified as ~, it represents the user's shell variable HOME, . represents the current directory, and .. represents the upper-level directory of the current directory.
  • The environment variable CDPATH is one or more directories separated by colons. You can add the upper level of frequently visited directories to CDPATH to facilitate access to them; if dir starts with / then CDPATH ` will not be used.
  • When the shopt option cdable_vars is turned on, if dir does not exist in CDPATH nor in the current directory, it will be treated as a variable and its value will be read as the directory to be entered.

Parameters

dir (optional): Specifies the directory to switch to.

Options

-L (default) If the target directory to switch to is a symbolic link, switch to the directory of the symbolic link.
-P If the target directory to be switched to is a symbolic link, switch to the physical location directory it points to.
- The current working directory will be switched to the directory represented by the environment variable OLDPWD, which is the previous working directory.

return value

The return status is successful unless the specified directory cannot be entered.

example

cd # Enter the user's home directory;
cd / # Enter the root directory
cd ~ # Enter the user's home directory;
cd .. # Return to the upper-level directory (if the current directory is "/", it will still be at "/" after execution; ".." means the upper-level directory);
cd ../.. # Return to the directory two levels above;
cd !$ # Use the parameters of the previous command as cd parameters.

Instructions for switching to the previous working directory

cd-
# The command will first display the target directory to be switched to, and then enter.
cd ${OLDPWD}
# The command will directly switch to the previous working directory.

About CDPATH

# Set the desktop folder as the value of CDPATH.
CDPATH='~/Desktop'
# Assume that there are no test3 folders under the paths ~ and ~/Desktop we are going to demonstrate next, create them now.
mkdir ~/test3
mkdir ~/Desktop/test3
# Enter the ~ directory.
cd ~
# Enter the test3 directory.
cd test3
# After execution, ~/Desktop/test3 is displayed and enters this directory instead of the test3 directory of the ~ directory.
# If CDPATH has a value, it will first search in CDPATH and enter the first successful match. If all fail, the current directory will be tried last.

About cdable_vars

# Open options.
shopt -s cdable_vars
# Assume that the current path and CDPATH do not have a directory named new_var.
new_var='~/Desktop'
# Try to enter.
cd new_var
# Close option.
shopt -u cdable_vars

Notice

  1. This command is a built-in bash command. For related help information, please see the help command.

  2. It is recommended that when you use the cd command when writing a script, please add necessary comments to remind readers of the current working directory to avoid problems such as file not found.