Linux / Command / rm / Command to delete files or directories

The rm command in Linux deletes files, and using the -r option also deletes directories and their contents. Since it generally does not go through the recycle bin, make sure to check the path and the items to be deleted before executing the command.

Things to check before deletion

This article is based on GNU coreutils' rm and the Bash shell. Files deleted with rm may be difficult to recover by normal methods. Back up important files separately, and check your current location and target first with pwd and ls -la.

rm does not display a confirmation message every time. The default behavior depends on file write permissions, whether input is from a terminal, etc., and alias settings in the shell may also affect it. To confirm each file explicitly, use -i. For detailed conditions, refer to the GNU rm manual.

Deleting Files

Below is an example of creating a new practice directory and preparing files to delete inside it. If rm-practice already exists in the current location, use another empty location, and if the first command fails, do not continue executing.

mkdir rm-practice
cd rm-practice
printf 'practice\n' > sample.txt
ls

After confirming that sample.txt is displayed, execute the command below. In the confirmation message, enter y to delete, or enter n if you do not want to delete.

rm -i sample.txt
ls

If you approved the deletion, sample.txt will no longer appear in the ls results. rm sample.txt can delete the file without confirmation, so do not assume that a confirmation message will always appear.

Deleting Directories

To delete an empty directory only, you can use rmdir. It usually fails if the directory is not empty.

mkdir empty-dir
rmdir empty-dir

To delete directories with contents, use -r. The command below creates practice directories and files, verifies them, and then deletes them. Using -i together will ask for confirmation for each item.

mkdir example-dir
printf 'practice\n' > example-dir/file.txt
ls -la example-dir
rm -ri example-dir

The confirmation messages may ask about entering directories and deleting files and directories. Before answering each question, verify the name and approve only the items you intend to delete. Refusing some questions may result in the directory remaining or displaying errors.

Wildcards and hidden files

Patterns like *.txt are expanded by the shell to matching names before rm executes. Before deleting multiple files at once, first check the matching names with a command like the following instead of the delete command.

printf '%s\n' ./*.txt

After confirming that the output names are the intended targets, use rm -i ./*.txt only when necessary. If there are no matching files, the pattern may be displayed as-is or the command behavior may change depending on the Bash settings. Enclosing the entire pattern in quotes prevents the usual filename expansion.

In default Bash settings, * does not include hidden items that start with a dot, but this may vary depending on settings like dotglob. Commands that delete a wide range like rm -rf * without checking can cause significant damage if the current location and expansion results are misjudged. The differences for each setting can be checked in Bash filename expansion documentation.

Frequently used options and paths

  • -i: Confirm each item to be deleted. Items that are denied will not be deleted.
  • -r: Recursively delete the contents inside the directory.
  • -f: Ignore targets that do not exist and do not display confirmation messages. This option does not solve permission errors.
  • -v: Output the deleted items to help verify the results.

Paths with spaces should be enclosed in quotes like rm -i "my file.txt". If the file name starts with a hyphen, specify it after -- like rm -i -- -draft.txt to prevent it from being interpreted as an option.

If there is a permission error, check if you have the necessary permissions in the parent directory of the target, and consult the administrator. Whether deletion is possible is not determined solely by the write permission of the file itself. After deletion, check the directory listing again, and if you have deleted a file you need, immediately stop the operation and check the backup or recovery procedure of the production environment.