rm command in Linux with examples (some are absolutely dangerous)

 rm command in Linux with examples (some are absolutely dangerous)



rm command in UNIX stands for remove and by default is used for removing files. It is simple but a powerful command especially when used with options such as -rf which allow it to delete non-empty directories forcefully.

Removing Files in Linux:

The rm command, by default, cannot remove Directories and only works on files. 

$ mkdir A

$ touch B.txt

$ rm B.txt

$ rm A       #will not work because A is a directory

Removing Multiple Files in Linux:

To remove multiple files at once, we can write file names separated by spaces after the rm command or use a pattern to remove multiple files that fit the pattern.

$ rm a b

$rm *.txt

Removing a Directory in Linux:

To remove a directory, you can use the -r or -R switch, which deletes a directory recursively including its content (subdirectories and files). If it is an empty directory you can also use rmdir command.

$rm A             #will not work because A is a directory

$ rm -R A/

Removing Files with Confirmation Prompt:

To get a confirmation prompt while deleting a file, use the -i option.

$ rm -i a.txt

Removing Directories with Confirmation Prompt:

To get a confirmation prompt while deleting a directory and its sub-directories, use the -R and -i option.

$ rm -Ri A/

---------------------important separate line--------------------------------

Removing File or Directory Forcefully:

To remove a file or directory forcefully, you can use the option -f force a deletion operation without rm prompting you for confirmation. For example, if a file is unwritable, rm will prompt you whether to remove that file or not, to avoid this and simply execute the operation.

$ rm -f a.txt

When you combine the -r and -f flags, it means that you recursively and forcibly remove a directory (and its contents) without prompting for confirmation.

$ rm -rf B             #even the file or directory is write-protected

Showing Information While Deletion:

To show more information when deleting a file or directory, use the -v option, this will enable rm command to show what is being done on the standard output.

$ rm -rv *

Is rm -rf Command bulletproof?

rm -rf as powerful as it is, can only bypass read-only access of nested files and not directories. To delete the directory ( B/C ) we need to access it through superuser privileges.

It is not recommended to use this command as a superuser if you are not 100% sure what you are doing as you can delete important files.

The “rm -Rf /” Command:

You should always keep in mind that “rm -rf” is one of the most dangerous commands, that you should never run on a Linux system, especially as a root. The following command will clear everything on your root(/) partition.

$ sudo rm -rf /


Featured Posts

SnowPro Badges and Certificates

SnowPro Badges and Certificates Online Verification https://achieve.snowflake.com/profile/richardhou888/wallet

Popular Posts Recommended