How to Use the Linux Command rm and Its 4 Options

The Linux Command rm is one of the most frequently used commands when you need to delete unnecessary files while using a Linux (or Unix) system. The rm command stands for “remove” and is used to delete files or directories. In this post, we will delve into the basic usage of the rm command, various options, and precautions to take when using it.

Basic Usage of the Linux Command rm

The rm command is very simple to use. The basic syntax is as follows:

rm 파일명
ShellScript

For example, if you want to delete a file named test.txt, you would type:

rm test.txt
ShellScript

When you enter this command, the test.txt file will be deleted. If the deletion is successful, no message will be displayed, and you will see the command prompt again.

Figure 1. Linux Command rm: File Deletion
Figure 1. Linux Command rm: File Deletion

Main Options of the rm Command

The rm command provides several useful options that can be used in various situations. Let’s explore the main options.

-i (interactive)

The -i option prompts a confirmation message before deleting a file. This helps prevent the accidental deletion of important files.

rm -i test.txt
ShellScript

When you enter this command, a message will appear asking if you want to delete the test.txt file before deleting it.

When deleting an empty file, it will prompt “rm: remove regular empty file ‘filename’?”

Figure 2. Linux Command rm with the -i option to delete an empty file
Figure 2. Linux Command rm with the -i option to delete an empty file

When deleting a non-empty file, it will ask “rm: remove regular file ‘filename’?”

Figure 3. Linux Command rm with the -i option to confirm deletion of a non-empty file
Figure 3. Linux Command rm with the -i option to confirm deletion of a non-empty file

In both cases, pressing Y will delete the file, while pressing any other key will cancel the deletion.

-r (recursive)

The -r option is used to delete a directory and all its contents, including subdirectories and files. You must use this option for non-empty directories.

rm -r test
ShellScript

The above command deletes the test directory along with all its files and subdirectories. In the figure below, you can see the contents of the test directory using the ls command, and then the deletion of the test directory and all its contents using the rm -r command.

Figure 4. Linux Command rm with the -r option for recursive deletion
Figure 4. Linux Command rm with the -r option for recursive deletion

-f (force)

The -f option forces the deletion of files without prompting any messages, even if the files do not exist or there are permission issues.

rm -f test.txt
ShellScript

Without the -f option, if you try to delete a non-existent file, you will see a message like “rm: cannot remove ‘filename’: No such file or directory”. However, with the -f option, no such message is displayed.

Figure 5. Error message when attempting to delete a non-existent file without the -f option
Figure 5. Error message when attempting to delete a non-existent file without the -f option

In the figure below, the file test.txt is owned by the root user and group. Without any options, the rm command will prompt “rm: remove write-protected regular file ‘filename’?” But with the -f option, it will delete the file without asking. Therefore, use the -f option cautiously to avoid accidentally deleting important files.

Figure 6. Linux Command rm with the -f option for force deletion
Figure 6. Linux Command rm with the -f option for force deletion

-v (verbose)

The -v option outputs the names of the files or directories being deleted, allowing you to see what is being deleted.

rm -v test.txt
ShellScript

In the figure below, the -v option is used to delete all files starting with test, and it shows which files are deleted. The directory test5 is not deleted because it is a directory. To delete test5 as well, you need to use the -r option.

Figure 7. Linux Command rm with the -v option showing detailed deletion results
Figure 7. Linux Command rm with the -v option showing detailed deletion results

Precautions When Using the rm Command

Data Recovery is Impossible

Once files are deleted with the rm command, they are completely removed and cannot be recovered by normal means. Therefore, always be cautious when deleting important files. If possible, create a backup.

If you are using Linux in a GUI environment, you might be able to delete files through a file manager, leaving them in the trash can.

Caution with Recursive Deletion

Be especially careful when using the -r option. When deleting an entire directory, there is a risk of accidentally deleting important data. Always double-check before using this option.

Caution with Force Deletion

The -f option is very powerful, as it bypasses permission issues and forcefully deletes files. Misuse of this option can result in the accidental deletion of system-critical files, so use it with caution.

Useful Applications

Bulk Deletion of Files with Specific Extensions

You can use the rm command with wildcards (*) to delete multiple files with a specific extension. For example, to delete all .log files, use:

rm *.log
ShellScript

This command deletes all .log files in the current directory.

Figure 8. Linux Command rm: Using wildcards to delete multiple files
Figure 8. Linux Command rm: Using wildcards to delete multiple files

Deleting All Files in a Specific Directory

To delete all files in a directory, use the rm command with the wildcard *. For example, to delete all files in the temp directory:

rm temp/*
ShellScript

This command deletes all files in the temp directory but not the subdirectories. To delete subdirectories as well, use the -r option:

rm -r temp/*
ShellScript

In the figure below, there are two files and one subdirectory in the temp directory. Using the rm temp/* command deletes the files but not the subdirectory. Using the -r option deletes all files and subdirectories in the temp directory.

Figure 9. Linux Command rm with the -r option to delete files and directories in subdirectories
Figure 9. Linux Command rm with the -r option to delete files and directories in subdirectories

Summary

The rm command is a highly useful tool for deleting files and directories in Linux. Understanding and utilizing the basic usage and main options can make file management much easier. However, given the powerful nature of this command, always keep the precautions in mind to avoid accidentally deleting important data. Making a habit of creating backups when necessary is also a good practice. I hope this post helps you use the rm command safely and effectively.

References

Leave a Comment