Rm Command in Linux

Updated on

3 min read

Linux rm Command

rm is a command-line utility for removing files and directories. It is one of the essential commands that every Linux user should be familiar with. Whether you’re removing a few files or deleting entire directories, “rm” is a reliable and efficient tool that can save you time and effort.

This article explains, we will explain how to use the rm command through examples and explanations of the most common rm options.

How to Use the rm Command

The general syntax for the rm (remove) command is as follows:

rm [OPTIONS]... FILE...

By default, when executed without any option, rm doesn’t remove directories and doesn’t prompt the user for confirmation to remove the given files.

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

rm filename

If you don’t have write permissions on the parent directory, you will get an “Operation not permitted” error.

If the file is not write-protected, it will be removed without notice. On success, the command doesn’t produce any output and returns zero.

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

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

Type y and hit Enter to remove the file.

The -f option tells rm never to prompt the user and to ignore nonexistent files and arguments.

rm -f filename

If you want to see the files that are being removed, use the -v (verbose) option:

rm -v filename
removed 'filename'

Removing Multiple Files

Unlike the unlink command, rm allows you to delete multiple files simultaneously. To do that, pass the filenames to the command as arguments separated by space:

rm filename1 filename2 filename3

You can use regular expressions to match multiple files. For instance, to remove all .png files in the current directory, you would type:

rm *.png

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

Removing Directories (Folders)

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

rm -d dirname

rm -d is functionally identical to the rmdir command.

This option allow you to remove one or more empty directories without having to manually check each one to ensure it is empty.

To remove non-empty directories and all the files within them recursively, use the -r (recursive) option:

rm -r dirname

The command above will delete the specified directory, including all files, directories, or symbolic links within it.

Prompt Before Removal

The -i option tells rm to prompt the user for each given file before removing it:

rm -i filename1 filename2

To confirm type y and press Enter:

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

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

rm -i filename1 filename2 filename3 filename4

You will be asked to confirm the removal of all given files and directories:

rm: remove 4 arguments? 

rm -rf

The rm command will prompt you to confirm the operation if the given directory or a file within the directory is write-protected. To remove a directory without being prompted, use the -f option:

rm -rf dirname

Please be aware that the rm -rf command is very dangerous and should be used with extreme caution and care.

Conclusion

We have shown you how to use the Linux rm command to remove files and directories from your Linux system.

Be extremely careful when removing essential files or directories because once the file is deleted, it cannot be easily recovered.