loading...

How to delete files and directories in Linux – Simple Guide

How to delete files and directories in Linux – Simple Guide

Hi guys, welcome back and today we will discuss how to delete files in Linux and what are different ways to delete files and directories in Linux. 

To delete files in Linux we use command-line options such as rm and unlink which will permanently delete files. You cannot restore them once you have deleted unless you have a backup of it. 

Table of contents –

rm (remove) command is a Unix/Linux command which is used to delete files from a filesystem. 

The rm command is used to delete files from Linux directories but there are some points to be noted down before using the rm command. Note these points because you may end up losing all your files if rm is used improperly.

  • rm deletes each specified file.
  • You can delete multiple files using the rm command.
  • rm cannot delete the Linux directories and does not work on directories that contain files. 
  • Deleting a file in Linux using rm command requires the write and execute permission.
  • rm never ask for confirmation while deleting a file. Make sure you follow the right steps while deleting a file. 

We can also use unlink command to delete files in Linux and we shall see examples about the same. Here, we are considering two files, file1.txt and file2.txt as an example to show how to delete files in Linux. 


Delete files in Linux using the rm command – 

The rm command is used in many ways to delete files and we will learn about each of the rm associated command using the examples. 

To use the rm command, open the terminal in that particular directory, and use the following commands as per your needs. 


Deleting a single file using rm – 

To delete a single file using the rm command, simply use rm with the filename. 

$rm file1.txt

This will delete the file, file1.txt. You can also specify the path to delete the file. 

$rm ABC/XYZ/QPR/file1

Deleting multiple files using rm – 

At times, there are too many files that you want to delete and that time you can use rm to delete multiple files. 

Here is an example, 

$rm file1.txt file2.txt

The above command deletes two files (file1.txt and file2.txt) using the rm command in Linux. 


Delete all files with the same extension –

To delete all files in that particular directory, open the terminal in that directory and type the following command. 

$rm *.txt 

The above command will delete all files which have txt extension. You can replace “txt” with ZIP, jpg, or any other extension of files available in that directory. 

There are many options available with the rm command which makes deleting the file easier in Linux type OS. 

Also read: Do these 6 things right after installing Kali Linux


Using the interactive mode in rm – 

We know that rm does not ask for confirmation while deleting a file but we can use interactive mode to confirm before deleting. 

To use the interactive mode with rm command, append ‘-i’ with rm command followed by the filename. 

Here is the example, 

$rm -i file1.txt

The above command will ask for confirmation before deleting the file. This should usually be practiced to prevent accidental deleting of files. 


Using force mode to remove without prompting – 

If you are sure to remove the file and don’t want confirmations then you can use the force command. 

To use the force option, append ‘-f’ with rm followed by the filename. 

Here is an example, 

$rm -f file1.txt

Get the report of each file after deleting – 

Generally, Linux users are too cautious about what kind of file existed and how much space it acquired and other additional information. Once the file is deleted, you cannot know this information later. To solve this problem, you can use ‘-v’ to show the report of each file removed. 

Also read: How to check the version of Linux? Check Linux Version

Here is a quick example, 

$rm -v file*.txt

Using the above command you get the report of each file. 

You can use the following rm command to explain what is being done with the file. 

$ rm -v moiz.list.txt bios-updates.doc 

This command will show all the operations which are done on the files while removing them (Just like a log file). 

You can access the manual page of rm here


Remove a directory in Linux – 

To remove a directory in Linux using the rm command, you need to ensure that the directory is empty. You can remove the directory by using the following command. 

$rm -d filenames/ 

You can also remove an empty directory using the rmdir command instead of the rm command. 

$rmdir directory_name

The above command will remove an empty directory. 

To delete a directory that has files within it, you can use the recursive mode and delete the directory along with the content. 

Here is a quick example to remove a directory in Linux which is non-empty, 

$rm -r filenames/

The above command will remove the directory along with files within it.

You may find it difficult to confirm prompt every time you want to delete a huge number of files. The rm command can combine the options so it becomes easy to delete files in Linux. 

Here is an example of how you can combine rm options to delete files in Linux, 

$rm -rf filenames/ 

The above command can be used to remove a directory in Linux along with its content and without a confirmation prompt. 

To remove multiple directories in Linux, use the following command – 

$rm -r dirname1 dirname2 dirname3

Here, dirname signifies the name of the directory. 

Never use rm -rf as the root user or superuser on your Linux operating system. This command will delete all the writable files on your computer and crash the OS. 

Never use these commands – 

$rm -rf /

$rm -rf *

Using unlink command to delete files in Linux – 

unlink command is not that powerful as compared to rm command. You get only two options with unlink command, –help option and –version option. 

Also, unlink command cannot delete multiple files and directories like the rm command. You can delete only a single writable file using this command. 

To delete files in Linux using unlink command, use the following code in the terminal. 

$unlink file1.txt

Conclusion: 

I am sure that with our step by step guide you are now able to delete files in Linux and it is much easier now. Prefer using rm command over unlink, it is more powerful and clean in comparison. Make sure you take care of points noted in the first section while discussing the rm command. You cannot recover the file once you delete it so it is recommended to take a backup copy of your file if it is too important. One last thing about using the rm command is, do not use it as the root user or superuser with ‘/’ or ‘*’, you can end up deleting the whole operating system.

See Also: 9 Best App Lock Apps For Your Android Device You Must Use [2020]


Frequently Asked Questions –

What is the command to delete a file in Linux?

To delete files in Linux we use command-line options such as "rm" and "unlink" which will permanently delete files. You cannot restore them once you have deleted unless you have a backup of it.

What is RM in Linux?

rm (remove) command is a Unix/Linux command which is used to delete files from a filesystem.

What is Rm F?

"Force mode to remove without prompting" -f is used when the user is not interested to get confirmations for file deletion using rf command.

$rm -f file1.txt

What is RM R?

To delete a directory that has files within it, you can use the recursive mode and delete the directory along with the content. using -r you can use the recursive mode with rm. Here is an example,

$rm -r filenames/

How can I RM without confirmation?

To use the force mode, append -f with $rm and no more confirmation will be prompted when deleting the file.

$rm -f file2.txt


See Also: How to Start, Stop and Restart Apache Ubuntu – Apache2

Leave a Reply

Your email address will not be published.