Linux cd command - change working directory path

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:

Linux cd command

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
Like(1)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?