Linux Commands – rm

Linux Commands – rm

Linux Commands – rm

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed mkdir command in Linux which is used to create a directory in Linux filesystem.

https://cloudaffaire.com/linux-commands-mkdir/

In this blog post, we will discuss rm command in Linux. rm stands for remove and is used to remove files and directory. You can remove single, multiple, or multilevel file/directory using rm command.

Linux Commands – rm:

You can delete single or multiple files using rm command. You need to provide the full or relative path of the file with rm command in order to delete it.

You can delete single or multiple directories using rm command. You need to provide the full or relative path of the directory with rm command in order to delete it. Use rm -d option to remove empty directory and rm -r or -R or –recursive to remove directory recursively along with their content.

You can use rm -i to prompt before every removal.

You can also use rm -I option to prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes

You can use rm -v or –verbose to print what rm command is doing.

You can use rm -f or –force option to remove a file/directory forcefully. This option is very useful when dealing with non-existent files or write-protected files. -f or –force option disables the prompt.

You can use -f and -r command together with rm command to forcefully and recursively delete directory and its contents. Be careful with this command as it can cause massive damage if executed with sufficient permission like root.

As mentioned above, rm -rf is the most dangerous command that you can execute as it removes everything in the target directory without any prompt. You can imagine what will happen if you execute this command in the root directory (/) as root user, it will try to delete the entire Linux filesystem and break your system. Linux as a failsafe for rm -rf command has the option –preserve-root[=all] which is also rm default behavior that prevents deletion of root by running rm -rf / as root user. The –no-preserve-root option overrides this behavior. So, if you really want to destroy your system you can execute rm -rf –no-preserve-root / as the root user. I have not tested the last command so don’t know if Linux OS has any fail-safe to override this or not, besides which OS wants to destroy itself.

Hope you have enjoyed this article. In the next blog post, we will discuss mv command in Linux.

 

Leave a Reply

Close Menu