On Linux/Unix systems, the rm command stands for “remove” and is used to delete files and directories.
In any operating system, deleting directories and files is one of the most basic, yet critical, functions. If you’re using Linux in a windowed environment, you can use any file management software to locate and delete those files.
Perhaps you’re using SSH to log in remotely, your Linux computer doesn’t have a graphical user interface installed, or you want more control over what you’re removing. There are a variety of reasons why deleting directories or files via the command line may be necessary or preferred in Linux.
The rm Command’s Syntax
The rm command has a straightforward syntax. There are two arguments required. One of them is completely optional.

Forceful removal of Directories/Files
If a file has the write-protected attribute, the rm command will notify you about the action. You’ll then have to confirm whether you want to remove the file or not. Using the ls command, you may see what permissions are configured for a file or directory.
We’ve provided an example of removing a file with read-only permissions to demonstrate this message.

If there is already a folder with that name, the result will be rm: remove write-protected regular file ‘clouddevelop’?
We can use the -f option to prevent having to answer to this message for every file that is write-protected.

This option instructs the rm command to remove the file by force. Any warning signals will be ignored, and it will attempt to delete the file regardless of any prompts.
This option will also remove directories when used in conjunction with the recursive argument.

When utilizing the recursive (-r) and force (-f) options together, be cautious. When paired with superuser rights, misusing these might swiftly delete a huge number of files, potentially causing damage to your operating system.
When you rm -rf, what happens?
The command operates by separating the filename from its accompanying data within the disk. That space is then identified as writeable, allowing the operating system to write data to those locations whenever it wishes. This means that if you make use of this command for deleting files, the contents will remain on the hard drive. It is now designated as a “free” space because it no longer has a filename connected to it.
The data is still on the storage device, but it is no longer accessible because the link has been broken. When you execute the rm command, you cannot revert a file removal.
Is the command rm -rf dangerous?
Always remember that “rm -rf” is one of the most destructive operations on a Linux system, and that you should never perform it as root. Everything on your root(/) partition will be cleared with the following command.

In Linux, create an alias for the rm command.
Using the -i option, you can instruct rm to always prompt you for confirmation before deleting a file or directory. Add an alias to your $HOME/.bashrc file to make this permanent.

Save your changes and close the document. Then, as mentioned, source your .bashrc file or open a new terminal to see the changes take effect.

This just means that rm will be run with the -i option by default every time you run it (although, the setting will be overridden when the -f flag is used).


When Used, Will the rm Command Delete the File?
The rm command actually unlinks a file from the drive rather than deleting it; nonetheless, the data will still remain on the disk, in which applications like Foremost, PhotoRec or Scalpel can be used to recover it.
If you truly want to destroy a file or directory permanently, you can use the shred command-line utility to replace it and hide its contents.
rm rf Commands in Use
If you don’t know how to properly use the rm rf command, it can be quite damaging to your system. The following are the most common rm-rf command combinations and options:
- To delete files in a Linux system, use the rm command.
- Without prompting, the rm -f command deletes read-only files from a folder.
- The rm -r command recursively deletes a folder’s contents.
- The rm -d command is used to delete an empty directory; however, if the directory is not empty, it will refuse to be deleted.
- All content in the root directory and sub folders is deleted using the rm -rf/ command (even if it’s write protected).
- The rm -rf* command is used to delete everything in the current directory (the one you’re working in) and its subfolders.
- The rm -rf command deletes everything in the current folder and its subfolders.
- The rm -rf command is used to delete everything in the current folder and its subfolders. You can also use the rm -r.[.]* command.
- The rm -i command is used to delete files and folders, but it will prompt you before doing so.
Best practices to avoid deleting a file or directory with the rm -rf command
Strategy 1: When using the rm command, one of the strategies is to start with #.

This prevents rm from being accidentally run on the wrong file or directory. After you’ve double-checked everything, delete the # from the beginning. This method works because in Bash, a word that starts with # is ignored along with the rest of the letters on the line. As a result, the command is simply disregarded.
Strategy 2: Using rm -rf * is one other approach you can use to keep any crucial directory from being deleted.

The *, in this case, expands -i to the command line, making your command rm -rf -i. As a result, before removing the command, it will prompt you. This file can be placed in your /, /home/, /etc/, and other directories.
Strategy 3: One other effective strategy entails adjusting your habits and refraining from using rm directly.
Running echo rm -rf /stuff/with/wildcards* first is one option. Check that the output from the wildcards is suitable, then execute the preceding command without the echo using the shell’s history.
Bonus Strategy:
Another option is to use the echo command just when you know exactly what you’ll be erasing. Remove the directory and create a new one instead of removing all the files in it. Renaming the existing directory DELETE-foo, then creating a new directory foo with proper permissions, and finally removing DELETE-foo is a reasonable method. Another advantage of this method is that the command rm -rf DELETE-foo is saved in your history.

If you absolutely need to remove a bunch of files but don’t want to delete the directory (because it must always exist or you don’t have the permission to create it again), move the files to a new directory and then delete the directory.

Final Words
Finally, it is strongly advised that you make regular backups. Back up your work whenever you create something you don’t want to lose. If you discover that backing up on a regular basis is too painful, then simplify the process. Use a program like git to mirror the code and retain history on another machine if you work on source code, for example. Have a script that rsyncs your files to another machine if you work with files.