1. Introduction

When working in Linux terminals, many of us use the rm command to remove files and directories. But since rm works by removing file or directory links and metadata from the filesystem, recovery is nearly impossible. So, we always err on the side of caution when using this command.

One failsafe we love to use alongside commands for removing files and directories is a confirmation alert. With a confirmation alert, resources are not deleted immediately after we run the command. We get one last chance to verify the operation.

In this tutorial, we’ll talk about how to enable confirmation alerts while removing files and directories. We’ll also go over what to expect when running various file and directory removal commands in Linux.

2. When Using the rm Command

When we use the rm command, we can enable confirmation alerts using the -i or -I option. But while both options throw a confirmation prompt, their use cases differ.

2.1. The -i Option

The -i option displays a prompt for every file we try to delete with the rm command. In other words, if we try to delete ten files using rm -i, we’ll get ten confirmation prompts. Similarly, if we try to delete 30 files, we’ll get 30 prompts.

So, if we try to delete five empty files named one, two, three, four, and five using rm -i, we’ll see a confirmation alert for each file:

$ rm -i one two three four five
rm: remove regular empty file 'one'? yes
rm: remove regular empty file 'two'? yes
rm: remove regular empty file 'three'? yes
rm: remove regular empty file 'four'? yes
rm: remove regular empty file 'five'? yes

Using rm -i to delete five empty files gave us five prompts, and we had to confirm each removal with a ‘yes’. We could also use ‘y’ to confirm the removal. If we don’t want to delete any of the files, we could answer ‘no’ or ‘n’ to the prompt corresponding to the file.

We can manually add the -i option to rm each time we remove a file or directory. But to ensure that we do not mistakenly run rm without -i, we could replace rm with rm -i using the alias command:

$ alias rm="rm -i"

Replacing rm with rm -i using the alias command as we did above is temporary. Once the terminal session ends, rm will return to its default mode. But if we want rm to revert to its default mode during the session, we can unalias:

$ unalias rm

To make the alias permanent, we can add the command to one of the shell configuration files that run on startup. In our case, we’ll add the alias to the ~/.bashrc file:

$ echo 'alias rm="rm -i"' >> ~/.bashrc

After adding the alias to .bashrc, we’ll restart the system for it to take effect. To return rm to its default mode, we’ll remove the command from .bashrc and restart the system.

The alias will only work for the user that owns the .bashrc file. Therefore, we must add it to the .bashrc files of other users if we want the same for them.

2.2. The -I Option

Unlike the -i option, the -I option prompts for just one confirmation when we are deleting at least four files at once. So, if we try to delete four empty files named one, two, three, and four using rm -I, we’ll see:

$ rm -I one two three four
rm: remove 4 arguments?

We can respond y, yes, n, or no to the prompt depending on what we want, and the command will act accordingly. If we tried to delete fewer than four files using rm -I, there would’ve been no prompt, and the files would’ve been removed.

If you prefer -I over -i, you can create a temporary or permanent alias for it as we did for -i above.

3. Other Commands Used to Remove Files and Directories

Many of the other commands used to remove files or directories don’t have an option for a confirmation prompt. But then, for some of them, the context in which we use them doesn’t require confirmation:

  • rmdir only removes empty directories. So, confirmation is not necessary.
  • We typically use shred and similar commands when we’re pretty sure we do not want to recover a file. So, a confirmation prompt wouldn’t be of much use.
  • unlink can only delete a file at a time, and it is not a popular command for removing files. So, it does not have an interactive prompt.
  • While srm comes with an interactive option (-i) on FreeBSD, the same option is not available on Debian distros at the moment.

4. Conclusion

In this tutorial, we went over how to enable a confirmation prompt when removing files and directories in Linux. We also talked about how to create aliases for the rm -i.

While we used rm as the alias for rm -i, we could have used any other string so long as the said string isn’t already a command. However, to achieve our goal of not mistakenly running rm without -i, we had to use rm as the alias.

Comments are closed on this article!