Linux rm Command: rm -rf Options and Deleting Without Confirmation

The rm commanddeletes files immediately, without sending them to a trash folder. rm is short for remove and deletes files and directories. This post covers basic usage, the options, and why the root directory survives even if you run [rm -rf /].
On AlmaLinux 9.8 the root account comes with [alias rm='rm -i'] by default, so it asks for confirmation before deleting. For Linux systems that do not have this setting, the post also shows how to add the alias yourself.
✅ Basic Usage
The basic syntax is as follows.
rm -options target: file or directory
A file is deleted by name alone; a directory is deleted only when you add the -r option.
# Delete a file
rm file1
# Delete several files
rm file1 file2
# Delete a directory and everything in it
rm -r dir1/
# Delete an empty directory
rm -d dir1/~]# rm dir1/
rm: cannot remove 'dir1/': Is a directoryIf you name a directory without an option, it is not deleted, as shown above.
Deleting a symbolic link removes only the link; the file it points to stays in place. To check before and after deleting, use the ls commandalongside it.
✅ rm -rf: Deleting Without Confirmation
Running the rm command asks [remove regular file] because, on the root account, rm runs through the [rm -i] alias.
~]# rm file1
rm: remove regular file 'file1'? y
~]# alias rm
alias rm='rm -i'Deleting a directory with the -r option asks for confirmation on entering each directory and for every file, which gets tedious as the number of files grows.
~]# rm -r logs/
rm: descend into directory 'logs/'? y
rm: remove regular file 'logs/access.log'? y
rm: remove regular empty file 'logs/error.log'? y
rm: remove directory 'logs/'? yIn that case, add the -f option: it deletes without confirmation and reports no error even when the file does not exist.
rm -rf logs/Putting a backslash [\] in front of the command to bypass the alias, or putting [yes |] in front so that y is entered at every prompt, gives the same result.
\rm -r logs/
yes | rm -r logs/✅ rm Command Options & Simulator
| Option | Description | |
|---|---|---|
| -f | --forceDeletes without confirmation and reports no error for files that do not exist | |
| -i | Asks the user for confirmation before every deletionalias rm='rm -i' | |
| -I(capital i) | Asks only once when there are four or more files or when used with -r | |
| -r, -R | --recursiveDeletes a directory together with its subdirectories and files | |
| -d | --dirDeletes an empty directory | |
| -v | --verbosePrints each deleted file and directory on its own line | |
| --preserve-root | Does not delete the root directory (/) (default) | |
| --no-preserve-root | Turns off the protection of the root directory (/) |
✅ rm -rf /: Protecting the Root Directory
In hosting work, files that should not be deleted sometimes get deleted. Most of them can be restored from a backup, but once the root directory is deleted, recovery is a hassle even with a snapshot. I have heard several times of cases where a mistyped rm -rf command could not be undone.
The rm command applies the [--preserve-root] option by default, so the root directory is not deleted even if you run [rm -rf /].
~]# rm -rf /
rm: it is dangerous to operate recursively on '/'
rm: use --no-preserve-root to override this failsafeIt does not stop [rm -rf /*], however. The shell replaces [/*] with directory names such as /bin, /etc and /home before passing them on, so the rm command deletes each of those directories rather than [/].
A fresh AlmaLinux 9.8 install has the -i aliases for the rm, cp and mv commands in [/root/.bashrc], as shown below. Regular user accounts do not have these aliases.
~]# grep alias /root/.bashrc
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'On an account or a Linux system without the alias, add one line at the bottom of [~/.bashrc] and reload it.
# Add at the bottom of ~/.bashrc
alias rm='rm -i'
# Reload it
source ~/.bashrc
ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.