1. Overview

Linux offers various tools to manage files and directories. One of these tools is rm, which helps to remove files. When we use the command, we need to understand how to use it correctly since it permanently erases data. Luckily, rm conveniently provides various options to customize the output and ensure the efficient deletion of files.

In this tutorial, we discuss the basics of the rm command, exploring its syntax, options, and examples.

2. Understanding rm

So, rm uses a basic syntax:

$ rm [OPTIONS] FILE

[OPTIONS] enables us to include different flags to customize the behavior of rm. Additionally, FILE represents the file or directory to delete.

2.1. Removing Files

To remove a single file, we use rm followed by the filename:

$ rm file1.txt

Above, we remove the file1.txt file.

Additionally, we can remove multiple files at once:

$ rm file1.txt file2.txt file3.txt

In this command, we specify various files separated by spaces. Alternatively, we can use the wildcard character (*) to delete multiple files. For instance, let’s say we have the following files in the current directory:

$ ls
errors.log  file1.txt  file2.txt  file3.txt  file.sh

We can specifically delete files with a specific file extension:

$ rm *.txt

This command deletes the file1.txt, file2.txt, and file3.txt. So, the other files are not removed since they don’t have the .txt extension.

Also, we can remove files with filenames that start with file followed by a single number from 1 to 3:

$ rm file[1-3]*.txt

file[1-3] expands to file1, file2, and file3. The command deletes the file1.txt, file2.txt, and file3.txt files.

2.2. Removing Directories

By default, the rm command doesn’t delete directories.

First, let’s use the -d option to remove an empty directory:

$ rm -d projects

However, when we try to delete a non-empty directory using the above command we get an error:

$ rm -d Movies
rm: cannot remove 'Movies': Directory not empty

So, let’s remove the Movies directory and its contents:

$ rm -r Movies

Now, this command recursively removes the directory and all its contents with the help of the -r option.

2.3. Additional Options

First, we can use the -v option to instruct rm to print a notification for each file deleted:

$ rm -v file1.txt file2.txt file3.txt file4.txt
removed 'file1.txt'
removed 'file2.txt'
removed 'file3.txt'
removed 'file4.txt'

This command displays detailed information about the deletion process, including the names of the deleted files.

Also, we can use the -f option with rm to forcefully remove write-protected files:

$ rm file1.txt
rm: remove write-protected regular empty file 'file1.txt'?

Since file1.txt is write-protected, rm prompts for confirmation. Now, let’s override this:

$ rm -f file1.txt

Here, -f overrides the prompt for confirmation and ensures the file is removed forcefully.

Additionally, we can use rm in combination with the -i option to prompt a user for confirmation before proceeding with file deletion:

$ rm -i file1.txt file2.txt
rm: remove regular empty file 'file1.txt'? y
rm: remove regular empty file 'file2.txt'? y

If file1.txt exists, rm prompts the user for a confirmation message. If the user enters y for yes, rm continues to delete file1.txt. On the other hand, if the user enters n for no, rm cancels the deletion operation without removing the file1.txt file. It works the same in the case of file2.txt. So, the -i option requires the user to explicitly confirm before deleting files, which helps to prevent the accidental deletion of important files.

3. Conclusion

In this article, we explored the rm command.

By understanding the rm command’s options and its best practices, we can efficiently manage the file system. Importantly, we need to use rm carefully to avoid accidentally deleting important files. This is because once we delete a file with rm, it can only be recovered from a backup.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments