by Swapnil Bhartiya

How to manage directories in Linux using the command line

Sep 30, 2016
LinuxOpen Source

Learn the basics of directory management in Linux using the command line tool.

Did you know that you can manage all of your files and folders right from the command line, using applications like Gnome Terminal?

Here are some of the basic commands that we use to manage directories (also known as folders) in Linux distributions.

Move among folders

If you are using a file manager, you can switch between directories just by clicking on that folder. We use the ‘cd’ command that stands for change directory to achieve the same using the command line interface (CLI).

When you open the Terminal app, you are in the home directory of the logged in user. You will notice something like this:

swapnil@ubuntu: ~ $

Here ‘swapnil’ is the logged in user, ubuntu is the hostname of the machine and ‘~’ denotes the home directory.  

Let’s say I want to go to the Downloads directory that’s inside home. We run this command:

cd /home/swapnil/Downloads/

This is what you see:

swapnil@ubuntu:~/Downloads $

It means you are now in the Downloads directory. If you want to see the content of that directory just run the ‘ls’ command.

Create directory

We use ‘mkdir’ (make directory) command to create new directories. If you simply run the mkdir command followed by the name of the new directory, the folder will be created in the current directory. If we run ‘mkdir cio’, a new directory named ‘cio’ will be created inside the ‘Downloads’ folder.

If we want to make nested directories such as ‘Downloads/cio/stories then we use ‘-p’ option which creates parent and child directories along the path:

mkdir -p /home/swapnil/Downloads/cio/stories

Move out of directory

To get out of the current directory and go back to home, just type ‘cd’ If you want to go back one directory, use ‘cd ..’. Say you are in the ‘stories’ directory and you want to move out of it and go to ‘cio’, just use ‘cd ..’ command.

swapnil@ubuntu:~/Downloads/cio/stories $ cd .. 

Your directory will change to cio:

swapnil@ubuntu:~/Downloads/cio $  

Copy and move directories

There are two commands for copying or moving directories: cp (stands for copy) and mv (stands for move). The copy command simply makes a copy of the folder to the new location, whereas the move command makes the copy of the folder to the new location and deletes it from the original location. Be extra careful when using the ‘mv’ command.

To copy a directory, use the cp command:

cp -r /path_of_current_folder /path_of_destination

Let’s say we want to copy the ‘stories’ folder from home directory to another location. The command will be:

cp -r /home/swapnil/Documents/cio/stories /media/file/

This command will make a copy of ‘stories’ folder in ‘media/file/’, which is a secondary hard drive of my system.

If you only want to move the contents of a folder, and not the whole folder, to another directory then use ‘*’ option. Let’s say we want to copy all files saved in the ‘stories; directory to a new folder, we will run this command:

cp -r /home/swapnil/Documents/cio/stories/* /media/file/stories/

It will basically copy everything ‘inside’ the source folder to the destination folder.

Move directories around

The move command (mv) works similar to the copy command (cp), the only difference being deletion of the source directory.

Let’s say I want to move the ‘stories’ folder from home directory to another location. The command will be:

mv -r /home/swapnil/Documents/cio/stories /media/file/

If I want to move all the content of the ‘stories’ folder then I will use the ‘*’ option

mv -r /home/swapnil/Documents/cio/stories/* /media/file/stories/

How to delete a directory

In Linux-based operating systems ‘rm’ is the command to remove or delete any file or folder. 

Warning: Never run the ‘rm -r’ command without absolute path because it will delete everything in the current directory, which is basically your whole system.

If you want to remove the ‘stories’ directory with all of its content, the command is

rm -r /home/swapnil/Documents/cio/stories

If you want to keep the ‘stories’ folder but want to delete all of its content, the command is:

rm -r /home/swapnil/Documents/cio/stories/*

Those are some of the basics of using command line to manage your directories on Linux based distributions. If you have any questions, feel free to ask in the comments below.