Linux

How to Delete Files and Directories in Linux

You may, in some cases, need to delete directories and files that you do not need to free up space on your hard drive. In this brief tutorial, you will learn how to delete files and directories in Linux.

To remove files, the rm command is used. A word of caution, using the rm command to delete files and directories in Linux is irreversible. Therefore, extra caution should be taken.

Deleting a file

To delete a single file using the rm command, use the syntax below

$ rm filename

For example, we will create a text file file1.txt using the touch command. To delete the file run

$ rm file1.txt

Output

removing a single file with rm command in Linux

Deleting multiple files

To remove many files in a single command using the rm command, use the syntax

$ rm file1 file2 file3

Once again,  we will create 3 files – file1.txt file2.txt file3.txt using the touch command. To remove 3 files –  – in one command run

$ rm file1.txt file2.txt file3.txt

Output

remove several files with a single command

This result can also be achieved by making use of the wildcard symbol (*). In the example below, The wildcard matches and deletes all files with the extension .txt

$ rm *.txt

Remove files interactively

If you wish to confirm whether to remove files before deleting them, use the -i flag as shown

$ rm -i file1.txt

Output

remove a file interactively

Force remove files

To remove files forcefully, especially ones that have been write-protected, use the -f flag as shown below

$ rm -f file1.txt

Output

force remove a file using rm -f command

Removing empty directories

If you want to remove an empty directory, use the -d flag with the rm command

$ rm -d directory_name

Let’s create an empty directory and call it data

To remove the directory run

$ rm -d data

Output

remove empty directory in Linux

The same result can be achieved using the rmdir command. This is short for remove directory. The syntax is as shown below.

$ rmdir directory_name

In our second example, we are going to create a new directory and call it linux . We will then remove it using the rmdir command.

Output
remove directories in Linux using rmdir command

Deleting non-empty directories

If you want to remove non-empty directories together will all files, use the -r flag as shown

$ rm -r directory_name

In this example, we have a directory called distros containing 5 files namely centos, debian, fedora and mint.

To remove the entire directory alongside its contents run

$ rm -r distros

Deleting multiple directories recursively

If you wish to delete or remove multiple directories at once use the syntax below

$ rm -r directory_1 directory_2 directory_3

For instance, to remove 3 directories namely data1 data2 & data3 run

$ rm -r data1 data2 data3

Output
remove multiple directories recursively

Forcefully and recursively remove directories

If you want to recursively and forcefully remove directories without being prompted, , use the combination of  -rfflag as shown below

$ rm -rf directory_name

Wrapping up

Using the examples in this article, you now know how to delete a file in Linux safely. We value your feedback. Drop us a comment in the comment section. Keep it locked for more informative and insightful articles.

Facebook Comments

Show More

Leave a Reply

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

Back to top button