Cd Command in Linux

superior_hosting_service

How To Use Cd Command in Linux With Examples

Cd_Command_in_linux.png

The cd (“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems. It is one of the most basic and frequently used commands when working on the Linux terminal.

The current working directory is the directory (folder) in which the user is currently working in. Each time you interact with your command prompt, you are working within a directory.

In this tutorial, we will show you how to use the cd command to navigate your system’s directory tree.

cd Command

cd is a shell builtin, and its behavior may slightly differ from shell to shell. It uses the shell environment variables to determine necessary information for its execution.

We will cover the Bash builtin version of cd.

The syntax for the cd command is as follows:

$ cd [OPTIONS] directory

The command accepts only two options that are rarely used.

  • −L, Follow symbolic links . By default, cd behaves as if -L option is specified.
  • −P, Don’t follow symbolic links. In other words, when this option is specified, and you try to navigate to a symlink that points to a directory, cd will change into the directory.

In its simplest form, when used without any argument, cd will take you to your home directory.

When navigating through the file system, you can use the Tab key to autocomplete the names of directories. Adding a slash at the end of the directory name is optional.

To switch to a directory, you must have executable permissions for that directory it.

The pwd command allows you to find out what directory you are currently in.

Absolute and Relative Path Names

When specifying a directory to change to, you can use either absolute or relative path names. The absolute or full path starts from the system root /, and relative path starts from your current directory.

By default, when you log into your Linux system, your current working directory is set to your home directory. Assuming that Downloads directory exists in your home directory, you can navigate to it by using the relative path to the directory:

$ cd Downloads

You can also navigate to the same directory by using its absolute path:

$ cd /home/username/Downloads

In short, if the path starts with a slash (/) it is the absolute path to the directory.

The Parent Directory

On Unix-like operating systems, the current working directory is represented by a single dot (.). Two dots (..), one after the other, are representing the parent directory or in other words the directory immediately above the current one.

If you type cd ., you will change into the current directory or, in other words, the command will do nothing.

Suppose you are currently in the /usr/local/share directory, to switch to the /usr/local directory (one level up from the current directory), you would type:

$ cd ../

To move two levels up to the /usr directory (the parent’s parent), you could run the following:

$ cd ../../

Here is another example. Let’s say you are in the /usr/local/share directory, and you want to switch to the /usr/local/src. You can do that by typing:

$ cd ../src

To change back to the previous working directory, pass the dash (-) character as an argument to the cd command:

$ cd -

To navigate to your home directory simply type cd. Another way to return directly to your home directory is to use the tilde (~) character, as shown below:

$ cd ~

For example, if you want to navigate to the Downloads directory, which is inside your home directory, you would type:

$ cd ~/Downloads

You can also navigate to another user’s home directory using the following syntax:

$ cd ~username

Directories with Space in Their Names

If the directory you want to change to has spaces in its name, you should either surround the path with quotes or use the backslash (\) character to escape the space:

$ cd 'Dir name with space'
$ cd Dir\ name\ with\ space

Conclusion

By now, you should have a good understanding of what is the current working directory and how to use the cd command to navigate into a different directory.