Figuring out how to go up a directory on Linux is vital to navigating your way through the file system. Here’s a detailed guide that covers every aspect of it.
The operating system follows a hierarchical structure to store information in folders and files. A directory or folder is nothing but a location to save files. We often traverse these folders to store data at various levels. The change directory command is used to move from one directory to another.
How to Go Up a Directory on Linux
At times, you may get stuck and may not know which particular directory you are in. The commands ‘cwd
’ and ‘pwd
’ can be used to find the current or present working directory. However, in some cases, you might want to move to the parent folder. This guide will discuss navigating or moving one level up in Linux.
The cd command
The cd command can be used to move to different folders while we are in the Terminal. You generally need superuser privileges to run the cd command. Alternatively, you can prefix the cd
command with sudo to run the command with administrative privileges. The general syntax of this command is as follows:
For instance, to move to the home directory of the current user, you can type the following command:
$cd /user/home
When you start the system and open the Terminal, the home directory is the default directory in which you are located.
Tip: A shortcut command can be used to navigate directly to the home directory. Simply type the following command to navigate to the home directory:
$cd
After that, you will then be taken to the home directory.
Tip: The ‘~
’ character stands for the home directory. So, you can also navigate to the home directory by using the following command:
$cd ~
Similarly, if you want to move to a specific directory, you can type the cd command followed by the complete path of the directory. The following command moves you to the dev folder:
$cd /dev
Now, to see the contents of the current directory, you can always type the following command:
$ls
Moving to a directory up
Now, you can move to an upper level and reach to parent directory by typing the following command:
$cd ..
Here, ‘..’ represents the parent directory. You can also type the following command to reach the parent directory:
$cd ../
Moving to two levels up
To jump to two levels up in the directory, type in the command you see below:
$cd ../../
Returning to the previous level
If you want to return to the previous level where you were before changing the directory, you can type the following command:
$cd -
Navigate to the root directory
No matter which folder you are currently in at the Terminal, you can move to the root directory by typing the following command:
$cd /
Working with directories with space in their names
When working with directories with space in their names, you should double-quote the name of the directory. For instance, the following command illustrates how you can change to a directory with space in their names:
$cd "Comparative Religion"
Alternatively, you can also use ‘\
’ to switch to a directory with space in their names. The following command shows how you can change the directory with space in their name using this command:
$cd Comparative\Religion
Using shell command to go up to a significantly higher level in the hierarchy
The standard approach to go up to a higher level is using the cd
‘..’ command multiple times. This can be pretty tedious sometimes. So, instead of writing cd
‘..’ numerous times, you can use shell programming and write the following function into the shell.
num=$1
while [$num-ne 0];do
cd..
num=$((num-1))
done
You need to place this command in the .bashrc
file and then run the following command to activate it:
$source .bashrc
Now, this function can be used in the Terminal. For instance, to go up by three levels, the following command can be used:
$goUp 2
Summary of characters for representing various directories
Before winding up the article, we would like to mention some special characters used for representing different special directories. The ‘.’ and ‘./’ represents the current directory. You can even use multiple slashes ‘.///.’ They all represent the same thing. The ‘..’ and ‘../’ represents the parent directory, and the ‘~’ represents the home directory.
Irrespective of the file system used, such as FAT32, ext, or any other file system. Almost all follow a hierarchical structure to organize the contents in terms of directories, sub-directories, and files. Switching from one directory or folder to another can be done with the help of the cd command.
Here, we discussed various options to move from one directory to another. This process can be done by switching to the home directory, parent directory, two levels up, and going back to the last directory. Interested readers can refer to the Linux manual to obtain detailed information about this command.
If this guide helped you, please share it.