Linux cd command is used to change the current directory. cd is the abbreviation for change directory.
The cd command is a frequently used command in Linux like the ls command. To switch to a new directory, use the cd command with the directory path.
Switch to a directory using Relative Path
The below command uses a relative path to change the directory,
cd [directorypath]
Switch to a directory using Absolute Path
cd /usrhome/files/texts
Autocomplete Directory Name
After entering a directory name press Tab key to show autocomplete suggestions.
If the folder name is userfonts and thping cd u[Tab] will auto fills the folder name.
cd u[Press Tab Key after u]
After switching to the desired directory, use the pwd command to view the current working directory
pwd
Change Directory and List the Directory Contents
cd dirName & ls
Above command will change the directory and list the directory contents simultaneously.
Change to Previous Directory
To Switch back to the previous directory use cd followed by dash(hyphen) - symbol.
cd -
Change to the root directory
A user can change from pwd to root directory using the forward slash '/'
cd /
Change to the home directory
To change to the home directory just use the cd command or cd command followed by the tilde(~) symbol,
cd
cd ~
If a user is on some other directory and wants to switch to the home directory, this command will be useful.
Change to Parent Directory
Consider you are in /home/files/all files/dir1/dir2/dir3. The parent directory for dir3 is dir2 and the parent directory for dir2 is dir1.
cd ..
The above command will take the user to dir2 i.e., /home/files/all files/dir1/dir2. This will help you to navigate one up to level up to the parent directory.
A single dot '.' represents the current directory and double dot '..' means parent directory.
Go two levels up to the parent directory
cd ../../
Navigate to Directory Name containing Spaces
To navigate to directory that have spaces in name use single quotes or add back slash '\' after each word except the last one.
cd 'All Files'
cd All\ Files
To get more info about the cd command use the following two commands to see all the supported options
cd --help
help cd