English
+370 5 205 5502 sales@monovm.com

How to use the cd command in Linux?

Changing directory is an effortless and frequently used command whenever you work with Linux commands. Here's how you can use the cd command in all its formats.

22 Dec, 21 by Susith Nonis 6 min Read

List of content you will read in this article:

Linux is a very stable and flexible operating system allowing you to carry out various tasks using its command-line terminal. Whenever you open the command line terminal, you will end up in the desktop path. There might be many situations when you want to change that directory to another to carry out specific tasks. For example, suppose you have installed software and downloaded it to the C directory.

Then, it would be best to go to the particular directory path with the executable file for running that software. So to change the directory from one to another will require you to run the "cd" command. The "cd" stands for change directory.

It is one of the simplest commands that will require you to provide the destination directory as a parameter. Whenever you work on any script in Linux, you will come across this command very frequently. So make sure you learn its concepts and use them with various options available.

In this article, we will be highlighting the use of the cd command with its options to understand how it works around changing directories in the Linux system.

The "cd" command comes with a shell and is available as a built-in command. The working of the "cd" command may vary depending on which shell you are working on. This command will use that particular shell's environment variables to determine the desired information for its execution. Here, we use the Bash-builtin version for using the "cd" command.

You can use the below-mentioned syntax for the "cd" command.

cd [OPTIONS] directory

The "cd" command can be used with two options mentioned below, but they are rarely used. The options are-

  • −L, it follows the symbolic links. By default, the cd command behaves as if the -L option is specified. You can consider it as an alias of the cd command.
  • −P, it does not follow symbolic links, which means if this option is mentioned, and you try to go to a symlink that points to a specific directory, the cd will change into that directory.

In short, you can understand it like whenever you use the "cd" command without any option; then it will take you to the home directory. Also, if you are navigating through the filesystem of Linux, you can click the tab key for autocompleting the directory names. It is up to you to add a "/" character at the end of the directory or not.

If you want to switch to a specific directory, you must have the appropriate executable access. If you want to check your current working directory, you can execute a simple command; the command is "pwd". The "pwd" stands for the current working directory.

While using the "cd" command, you need to provide the destination directory where you want to move. So for that directory, you can either use the absolute path or the relative path names. The whole or the complete path will start from the system root /, while the relative path will start from the current directory. 

Whenever you log into your Linux system, your current working directory will be the home directory by default. Suppose, in your home directory, and you will have the test1 directory. Suppose you want to navigate to the test1 directory available under your home directory. In that case, you can do it by using the relative path of the directory; then you can run the following command.

cd test1

But if you want to navigate to the "test1" directory with the help of the absolute path, then you can run the following command

cd /home/username/test1

As in the above example, the path starts with the "/", it means the specified path is the absolute path to the directory.

While working on the Unix-like operating system, the current working directory is specified by the single dot ".". If you want to mention the parent directory or the directory, that is immediately a level up for the current directory can be done by using the double dot "..". 

For navigating to the current directory, you can hit the following command in your terminal.

cd .

Suppose you are in the directory- /usr/local/share directory. You want to navigate to another directory- /usr/local directory that is one level up to the current directory. Then you can use the following command in your terminal.

cd ../

If you want to navigate to the two levels up to the /usr directory, you need to run the following command in your terminal.

cd ../../

If you want to switch back to the previous working directory, you need to use the dash character (-) as an option along with the "cd" command as shown below.

cd -

If you want to move to your home directory, you can directly execute the "cd" command. This will take you to the home directory. If you want to try another method for moving to the home directory, use the tilde character (~) as an option with the "cd" command.

cd ~

Suppose, for navigating to the downloads directory under your home directory; you can use the following command.

cd ~/Downloads

Also, you can use the following command if you want to move to the home directory of another user in Linux.

cd ~username

If you want to move to the directory with the space in its name, you need to surround the path with quotes or even use the backslash (\) character to escape the space.

cd 'Dir name'

cd Dir\ space

Changing directory is an effortless and frequently used command whenever you work with Linux commands. Also, if you write any bash script, you need to use the "cd" command several times. It is also a simple command to use if you understand its concept and how it works with various arguments. 

Once you go through this article, you will know how to quickly move around directories and switch to another user's home directory. So get your concepts clear and make sure to practise the commands using its shortcuts (arguments) as it will help you a lot and save your time in running the "cd" command repeatedly.

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I veture into the world of Technology I try to share what I know in the simplest way possible. Not a fan of coffee, a sweet addict and a self accredited 'master chef'.