1. Overview

Being in the habit of running the rm command to delete files might get us into trouble. We may unintentionally remove important files for good. Although this action might be irreversible, we can still cancel rm when running it on a list of files. Additionally, there are also other ways to get around accidental deletions.

In this tutorial, we’ll explore how to cancel the rm command and prevent accidental file deletions.

2. Can We Recover Files After an rm Command?

The short answer is usually no, at least not without proper prearrangements. When a file is deleted with rm, the data isn’t really gone, but our system deletes the link to the file. This means the rm command marks the inode and storage block of the deleted file as unused so the system can add in different data.

Of course, deleting the link to the file means our data can still be recoverable if we use the proper ways. Another simple way to restore data is by backing up our system regularly.

3. Canceling rm on Multiple Files

Linux offers different ways to stop a command in progress, and these can also work to cancel rm.

3.1. Interrupting rm

If we issue an rm command on multiple files, we might be able to stop the operation by simply pressing Ctrl + C, i.e., sending an interrupt signal.

Let’s try this on a directory with 10000 files using the -v flag (verbose) to see exactly what happens:

$ rm -v *
removed '3301'
removed '3302'
removed '3303'

We used the * globbing character to match all files in the current directory. Most importantly, the ^C (Ctrl + C) key combo instructs the terminal to send an interrupt signal (SIGINT) to the rm process. This means the deletion action should stop right away.

3.2. Killing rm

Linux commands, including rm, might ignore the SIGINT signal. However, we can work around that by using the kill command to send a SIGKILL. This signal is also a great way to cancel an rm running in the background.

To do that, we simply run the ps command to find the process identifier (PID) of the rm instance:

$ ps 
  PID TTY          TIME CMD
2143 pts/1    00:00:00 bash
1033 pts/1    00:00:00 rm
1495 pts/1    00:00:00 ps

After that, we launch a kill command on the target PID (1033) using the -9 flag, which is the identifier of SIGKILL:

$ kill -9 1033

Canceling an rm process doesn’t mean reversing the deletion effect. What’s done is done, we can only stop rm from deleting further files.

4. Confirming Deletion

The rm command has a couple of useful options that can potentially reduce deletion mistakes.

4.1. On Every rm Instance

To mitigate accidental file removal, we can use the -i option (short for –interactive) to instruct rm to prompt us if we’re certain we want to proceed with the operation.

Let’s run rm on a file that we don’t actually want to delete:

$ rm -i bouhannana.txt 
rm: remove regular empty file 'bouhannana.txt'? n

The command prompts us for a confirmation where we can answer with either y (yes) or n (no). After typing in n and pressing Enter, the rm command cancels the deletion process and the bouhannana.txt file remains intact.

We can set this behavior as the default by aliasing the rm -i command to just rm in our .bashrc or similar shell setup file:

alias rm='rm -i'

Now, whenever we execute the rm command, the confirmation prompt should appear, just like it would if we were using the -i option explicitly. Such an alias is part of some Linux distributions by default.

However, this comes at a cost. If we switch systems, the rm command may not prompt a confirmation.

4.2. On a Group of Four Files or More

Getting a confirmation prompt on every file could be annoying. That’s why rm offers -I, an alternative interactive switch triggered only if we’re deleting four files or more in one go.

Let’s try to delete three files and see:

$ rm -I baeldung.py smara.jsp 33.txt

No prompt, the rm command deletes the files without waiting for confirmation.

Now, let’s run it on more than three files:

$ rm -I important.doc secret.txt cicada.sh zack.jpeg
rm: remove 4 arguments? n

We can notice the current rm interactive mode automatically kicks in when detecting 4 files. This switch is less intrusive than -i and instructs rm to wait for user confirmation only when launching a group file deletion (more than three).

4.3. Launching an rm Simulation

There are also ways to preview the files that rm would delete, which is helpful when using globbing patterns to target certain files. We can think of this solution as listing the files we want to delete with an rm command.

Let’s say we want to run rm *.txt to delete all text files in a directory. We simply use an echo (or similar) command instead of rm to view what’s going to be deleted:

$ echo *.txt
secret.txt 33.txt nitro.txt

If we’re sure about the output above, we can go ahead and switch back to rm to delete those files safely. Running this kind of trail may be useful to avoid executing an rm with the wrong pattern or path.

5. Using a Recycle Bin or Trash

If we’re having doubts when deleting files or simply if we want the possibility to undo the deletion action, we can use the trash tool instead of rm. This command lets us move files into a trash directory using the terminal. Almost all Linux desktop environments have this Recycle bin implementation to manage trashed files.

5.1. Installing cli-trash

First, let’s install the cli-trash package on our current distro (Debian):

$ sudo apt install trash-cli

This package is also available in other distros, including Arch Linux, Fedora, and openSUSE.

5.2. Trashing Files

After getting cli-trash installed, let’s type in trash followed by the name of the file we want to delete temporarily:

$ trash zack.dat

Now let’s use the ls command to list and find our trashed file in the trash directory:

$ ls ~/.local/share/Trash/files

From there, it’s easy to just copy or move back the file to undelete it. We can also empty the trash directory and delete its content permanently simply by issuing the rm command on all existing files.

5.3. Managing Trashed Files

The cli-trash package also comes with other commands:

  • trash-list: list the files in the trash directory
  • trash-restore: undelete files
  • trash-empty: empty the trash directory
  • trash-rm: permanently delete specific files from the trash directory

This way, we can seamlessly list, restore, or permanently delete trashed files without relying on an outside command.

6. Conclusion

In this article, we learned how to cancel rm while deleting a group of files and ways to mitigate unintentional deletion using its interactive mode. We also discussed using the trash command to delete files temporarily instead of deleting them outright with rm.

In conclusion, using the rm interactive mode might mitigate accidental data deletion, but using trash always offers a second chance so we can undelete files.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.