Cd Command in Linux (Change Directory)
Introduction
In Linux and other Unix-like operating systems, the cd
("change directory") command is used to change the current working directory. When working on the Linux terminal, it is one of the most basic and frequently used commands.
The user's current working directory is the folder (directory) in which he or she is now working. You're operating in a directory every time you interact with your command prompt.
This tutorial will show you how to browse your system's directory tree using the cd
command. We will also address a few FAQs on cd
command in Linux.
cd
Command
cd
is a shell built-in whose behavior varies slightly depending on the shell. It determines the essential information for its execution by looking at the shell environment variables.
We'll look at the built-in version of cd
in Bash.
The cd
command has the following syntax:
cd [OPTIONS] directory
Only two options are available to the command, both of which are rarely used.
-L
, Follow symbolic links. By default,cd
behaves as if the-L
option is specified.−P
, Symbolic links should not be followed. To put it another way, if you choose this option and try to go to a symlink that refers to a directory, thecd
will change to that directory.
When used without any arguments, cd
will transport you to your home directory in its most basic form.
You can use the Tab
key to autocomplete directory names when moving around the file system. It's not necessary to include a slash at the end of the directory name.
You must have executable permissions for a directory in order to switch to it.
You can use the pwd
command to find out what directory you're in right now.
Absolute and Relative Path Names
You can use absolute or relative path names to define a directory to change to. The absolute or full path begins at the system root /
, whereas the relative path begins in the current directory.
Your current working directory is set to your home directory by default when you connect to your Linux system. If the Downloads
directory is present in your home directory, you can navigate to it using the relative path:
cd Downloads
You can also use the absolute path to go to the same directory:
cd /home/username/Downloads
In other words, a route that begins with a slash (/
), is an absolute path to the directory.
The Parent Directory
The current working directory is represented by a single dot on Unix-like operating systems (.
). The parent directory, or the directory directly above the current one, is represented by two dots (..
), one after the other.
If you type cd.
, you will change to the current directory, otherwise, the command will have no effect.
Assume you're in the /usr/local/share
directory right now. To go to the /usr/local
directory (which is one level above the current directory), type:
cd ../
You might use the following command to go two levels up to the /usr
directory (the parent's parent):
cd ../../
Here's another illustration. Assume you're in the /usr/local/share
directory and wish to change to /usr/local/src
. You can do so by typing the following:
cd ../src
Navigate to the Previous Directory
Pass the dash (-
) character as an argument to the cd
command to return to the previous working directory:
cd -
Navigate to the Home Directory
Simply type cd
to get to your home directory. The tilde (~
) character can also be used to return directly to your home directory, as demonstrated below:
cd ~
For instance, if you wanted to go to the Downloads
directory in your home directory, you would type:
cd ~/Downloads
You can also use the following syntax to get to another user's home directory:
cd ~username
Directories with Space in Their Names
If the directory you want to shift to have spaces in its name, you can either quote the path or escape the space using the backslash () character:
cd 'Dir name with space'
cd Dir\ name\ with\ space
FAQs on cd command in Linux
How do I change to a specific directory using an absolute path?
To change to a directory using an absolute path, you can use the command "cd /path/to/directory".
Can I use a relative path with the "cd" command?
Yes, you can use a relative path with the "cd" command. For example, "cd ../directory" would take you one level up in the directory hierarchy and then into the "directory".
How can I go back to the previous directory quickly?
To go back to the previous directory, you can use the command "cd -" (cd followed by a hyphen).
How do I switch to the last directory I accessed?
You can switch to the last directory you accessed by using the "cd" command without any arguments or by using "cd ~-" (cd followed by a tilde and a hyphen).
Is it possible to create a shortcut for a frequently used directory?
Yes, you can create a shortcut for a frequently used directory by creating an alias. For example, you can use the command "alias mydir='cd /path/to/directory'" to create an alias called "mydir".
How can I navigate to the parent directory?
To navigate to the parent directory, you can use "cd .." or "cd ../" (with or without a trailing forward slash).
What if I want to change to a directory with a space in its name?
If the directory name contains a space, you need to enclose the directory name in quotes to change to that directory. For example, "cd 'directory with space'".
Conclusion
By now, you should know what the current working directory is and how to travel across your system's directory structure using the cd
command.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.