cd
Description
The cd
command, also known as chdir
(change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.
The syntax for the command is:
cd [directory]
Use cases
Imagine you have a folder (
videogames
) on your computer where you keep all your video games. You use thecd
command to go there so you can play your favorite games.cd videogames
Suppose you have a folder (
music
) where you store your music. You usecd
to enter that folder to listen to your favorite songs.cd music
Imagine you are working in a project folder named
my_project
that is located within aprojects
directory, which, in turn, is inside yourDocuments
directory. To move up to the parent directory (i.e.Documents
), you can use thecd ..
command.cd ..
You're working in two directories:
source
anddestination
. You want to quickly switch back and forth between them. You can usecd -
to toggle between these directories.cd source cd - # Switches to "destination" cd - # Switches back to "source"
Additional Section
Navigating to the
root
DirectoryTo navigate to the
root
directory, which is the top-level directory in the file system, we can usecd /
command.cd /
Navigating to
home
DirectoryTo navigate to
home
folder, where personal files and settings are stored, we can use thecd
command with either username or the tilde (~
) symbol.cd susheel
OR
cd ~
OR
cd
Navigating Directory with spaces in them
Suppose you have a directory named `My Documents`` with a space in the name, and you want to navigate to it. To do this, you need to enclose the directory name in quotes.
cd "My Documents"
Note: You can also use escape character to achieve same result
cd My\ Documents
Navigating using absolute paths and relative paths
Absolute Path
- An absolute path specifies the complete directory structure from the root directory to the target directory.
- It always begins with a forward slash (
/
) in the Unix-like systems.
Example
Suppose you have the following directory structure:
/ ├── home │ └── user │ ├── documents │ └── pictures └── var └── logs
To navigate to the
pictures
directory using an absolute paths:cd /home/user/pictures
Relative Paths
- A relative path specifies the location of the target directory relative to your current working directory.
- It doesn't start with a forward slash.
Example
If you are currently in the
documents
directory./ ├── home │ └── user │ ├── documents │ └── pictures └── var └── logs
To navigate to the
pictures
directory using a relative pathcd ../pictures
Note: You can learn more about absolute and relative path from here.
Exercises
You are in the
videogames
directory and want to navigate to themovies
directory, which is a sibling ofvideogames
. How would you use the cd command to achieve this?Directory Tree
. ├── movies └── videogames
You want to quickly switch between two directories named
work
andprojects
multiple times. How would you use the cd command to achieve this?