Signup/Sign In
LAST UPDATED: JULY 13, 2021

How to Remove(Delete) Files or Directories in Linux?

In Linux, if we want to remove files and directories, we can use the rm command.

The following command is used to remove a directory recursively, which means it will remove its subdirectories too, which means complete removal:

rm -rf /path/of/directory/

The -r flag is for recursive removal and -f is for forceful removal. So use this command to completely remove/delete a directory and whatever is in it.

To remove a single file, run the following command,

rm myfile.txt

The rm (remove) command can be used to remove files and directories both. You can use the full path or a relative file path to specify the location of the file to delete.

The rm command doesn't delete a directory by default, to remove a directory we should either use the -d flag or the -r flag which we used in the example above.

The rm command is a silent killer, that is it works silently and will not print any output message on the screen when it completes successfully. The rm command generates messages only in the case of an error so you should be careful while using this command.

Syntax of the rm command:

Here is how to use this commad,

rm [OPTION]... FILE...

Linux rm command options:

The following table contains a brief description of options available with the rm command.

Options Description
-f, --force Adding this flag will remove the directory or a file no matter what. This stands for forceful removal.
-i It is used to display a message and confirm with the user before every removal.
-I It is used to prompt(same as -i flag) once before removing more than three files.
--interactive[=WHEN] It is used to prompt as per the condition we provide.
--one-file-system It is used when removing a hierarchy recursively.
--no-preserve-root It is used to specify that the root should not be treated differently, and it can also be removed.
--preserve-root This flag is the default option too and doesn't allow removing of the root directory.
-d, --dir It is used to remove empty directories.
-r, -R, --recursive It is used to remove directories and their contents recursively.
-v, --verbose It is used to explain what is being done. Every filename is printed on the console which is removed by the command.
--help It is used to display help related to the rm command.
--version It is used to get version information fr the rm command.


Example: Delete a file from your storage using the rm command.

In this example, by using the rm command, we will delete a file dubey.cnf that is available in the shadow directory.

linux rm command

Example: Delete an empty directory

In this example, myfolder is an empty directory. It is not possible to delete a directory by just using the rm command. So for deleting a directory you have to use -dir option along with the command.

linux rm command

Example: Delete a file using rm command with -i (Interactive Deletion) option

In this example, we are using the -i option with the rm command that asks the user for confirmation before removing each file.

linux rm command

Example: Delete file starting with a - (hyphen) symbol

If any file use -(hyphen) in its name then we can not delete it directly by using the rm command. We must use -- (double hyphen) separately before the file name, just like options, to remove the file.

This is the way rm command does not misinterpret the file name as an option.

linux rm command

Conclusion

In this tutorial, we covered how to remove files and directories using the rm command with available options and suitable examples. rm command is similar to the del command in MS-DOS and Windows operating system.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.