rm - Unix, Linux Command



The rm command is a powerful Linux command that deletes files, directories and symbolic links from your Linux file system. The rm command is the abbreviation of remove that works silently on your Linux system and removes the file and directory with a single command.

The users should be careful while using the rm command because the files or contents of the directories deleted through the rm command cannot be recovered easily.

Read this guide to learn about the −

Syntax for rm Command

The basic syntax to use rm command in Linux is provided below −

rm [options] [file_name or directory_name]

Where you can use different options or flags in place of [options] to change the working of the rm command. You can also add your file_name or directory_name which you want to remove from your system.

Different Options Available for the rm Command

As already mentioned that rm command can be used with different options. These options will help you remove your specific file or directory from the system. You can find those options and their description in the table provided below −

Options Description
-r or R It recursively processes the directories, means it will operate on all files and subdirectories within a specified directory.
-d It deletes files or empty directories
-f Force the delete operation, means it will remove files or directories, even there is an error or warning.
-i Also called Interactive Mode, as it will ask for confirmation before removing the file or directories from the system.
-v It is a verbose output that is used for providing additional information during the execution.
-I It is used for case-sensitive matching, means it ignores the case-differences when deleting the files and directories.
--preserve-root It prevents the command to operate on root directory.
--no-preserve-root It overrides the previous option and allows the command to operate on root directory.
--one-file-system It restricts the operation to a single file system (doesn’t cross mount points).

Examples for the rm Command

Now, let’s go through some examples to better understand the working of rm command in Linux. With rm command, you can −

Removing a Single File from Linux

When you use rm command without an argument, it silently removes the file from your system without asking for your confirmation. However, for this command to work on Linux, you have to provide the accurate file name as an input argument.

In the following example, we are removing a file myfile.txt with the rm command using −

rm myfile.txt
rm Command 1

Note that you can only remove files from the rm command without an argument. If you remove the directory from the rm command without an argument, it will not remove it.

Here, as an example, we are trying to remove linux directory through rm command, but it fails to remove it from the system.

rm Command 2

Removing a Directory from Linux

To remove a directory from Linux with the rm command, you have to add -r flag as an additional option. By doing this, rm command will recursively remove the directory and the contents inside the directory. Like, the following command will remove the linux directory from the system −

rm -r linux

Note − Don’t forget to replace linux directory name with the directory you want to remove from the system.

rm Command 3

If you have an empty directory, you can also remove it from the rm command with -d flag, besides using the -r flag. The following example will remove the empty mydocs directory from the system −

rm -d mydocs
rm Command 4

To remove a subdirectory from Linux, simply use the -r flag with the path to the subdirectory. The below-given command will remove the subdirectory pics from the system −

rm -r docs/pics
rm Command 5

Removing a File or Directory from Linux with Approval Prompt

By default, the rm command silently removes the file or directory from the Linux system. However, you can change its behavior by using the -i flag. The advantage of using such a command is that a prompt will appear asking for your approval to delete the file or directory from the system. You can then reply it with y or n according to your choice.

The following command example will ask for the approval before removing the file hello.txt from the system −

rm -i hello.txt
rm Command 6

If you want to remove a directory through this way, you can add r argument with -i. The command provided below will ask for approval before removing the hello directory from the system −

rm -ir hello
rm Command 7

Removing Multiple Files or Directories from Linux Simultaneously

For multiples files or directories that you want to remove, use the rm command followed by the files or directories names with a space in between them. The following command will remove two directories docs and docs1 from the system simultaneously −

rm -r docs docs1
rm Command 8

The same goes for removing the files as well. Just add the file name followed by the space between them to remove them from the system.

Apart from that, you can also use the wildcard option to remove a set of files associated with the same name or extension. Like, if you have text files with .txt extensions, you can remove them from the system using the below-given command −

rm *.txt
rm Command 9

To include the file and folder with the same name and remove them, add the wildcard option after the name. Like, if you have directories and files with config as prefix, you can delete them with the following command −

rm config*
rm Command 10

Conclusion

The rm command is a versatile command used in the Linux systems for removing files or directories. With rm command, you can use different options like r, d, i, v and more. These options will change the behavior of the rm command.

You can use flags like r or d to remove directory with contents or empty directory from the system, respectively. To remove the silent removal feature of the rm command, you can use the i flag that asks for your approval before removing a file or directory from the system.

Hope you got a good understanding of the rm command in Linux with this guide.

Advertisements