LinuxLexiconLinuxWorkingWithDirectoriesFeaturedImageIn our previous article on the Linux Directory Structure, we discussed the importance of directories in Linux and their organisation. Today, we are going to take a step further and learn how to manipulate and handle the various directories in Linux.

Directories form an integral facet of the Linux system. It is really important for any beginner to have a sound knowledge of how to work with directories.

In layman terms, directories can be considered analogous to folders and serve as containers. They are used to store files and other directories. This time, we are going to discuss the ‘cd’ command in detail, navigation between various directories, how to create or remove a directory, and much more.

pwd command

A command line interface can not provide you with a graphical representation of the file system structure and thus it can be pretty difficult for the starters to gather exact whereabouts in the system. In order to determine the working directory in which you are currently placed in, we use the pwd command.

pwd-Working-With-Directories

cd command

One of the most basic Linux commands, the cd command is usually the very first command that newbies get their hands dirty with at the beginning of their Linux journey. The cd command stands for ‘change directory’ and is used to switch your working directory from one to another. Here is an illustration of how it works:

cd0-Working-With-Directories

The directory in which you are currently placed is referred to as the ‘working directory’. By default, whenever you log in to a Linux system, you are placed inside your home directory (which would be something like /home/username).

Now before we move any further, you should be aware of the following symbols:

. – The ‘dot’ symbol refers to the working directory itself
.. – The ‘dot dot’ symbol refers to the parent directory of the working directory
/ – The ‘forward slash’ refers to the directory separator. Directories are assumed to end in a forward slash. The directory separator is optional for the last subdirectory in a path or command.

Here are some illustrations for you:

cd1-Working-With-Directories

The ‘cd ..‘ command takes you to the directory just above the current directory. Using the ‘cd .‘ command would not have any effect because the ‘.’ (dot) symbol refers to the current working directory itself.

cd2-Working-With-Directories

As you may have observed, this time around, I have specified a particular path against the command cd. Such a way of specifying pathnames is referred to as ‘absolute pathname’. An absolute pathname begins with the root directory and follows the tree structure branch by branch until the desired path is obtained.

cd3-Working-With-Directories

The other way of specifying the pathname is referred to as the ‘relative pathname’. Here, the pathname is specified with respect to the working directory instead of specifying the whole path from the root directory itself.

cd4-Working-With-Directories

Here is another shortcut that you may find handy at times. Use the ‘cd –‘ command to take you to your previous working directory.

cd5-Working-With-Directories

As shown in the example above, just typing in the cd command without specifying any pathname will automatically change your working directory to the home directory.

Also Read: 20 Quirky Things You Didn’t Know The Linux Terminal Could Do

Creating & Removing Directories

In order to create a directory, we use the mkdir command. Here is how it works:

mkdir [-p] directory

mkdir0-Working-With-Directories

Now suppose we wish to create intermediate directories as well, this is where the ‘[-p]’ ( or the parents) option that I mentioned above comes into play. It works in the following manner:

mkdir1-Working-With-Directories

In order to remove a directory, we use the rmdir command. We use the ‘[-p]’ (parents) option to remove the specified directories. Remember that the rmdir command can only be used to remove empty directories.

rmdir [-p] directory

In order to remove the directory along with all files and directories in it, we use the rm command. But, I would advise you to exercise caution while utilizing the command as there is no recycle bin or trash container from which you can restore the files that you have deleted. When you delete something, you can say goodbye to it forever.

rm -rf directory

rmdir&rf-Working-With-Directories

Try out all these commands for your own. You could even check out their man pages for further details. Being well-versed with them is going to make it easier for you to navigate your way through the Linux system and go a long way in ensuring that Linux is on your tips by the time we complete this series.

Read Our Whole Linux Lexicon Tutorial Series Here

Got any doubts, queries of your own, or any suggestions on the topics that you would like us to cover? Drop them in the comments below.