CD is one of the very basic commands in Linux which is used very often. It is called the change directory command.
In this tutorial, we learn how to use the cd command in Linux. Here will cover its syntax and navigate directory structure with basic examples.
- Access to Linux terminal.
- Required permission to run the command.
- A good smile to learn.
The cd command is used to change the current working directory in Linux. The word 'cd' means change directory.
The current directory is the directory in which the user is currently working. Pwd command print the path of the working directory
The syntax of the cd command:
cd [options] directory
Cd command options:
-L: Force follow symbolic links. The default is the same.
-P: Use the physical directory structure without following symlinks.
How to CD to a Directory
To change a directory, use the cd along with specifying the directory path.
$ cd [path/to/directory]
Use the pwd command to print the present working directory then, use the absolute or relative path to navigate into another directory. You can use ls command to list directories and files in the file system.
For example, I am currently working in
/home/linuxopsys directory, and to change into the
Pictures directory, type:
$ cd Pictures
How to Use CD Command
Linux cd command allows you to change the directory using the relative and absolute paths, change to the root directory, parent directory, move into the previous directory, and more.
Change to Absolute or Relative pathname
The absolute path starts from the system root `/` whereas the relative path starts from the current working directory.
When you login into the Linux system, the default working directory is set to your home directory.
Let's change the directory using the relative path, here I change to
Documents directory from the current working directory:
$ cd Documents
To access the same
Documents directory using the absolute path, use the following command:
$ cd /home/username/Documents
Change to Home Directory
There are two ways to change to the
Home directory, simply type
cd ~ from the terminal.
$ cd ~
For example, navigate to
Documents directory which is inside
Home directory, type:
$ cd ~/Documents
To change into specific user’s home directory, use the following command:
$ cd ~username
Make sure you have enough privilege to change to the user's home directory.
Change to Root Directory
The root directory is the first directory or the main directory in the file system hierarchy which is represented by a single slash (`/`).
To change to the root directory, type cd command followed by
$ cd /
Change directory to next level
To change to the parent directory of the current working directory, type
cd .. or
cd ../ from the terminal.
Basically, move one directory level up from the current directory.
You can also change into a specific directory by one level up:
$ cd ../Desktop
To move into the two-level up from the current directory, use this command:
$ cd ../../
Change to Previous working Directory
To change to the previous working directory, you can use the following command:
$ cd -
In the example, we were on
/home/linuxopsys directory and after running
cd - command, the current working directory changed to the previous directory
Change to Directory with space in the name
You may some noticed directory names with space. This commonly occurs when directories are moved from Windows.
To prevent space from being parsed, you should use quotes. For example to change a directory that has spaces in its name, type :
$ cd 'my personal directory'
You may also use a special character backslash (\) to ignore the space as follows:
$ cd my\ personal\ directory
CD with other commands
For more productivity, you can use cd command with other commands. For example, change to Documents directory and list the contents, type:
$ cd Documents && ls
In this tutorial, we have demonstrated how to use the Linux cd command in this tutorial.
Please feel free to provide your feedback and suggestions.