Changing Directories in Linux Terminal
The cd command in Linux allows you to change directories. You just have to give the path to the directory.
cd path_to_directory
And here comes the first challenge if you are new to Linux. You are probably not sure about the path.
Let’s tackle that first.
Understanding paths in Linux
The path traces the location in the Linux directory structure. Everything starts at the root and then goes from there.
You can check your current location with the following:
pwd
It should show an output like /home/username. Of course, it will be your username.
As you can see, paths are composed of / and directory names. Path /home/abhishek/scripts
means the folder scripts is inside the folder abhishek
, which is inside the folder home
. The first /
is for root (from where the filesystem starts), the trailing / are separators for the directories.
Now, there are two ways to specify a path: absolute and relative.
Absolute path: It starts with the root and then traces the location from there. If a path starts with /, it is an absolute path.
Relative path: This path originates from your current location in the filesystem. If I am in the location /home/abhishek and I have to go to /home/abhishek/Documents, I can simply go to Documents instead of specifying the absolute path /home/abhishek/Documents.
Before I show you the difference between the two, you should get familiar with two special directory notations:
- . (single dot) denotes the current directory.
- .. (two dots) denote the parent directory taking you one directory above the current one.
Here’s a pictorial representation.
Want more on paths in Linux? This article will help you.
Changing directory with cd command
Now that you are familiar with the concept of path, let’s see how you can change the directory.
Enter the following command to see the directories inside your home directories:
ls
This is what it shows to me:
abhishek@ituxedo:~$ ls
Desktop Downloads Pictures Templates VirtualBoxVMs
Documents Music Public Videos
Yours may be similar but not exactly the same.
Let’s say you want to go to the Documents directory. Since it is available under the current directory, it will be easier to use the relative path here:
cd Documents
Now, let’s say you want to switch to the Templates directory that was located in your home directory.
You can use the relative path ../Templates
(.. takes you to the one directory above Documents to /home/username and from there you go to Templates).
But let’s go for the absolute path instead. Please change ‘abhishek’ with your username.
cd /home/abhishek/Templates
Now you are in the Templates directory. How about going to the Downloads directory? Use the relative path this time:
cd ../Templates
Here’s a replay of all the above directory change examples you just read.
Troubleshooting
You may encounter a few common errors while changing the directories in Linux terminal.
No such file or directory
If you see an error like this while changing the directories:
bash: cd: directory_name: No such file or directory
Then you made mistake with the path or name of the directories. Here are a few things to note.
- Make sure there is no typo in the directory name.
- Linux is case sensitive. Downloads and downloads are not the same.
- You are not specifying the correct path. Perhaps you are in some other location? Or did you miss the first / in the absolute path?
Not a directory
If you see an error like this:
bash: cd: filename: Not a directory
It means that you are trying to use the cd command with a file, not a directory (folder). Clearly, you cannot enter a file the same way you enter a folder and hence this error.
Too many arguments
Another common rookie Linux mistake:
bash: cd: too many arguments
The cd commands take only one argument. That means that you can only specify one directory to the command.
If you specify more than one or mistyped a path by adding a space to the path, you’ll see this error.
cd -
, it will take you to your previous directory. It’s quite handy when you are switching between two distant locations. You don’t have to type the long paths again.Special directory notations
Before ending this tutorial, let me quickly tell you about the special notation ~
. In Linux, ~ is a shortcut for the user’s home directory.
If user abhi
is running it, ~ would mean /home/abhi
and if user prakash
was running it, it would mean /home/prakash
.
To summarize all the special directory notations you learned in this chapter of the terminal basics series:
Notation | Description |
---|---|
. | Current directory |
.. | Parent directory |
~ | Home directory |
– | Previous directory |
Test your knowledge
Here are a few simple exercises to test your newly learned knowledge of the path and the cd command.
Move to your home directory and create a nested directory structure with this command:
mkdir -p sample/dir1/dir2/dir3
Now, try this one by one:
- Go to the dir3 using either absolute or relative path
- Move to dir1 using relative path
- Now go to dir2 using the shortest path you can imagine
- Change to the sample directory using absolute path
- Go back to your home directory
Want to know if you got all of them right or not? Feel free to share your answers in the It’s FOSS Community.
I highly recommend reading this article to know small but useful things about the terminals and the commands.
Stay tuned for more chapters in the Linux Terminal Basics series if you want to learn the essentials of the Linux command line.
And, of course, your feedback on this new series is welcome. What can I do to improve it?