Rm Command in Linux

Introduction

The command-line utility rm is used to remove files and directories. It is one of the most important commands that every Linux user should know.

This tutorial will show you how to use the rm command by providing examples and explanations of the most common rm options. We will also address a few FAQs on rm command in Linux.

How to Use the rm Command

The rm (remove) command has the following general syntax:

rm [OPTIONS]... FILE...

When run without any options, rm does not remove directories and does not prompt the user to continue with the removal of the given files.

To delete a single file, use the rm command as an argument, followed by the file name:

rm filename

You will receive a "Operation not permitted" error if you do not have write permissions on the parent directory.

If the file is not write protected, it will be deleted automatically. If the command is successful, it produces no output and returns zero.

The command will prompt you for confirmation when removing write-protected files, as shown below:

Output

rm: remove write-protected regular empty file 'filename'?

To delete the file, type y and press Enter.

The -f flag instructs rm to never prompt the user and to disregard nonexistent files and arguments.

rm -f filename

If you want to know what is being removed, use the -v (verbose) option:

rm -v filename
Output

removed 'filename'

Removing Multiple Files

Unlike the unlink command, rm lets you delete multiple files at the same time. Pass the following filenames as arguments, separated by a space:

rm filename1 filename2 filename3

Regular expressions can be used to match multiple files. To remove all .png files in the current directory, type the following command:

rm *.png

When using regular expressions, it is always a good idea to list the files with the ls command before running the rm command so that you can see which files will be deleted.

Removing Directories (Folders)

To eliminate one or more empty directories, use the -d option:

rm -d dirname

The rm -d command is functionally equivalent to the rmdir command.

Use the -r (recursive) option to remove non-empty directories and all files within them:

rm -r dirname

Prompt Before Removal

The -i option instructs rm to prompt the user before removing each specified file:

rm -i filename1 filename2

Type y and press Enter to confirm:

Output

rm: remove regular empty file 'filename1'? 
rm: remove regular empty file 'filename2'? 

When removing more than three files or removing a directory recursively, use the -I option to get a single prompt for the entire operation:

rm -i filename1 filename2 filename3 filename4

You will be asked to confirm the deletion of the following files and directories:

Output

rm: remove 4 arguments? 

rm -rf


If the specified directory or a file within it is write-protected, the rm command will prompt you for confirmation. Use the -f option to remove a directory without being prompted:

rm -rf dirname

The command rm -rf is extremely dangerous and should be used with extreme caution!

FAQs to Rm Command in Linux

Can I use the "rm" command to delete multiple files at once?

Yes, you can pass multiple file names as arguments to the "rm" command to delete several files simultaneously. For example: rm file1.txt file2.txt file3.txt

What happens if I use the "rm" command to delete a directory?

By default, the "rm" command does not remove directories. If you attempt to use it directly on a directory, you will receive an error message. To remove a directory and its contents, you need to use the "-r" (or "--recursive") option.

How can I delete a directory and its contents using the "rm" command?

To delete a directory and its contents recursively, use the following syntax:rm -r DIRECTORY_NAME

Can the "rm" command delete files and directories permanently without confirmation?

Yes, the "rm" command can delete files and directories permanently without confirmation if you use the "--force" (or "-f") option. However, exercise caution with this option, as it bypasses most safety checks.

Is there a way to display a confirmation prompt before deleting files with the "rm" command?

Yes, to display a prompt for confirmation before deleting each file, use the "--interactive" (or "-i") option. This option provides an extra layer of safety by prompting you to confirm each deletion.

Can the "rm" command delete hidden files and directories?

By default, the "rm" command does not differentiate between hidden and non-hidden files or directories. It will remove hidden files and directories as long as you have the necessary permissions.

Are there any other useful options available with the "rm" command?

Yes, you can use various options with the "rm" command, including:

  • "-r" or "--recursive" to delete directories and their contents.
  • "-f" or "--force" to delete files without confirmation.
  • "-i" or "--interactive" to prompt for confirmation before each deletion.
  • "-v" or "--verbose" to display additional information during the deletion process.
  • "--help" to display the command's help documentation.

Conclusion

We demonstrated how to remove files and directories from your Linux system using the rm command.

When deleting important files or directories, use extreme caution because once a file is deleted, it cannot be recovered.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.