Using cd command in Linux

While working in a Linux terminal you often need to switch from one directory to another. The cd is a command used to change the directory in Linux. This command is common in most of the operating systems including Unix, MS-DOS, IBM OS/2, Amiga OS, etc.

By using this command you can move to some other directory in your filesystem from your current working directory. In this article, we will see the cd command and its usage in Linux with some examples.

The syntax of the cd command

The cd command is one of the most frequently used commands in Linux. Its syntax of using in Linux or Unix is given below –

cd directory_name

What is a file or directory path in Linux?

A path is a human-readable representation of a file or directory’s location in the Linux filesystem. It may be a little bit frustrating if you are not familiar with the Linux filesystem or working with the terminal.

Following is the example of directory path –

/usr/local/games

There are two types of file or directory paths i.e absolute and relative path. In Linux or Unix, all the directories are organized under the / so the absolute path in Linux starts from / directory. A relative path is a path that starts from your home directory.

How to change the directory in Linux

The current working directory is the directory where currently you are working in the Linux filesystem. Now use the following command to list the files & directories in your current working directory –

ls

Change the directory to dir

Use the following command to move to the directory dir

cd dir

Change directory to /

To change the current working directory to the root directory you need to execute the following command in your terminal –

cd /

Going back to your home directory

Now if you want to move to your home directory then use the following command –

cd

or use –

cd ~

Going back to the previous directory

If you want to navigate to the previous directory then use - with cd command-

cd -

Navigate to directories that contain space in their name

If you want to move to a directory lets say Untitled Folder that contains white spaces in its name then pass the name with single quotes –

cd  'Untitled Folder'

If the directory is not under your current working directory the use its full path(absolute path).

Conclusion

That’s how you can use the cd command to navigate among the directories in the Linux filesystem. Now if you have a query then write us in the comments below.

Leave a Comment