Manipulate files in Linux: rm command

M

The rm command (abridged from remove) deletes a file or a directory. The syntax of this command is simple:

rm [options] file

Brief description of the most used options:

f – will automatically respond with y to all confirmations. It deletes files immediately without confirmation; if both -f and -i occur, the last one that is used in the terminal is taken into account by rm. Deleting a folder that has very high content is done using the -rf options together;
i – prompts the user before deleting any file;
q – suppresses all warning messages, but displays error messages;
R – stands for recursive deletion and is used to delete a directory along with all of its contents (all files and subdirectories it contains). When used by a regular user, it requires confirmation to enter a directory and then delete each individual file;
r – identical to -R – this will cause all the files, directories contained, and then the original directory to be deleted;
v – displays the name of the output file as it is processed (deleted), for each performed operation.
I – Requests user permission whenever more than 3 files are deleted or recursive deletion.
Using * as a parameter causes you to delete all the files contained but not the directories; to delete them use rmdir -r *.

The options presented are only the most frequently used ones; for more information, read the man page of the console by typing $ man command_name in a terminal.

By default, Linux does not have a recycle bin for the rm command executed on the command line. If a file was deleted using the rm command, it is lost and can not be recovered unless we have a backup or if we are using low-level disk maintenance tools (such as debugfs).

Every time, the rm command must be done with particular care, especially when using it as a root user. Be careful when using the -R option, because it can even destroy the Linux installation!

Eplaining the sudo rm -rf / command

Everyone who came into contact with Linux sooner or later saw the warning: DO NOT use sudo rm -rf /! Now knowing the rm command options, let’s see what this command means:

1. sudo – allows this command to be used as administrator (or superuser or root);
2. rm – a delete command;
3. –rf – tells the rm command to erase recursively (one after the other in the tree structure of the indicated directory) and without confirmation (immediately);
4. / – well, this slash means we tell the rm command to delete the contents of the main root directory, that is, all the operating system files – including the contents of the mounted Windows disks or partitions.

In other words, this command deletes without warning immediately the entire operating system!

Another form of this command is sudo rm -rf / * – the wildcard * tells the rm command to delete any file from the specified directory, regardless of its name.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories