Linux Directory Commands for Beginners

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.

Table Of Contents

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).

linux home directory

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:

systematic diagram of linux file system

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.

pwd: linux directory command
pwd output

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.

pwd command
another example of pwd 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

ls : linux directory command
ls output

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 command.

$ 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-l.

ls -l : linux directory command
ls -l command output
$ 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.

ls -a : linux directory command
ls -a command output

Note-2: Options are followed by ‘-‘ sign. So write ls -a instead of ls a.

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.

Usage: cd foldername

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.

cd : linux directory command
cd output followed by pwd output

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.

cd with ls command
cd and ls command

I want to go to practice folder. But, this time practice has lowercase p.

$ cd practice 

and checking with pwd command:

cd ls pwd commands
checking working directory using pwd command

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!!!!

coming back to directory
cd to come back to directory

Lets go back to practice folder directly using direct path.

$ cd Desktop/practice
cd command with direct path
using cd to long path

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 ../..
cd direct back command
using cd to come back twice in directory

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

OR

$ cd ~
cd command to take to home directory
cd command to take back to home directory

To know more about cd commands, there is a good article about cd commands from geeksforgeeks.

d. mkdir command

mkdir command means “make directory”. This command allows us to make a new folder (or directory) in our present working directory.

Usage:

mkdir new_folder_name

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 command

$ mkdir my_folder

This creates a folder. You can check new folder through your default file browser or check using ls command.

mkdir : linux directory command
mkdir creates new folder

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'
mkdir command
Two folder created using mkdir command

You can see those two folders in file browser also.

nautilus file explorer
showing two folders

Let’s use 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.

mkdir with parent option
mkdir with -p option

e. rmdir command

rmdir means ‘remove the directory’. This command allows us to remove the specified folders of the current working directory.

Usage:

rmdir folderName_to_remove

let’s remove two folders created above using mkdir command

$ rmdir my_folder
$ rmdir 'my_folder'
rmdir : linux directory command
rmdir command to remove directory

This command 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.

rmdir to remove non empty folder
rmdir us unable to remove nonempty directory

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.

Usage:

rm fileNameToRemove

You can see one example below where I removed a file of name ‘test.txt’ using rm command.

rm :inux drectory command
rmrm command in work

There are other options too for rm command. If we have to delete empty director then type

rm -d empty-directory-name

e.g 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

3. Conclusion

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.

Share this post:

Leave a Comment

Your email address will not be published. Required fields are marked *