How to Remove(Delete) Files or Directories in Linux?
In Linux, if we want to remove files and directories, we can use the
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/
-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 (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.
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.
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
Here is how to use this commad,
rm [OPTION]... FILE...
rm command options:
The following table contains a brief description of options available with the
||Adding this flag will remove the directory or a file no matter what. This stands for forceful removal.|
||It is used to display a message and confirm with the user before every removal.|
||It is used to prompt(same as
||It is used to prompt as per the condition we provide.|
||It is used when removing a hierarchy recursively.|
||It is used to specify that the root should not be treated differently, and it can also be removed.|
||This flag is the default option too and doesn't allow removing of the root directory.|
||It is used to remove empty directories.|
||It is used to remove directories and their contents recursively.|
||It is used to explain what is being done. Every filename is printed on the console which is removed by the command.|
||It is used to display help related to the
||It is used to get version information fr the
Example: Delete a file from your storage using the
In this example, by using the
rm command, we will delete a file dubey.cnf that is available in the shadow directory.
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.
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.
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.
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.