Linux Terminal is so powerful that we can achieve almost all tasks using commands in it. It replaces the graphical user interface(GUI) for a command lover like me. To use Terminal to its fullest, the first thing you have to learn is Linux Directory Commands. This article will guide you through everything, from listing files to making and removing a directory. As a result, you will be ready to learn more Linux Commands for various purposes.
1. Linux File System (Linux Directory)
If you are new to Linux, the file system seems to be difficult to understand at first, let alone commands. There is no local disk C or D like partitions in the Linux File system. Every file in the Linux system is inside a root directory or (we denote by /). The root directory is hidden for safety. When we open our file explorer (nautilus is the name of the file explorer in the Ubuntu system), we get the Home directory at first which is one branch of the root directory(or folder).
All the important directories like Desktop, Downloads, Documents, Music, etc are inside this Home Directory. For example, see my Home directory from the above image.
For more information, look at the diagram of file system below:
The top i.e ‘/’ is the root directory and there are many other directories that include the Home directory. Other directories except for the Home directory, include all the system files. So, it’s wise to avoid any changes inside those directories without prior knowledge.
Inside the Home directory, there is a directory under the name of your username (bkguru is my pc username). Remember that Home and <username> directory are like a similar directory. You can see other useful directories like Desktop and a few other important directories inside <username> directory.
Now let’s go and learn some basic Linux directory commands for navigation so that you can easily navigate through these directories using a terminal.
First, you open up a terminal either by pressing
Ctrl + Shift + T or searching Terminal application in a software search.
2. Important Linux Directory Commands
a. pwd command
pwd command meaning is “present working directory”. It means our current location in the Linux file system. Whenever we open our terminal, using Ctrl + Shift + T, the Linux file system takes us to the user folder. let’s see that in the terminal too. Type
pwd command see the output. But remember to type the command in lowercase letter.
So my output says “/home/bkguru“. your output might show your username instead of my username “bkguru“. This says, we are inside “bkguru” folder so, any other commands that do certain tasks will be done in this “bkguru” folder.
To try more, open up any folder of your choice and right-click, select the option ‘open in terminal’ and type the command.
b. ls command
ls command means ‘list’ that means this command lists out all the files and folders inside our current working folder.
So simply type the command
ls and see its output, as soon as, you open up the terminal using
Ctrl + Shift + T
The output shows all the files and folders inside my current working folder (username folder).
You can type ls command on any folder and it will list out all the things that are inside of that. Outcomes are shown in different color format as shown in image above.
But with the
ls command, we can provide options too. You can check all the options available for
ls command using command
man (for manual), typing
man ls. This command
man works for all commands help. Here is few options to work with
$ ls -l
-l means long form) gives all lists in long-form including date, size, permissions, and other properties.
Note-1: Remember to put space between command and options, like
ls -l not
$ ls -a
-a for all) gives the list of all files and folders including hidden system files as well. The files with the ‘.’ dot sign at the front are the hidden files.
Note-2: Options are followed by ‘-‘ sign. So write
ls -a instead of
c. cd command
cd command meaning is ‘change directory’. This command can be used to change our directory back and forth in the LINUX file system.
Now I am in my /home/bkguru/ directory, if I use
ls command then, I can see so many other folders inside my bkguru folder. For instance, I want to work on Desktop folder. That means I have to change the directory from bkguru (username directory) to Desktop. So I have to type
$ cd Desktop
Check your present working directory to confirm the above change.
As you can see in the above output,
cd Desktop takes us to Desktop folder, which is confirmed by
pwd output: “/home/bkguru/Desktop“
Note-3: Desktop has uppercase D. So,
cd desktop won’t work.
Let’s list out the folders available in Desktop using
ls command again.
I want to go to practice folder. But, this time practice has lowercase p.
$ cd practice
and checking with
We only used
cd command to move forward with folders. But, we can use
cd command to return to the previous directory also.
For that we have to type:
$ cd ..
Remember the space!!!!
Lets go back to practice folder directly using direct path.
$ cd Desktop/practice
We can use the long path as well if we know the path exactly.
Now lets come back using
cd ../.. directly to our username folder. first two dots .. take one step back and
../.. takes two step back directly.
$ cd ../..
Suppose we are in a different directory than our usual directory which is /home/bkguru. In that case, we have a direct command to come back to the initial directory using
$ cd ~
To know more about
cd commands, there is a good article about
cd commands from geeksforgeeks.
mkdir command means “make directory”. This command allows us to make a new folder (or directory) in our present working directory.
Let’s create a new folder name “my_folder” in Desktop.
First navigate to Desktop using cd command.
$ cd Desktop
Now we are in Desktop, lets create a folder using
$ mkdir my_folder
This creates a folder. You can check new folder through your default file browser or check using
But why do we write my_folder instead of my folder?
We cannot give space in the folder name in terminal similar to the variable name in any programming languages. If we really want folder name with space, we can give using single quotes (‘ ‘)
$ mkdir 'other folder'
You can see those two folders in file browser also.
mkdir command with
-p option. The option -p allows us to create a folder, inside one folder like a path.
$ mkdir -p new/more
This creates a more directory inside new directory.
rmdir means ‘remove the directory’. This command allows us to remove the specified folders of the current working directory.
let’s remove two folders created above using
$ rmdir my_folder $ rmdir 'my_folder'
rmdir works fine with the empty folder, but it cannot remove the folder which is not empty. for eg.
let’s try to remove new folder using
rmdir command which we created earlier.
It says failed to remove ‘new’ : Directory not empty.
In such case, we can use
rm -r command. Don’t worry for now, just type command:
$ rm -r new
This deletes ‘new‘ folder with everything inside it.
f. rm command
rm command means ‘remove’. It is basically like del key in the computer. It can delete any kinds of files and also directory with some options.
You can see one example below where I removed a file of name ‘test.txt’ using
There are other options too for rm command. If we have to delete empty director then type
rm -d empty-directory-name
rm -d myFolder
-d for directory
If we have to delete directory and everything inside it, type
rm -r directory-name
-r for recursive.
We have already used this command above.
If we want to see every step of terminal we can always use
-v (for verbose) command.
rm -v folder-name
Learning these Linux directory commands, we can easily navigate through any directories and that’s the beginning step for any work using the terminal in Linux. After you become pretty good with these commands, you can learn other commands. We suggest you going through the commands for File handling.