Master the essential Linux commands for managing files and directories. In this post, we’ll walk through the most important Linux commands for navigating, organizing, and manipulating the file and directories of Linux/Unix based operating system—tools every DevOps professional should have in their toolkit. Linux file and directory commands explained using screenshots, command format and examples for every command.
Directory Manipulation
Create a directory => mkdir
mkdir command is used to create new directories.
Command format => mkdir [options] directory_name
Command example => mkdir new_directory

Delete/Remove an Empty directory => rmdir
rmdir command is used to remove empty directory. Only works if the directory is empty.
Command format => rmdir directory_name
Command example => rmdir new_directory

Delete/Remove directory with files and subfolders in it => “rm -r” or “rm -rf”
-r
(recursive) : removes the directory and everything inside it.-f
(force) : suppresses warnings or confirmation prompts.
To explain this command, let’s use the directory shown in screenshot below. It is named new_directory and has two files in it, file1.txt and file2.txt.

Scenario 1: Using the command rmdir only => complains, directory not empty, shown in screenshot below.

Scenario 2: Using the command “rm -r” => Successfully deletes the directory and its contents (files and sub-folders) inside the directory.

Scenario 3: Using the command “rm -rf“, to delete a file without any prompts and warning => Let’s look at the scenarios where you would need to use -f flag along with -r flag to remove and suppress all the prompts and warnings.
1. Write-Protected Files or Directories
files can be write protected inside the directory you are trying to delete.
2. Aliased or Interactive Shell Settings
Often times, “rm” is aliased to “rm -i”, -i here stands for interactive. This is a safety feature, wherein, you will be prompted or asked every time you try to delete or remove a directory.
In both of the cases above (#1 and #2) , you can use “rm -rf” command and you will not be prompted.
Following screenshot shows how the prompts looks like when deleting a non-empty directory. In this scenario you use -rf to remove a directory and you will not be prompted.

List Directory Contents => ls
The ls
command is used to list directory contents in Linux. It is undoubtedly one of the most frequently used commands. It is used to Lists files and folders in the current directory.
Command format => “ls”
As shown in the screenshot below, ls command is used for listing contents of the directory “new_directory” having three files in it.

List Directory Contents, Detailed Info => ls -l
“l” in “ls -l” command stands for Long listing format. It shows permissions, owner, size, modification date, etc. Let us look at it with an example screenshot below. As you can see, I first create a directory (“new_directory”) with two files in it (file1.txt and file2.txt). Focus on the difference in output of “ls” and “ls -l” command.

List Directory Contents, Human Readable Sizes => ls -h
List Directory Contents, Sorted => ls -t, ls -tr
I would be combining the two variations above (-h and -t option) in one. So, here it goes..
h => Long format with human-readable file sizes (e.g., 5.3K
, 2M
)
t => Sorts files by modification time, newest first.
r => Combined with t flag above, it shows oldest files first.
If you want your directory content to be sorted by modification time, then use -lt option and if you want to have file sizes in human readable format then use -lh option. Human readable format appends K for Kilobyte, M for Megabyte, etc. for file sizes.
Let us understand the options above, using screenshot below. As you can see, using the option “ls -lth” in command line shows the directory contents in sorted order, based on modification time as well as display sizes in human readable format.
Output displays file2 first and then file1, as file2 was modified last. (Focus on timestamp highlighted in green).
Now, compare the sizes, boxed in orange, how 2005 & 4233 is displayed as 2.0K & 4.1K respectively, in second command.

NOTE: To show files in reverse order, i.e. files modified first, simply use -r option. Therefore, the command ls -lrth flag will show file1.txt output first. An exercise for you guys to verify.. 🙂
Show size of directory => du -sh
To show the size of a directory in Linux, use the du
(disk usage) command. Most frequently used options along with du are “s” and “h”. Wherein, “s” represents summary and h represents human readable size of directory and its contents.
Command format => “du -sh directory_name”
Command example =>
To display the size of directory, new_directory, use the command => du -sh new_directory
Let us look at the command above in the screenshot below. I have tried to show all the variations of the command, boxed in blue highlighted boxes. Focus on the “sh” flag, as you can see it convert the output to human readable sizes, from 16, which did not make much sense as just a number, as compared with 16.0K, which clearly signifies that directory size is 16.0KB.

Directory Navigation
Present Working Directory => pwd
The pwd
command stands for “present working directory” or some people call it as “current working directory”. It displays the full path of the current directory you’re in. This command comes in handy when you want to know where you are in the filesystem and hence used extensively when writing shell scripts.
Command format => “pwd”
Command example =>
Display the current directory you are in currently => “pwd”
As you can see in the screenshot below, we start with root directory, then navigate to “new_directory”. Running pwd command again shows us “root/new_directory” as the current location.

Navigate to / Change directory => cd, cd ..
The cd
command stands for “change directory”. As the name suggests, this command is used to change the directory you are working on or want to navigate to.
Scenario 1: Change to a specific directory
Command format => “cd directory_path”
Command example =>
Navigate to new_directory in usr folder => “cd /usr/new_directory”
Navigate to new_directory under the same folder you are in => “cd new_directory”

Scenario 2: Move up one directory
Command format => “cd ..”
Yes, you saw it right, use two dots “..” to navigate to parent directory
Command example =>
Navigate to parent directory => “cd ..”
Continuing from the example above, you were navigated to new_directory, now you want to move up to it’s parent directory.

File Manipulation
Create a file => touch
The touch
command in Linux is used to create empty files or update timestamps of existing files.For now, let’s focus on creating empty file(s).
Command format => “touch filename1” (creates one file) OR “touch filename2 filename3” (creates multiple files at once)
Command example =>
Create one file => touch file1.txt
Create multiple files at once => touch file2.txt file3.txt

Remove / Delete a file => rm
rm command is used to remove a file
Command format => rm filename1 (removes one file) OR rm filename2 filename3 (removes multiple files at once)

Note: Just like in case of directory deletion above, you can use -f option to forcibly remove files as well, in case prompted before deleting files. “rm -f filename”
Read the contents of file => cat
cat
command is used to display, concatenate, or create files as well.
Command format => cat [options] filename
Command example => cat file1.txt

Copy Files => cp
To copy files, you use the cp
(copy) command. It lets you copy files from one location to another. As the name is self explanatory, let’s get started with the command and it’s variation.
Command Format => cp [options] source_file_path destination_file_path
Command Example
Copy one file (file1.txt) in same directory as file3.txt, use the command => “cp file1.txt file3.txt”
Copy one file (file1.txt) in different directory (new_directory) as file4.txt, use the command => “cp file1.txt new_directory/file4.txt”
Let us look at examples in practical using the screenshot below. Both the commands mentioned above are shown below. “cp” commands executed are shown in square brackets, highlighted in blue. The arrows marked in orange shows the results of the command.

Rename or Move File => mv
The mv
command in Linux is used to move or rename files and directories.
Command format => “mv [options] source_file destination_file”
Command Example =>
rename the file oldname.txt to newname.txt => mv oldname.txt newname.txt
rename and move the file to another directory (new_directory) => mv oldname.txt new_directory/newname.txt
Let us understand the commands above in the screenshot below. As you can see file1.txt gets moved or renamed to file5.txt in the same directory. Similarly, if you want the file to be renamed or moved to another directory, simply append the directory path in front of the destination file name. for example, to move file5.txt in /home/dir2 directory, use the command such as “mv file1.txt /home/dir2/file5.txt”.

Click here to download Ubuntu, a version of Linux operation system, for practise at your end.
Click here to read more about Linux
I have tried to explain the Linux file and directory commands using as much examples and screenshots as possible. Let me know if you want to see something else in this blog post, in your comments or DM me directly on Linked or contact me using the form on contact us page.