The cp
(copy), mv
(move) and rm
(remove or delete) commands can all take a -i
option, which stands for “interactive”. However, the way this option works is subtly different with the rm
command than the other two, and in an attempt to stop you making mistakes it may succeed in doing the opposite.
cp
and mv
With the cp
and mv
commands, the -i
option will prompt before overwriting an existing destination file, but otherwise has no effect:
$ touch testfile $ cp -i testfile outputfile $ cp -i testfile outputfile cp: overwrite 'outputfile'? n $
That makes it a useful option: it’s rare that we want to copy or move a file over another one, and the prompt helps ensure we don’t do so by mistake.
rm
With the rm
command, the -i
option will always prompt:
$ rm -i testfile rm: remove regular empty file 'testfile'? n $
That may be useful if you have series of files where you only want to delete some of them, although I’d argue that isn’t the best way of achieving that result. However, the real danger is in aliasing the command.
Some distributions (for example, Red Hat and therefore CentOS) include the following in root
‘s .bashrc
:
alias rm='rm -i'
That means that every time the root user tries to delete a file, by default they will be prompted to confirm. The problem with this is that we humans are creatures of habit, and we quickly learn that typing y <return>
is the “correct” response. Unlike the similar prompt with cp
and mv
, the prompt doesn’t serve to warn us of a potential problem because it happens every time. Even if we have tried to delete the wrong file, we may not notice because of confirmation bias, which is a tendency to see evidence that supports our point of view (i.e, evidence that we typed the correct command).
In the aviation world, the tendency to carry out a certain action automatically in a given situation even when it may be the incorrect action is given a name: environmental capture. We respond automatically in a given environment, and if we almost always respond with y
when prompted, can we be sure that we will always catch a file about to be deleted in error?
The Fix
Part of the problem is relying on an alias to “fix” the problem of attempting to delete the incorrect file. That alias doesn’t exist on all Linux systems; even on those that ship with it by default, there’s no guarantee that it hasn’t been removed. In general, don’t rely on these aliases to avoid disaster. Part one of the fix is to remove the comfort blanket of the alias: it isn’t as good at protecting you from making mistakes as you might want.
If you want to delete a selection of files. move them to a temporary directory and, when you’re happy that it contains only files you don’t want, delete that directory.
Was This Linux Tip Helpful?
Let us know in the comments below.