The rm command on UNIX stands for remove and, by default, is used to delete files. It’s a simple but powerful command, especially when used with options like -rf that allow you to remove non-empty directories with force.
Deleting files on Linux
The rm command,
by default, cannot delete directories and only works on files.
$ mkdir A $ touch B.txt $ rm B.txt $ rm A
We use the mkdir and touch commands to create directories and text files respectively, and the ls command to list the files in the current working directory.
Deleting multiple files on Linux: To delete multiple files
at once, we can type space-separated file names after the rm command or use a pattern to delete multiple files that fit the pattern
. $ rm a b $ rm *.txt [Pattern] Delete a directory
To delete a directory, you can use the -r or -R switch. that deletes a directory recursively, including its contents (subdirectories and files). If it is an empty directory, you can also use the rmdir command.
$ rm a/ $ rm -R a/Deleting files with confirmation message:To get a confirmation message when deleting a file, use the -i option. $ rm -i a.txt
Deleting directories with
To get a confirmation message when deleting a directory and its subdirectories, use the -R and -i options.
Ri A/Force deletion of file or
To delete a
file or directory forcefully, you can use the -f option to force a delete operation without being prompted by rm. For example, if a file cannot be written, rm will ask you if you want to delete 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 delete a directory (and its contents) without asking for confirmation
. $ rm -rf B
Here, we created a text file and a directory and made it read-only by taking their write access using the chmod command.
Displaying information during deletion:To
more information when deleting a file or directory, use the -v option, this will enable the rm command to display what is being done in the standard output.
$ rm -rv *
Is rm -rf
RM -RF As powerful as it is, it 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 superuser if you are not 100% sure what you are doing, as it may delete important files.
The command “rm –
You should always keep in mind that “
rm -rf” is one of the most dangerous commands, which you should never run on a Linux system, especially as root. The following command will erase everything on your root (/) partition.
$ sudo rm -rf /
There are checks to prevent root removal, but the additional –no-preserve-root option skips that fail-safe. It is a meme on the Internet that is equivalent to the removal of system32 on your Windows operating system C: drive.
$ sudo rm -rf / -no-preserve-root
You should not use the above command in any case, for curious people I made you use the command with -no-preserve-root. And after deleting some important files and directories, I was left with nothing but the posted output shown below.
=”https://media.geeksforgeeks.org/wp-content/uploads/20210225234110/Screenshotfrom20210225033608-660×43.png” alt=”What does the “rm -rf” command actually do on Linux?” />Create alias for
the rm command on Linux:
To permanently use the -i option for security, add an alias in your $HOME/.bashrc file. alias rm=”rm -i”Source your .bashrc file
as shown or open a new terminal for the changes to take effect.
$ source $HOME/.bashrc
Now, every time you run rm, it will be invoked with the -i option by default (but using the -f flag will override this setting).
Does rm really delete a file?
RM doesn’t actually delete files, but instead unlinks them (frees up memory for later use). To permanently delete the data, you can use the shred or dd command.