10

There are a lot of questions about how to undo a finished rm command. In my case, my rm started asking: "Are you sure you want to delete this file?"

And I need to confirm for each one of them. Instead of manually confirming "y/n":

  1. Can I set "yes" to the remainder while being asked for y/n for the n'th file? (equivalent to "Yes to All" tickbox on Windows) (I know the yes | rm trick before starting)

  2. Can I abort the operation while being asked for y/n for the n'th file? (except for shutting down the machine :))

Andrejs
  • 315
  • 2
  • 4
  • 10

4 Answers4

19

To accomplish request 1 you will need to use a more sophisticated program than yes to send y N number of times and then pass keyboard input through beyond that. You can't do it with rm except to always ask (rm -i) or to never ask (rm -f).

You can always abort rm by

  • pressing Control-C to interrupt it (sends SIGINT),
  • pressing Control-Z to stop it (sends SIGSTOP) and then killing it,
  • sending a SIGTERM (kill), or
  • sending it a SIGKILL (kill -9).

This won't undo any file operations rm has already performed, but they will prevent it from performing any further file operations. If the rm process is currently prompting for user input it is not actively unlinking any files so killing it will merely prevent it from continuing.

casey
  • 14,754
  • What about yes | head -9 | rm... to get 9 (for example) yes's? – Jeff Schaller Nov 05 '15 at 01:51
  • 2
    @JeffSchaller That would get to the 9th y/n without input but you wouldn't be able to answer y/n to the 10th. – casey Nov 05 '15 at 02:15
  • 1
    One could use { yes | head -n9; yes n; } | rm -i … to delete first 9 files, then answer, "no" for any remaining files (and of course the { … } part could be expanded to cover any possible combination of numbers). – zrajm Nov 05 '15 at 09:55
  • 4
    ...though I don't see what it could be useful for. If you know which files you want unliked/not unlinked, why not pass the correct arguments to rm instead of building twisted, unreliable commands? – spectras Nov 06 '15 at 04:52
10

GNU rm supports -i, which I assume is what you are referring to and asks for confirmation for each file, but also -I, which is a little different:

-I     prompt once before removing  more  than  three  files,  or  when
       removing  recursively.  Less intrusive than -i, while still giv‐
       ing protection against most mistakes

As to whether it's possible to stop an interaction with rm after it has started, hitting Ctrl-C should do it.

dhag
  • 15,736
  • 4
  • 55
  • 65
2

with Ctrl+c you will be able to cancel the operation.

But if you look in your .bashrc file, you can comment the alias that force the confirmation, it use the -i param as expained dhag.

in you .bashrc you will find a entry like this :

alias rm='rm -i'

if you comment it, your rm -f will works directly

lese
  • 2,726
1

Under bash, you can bypass an alias by prepending a \ to the command, so

rm filename

becomes

\rm filename
Andrew Henle
  • 3,780
  • 2
    I don't see how this has something to do with my question of "how to interrupt the rm process" – Andrejs Nov 04 '15 at 20:22
  • @AndreyDoronin It addresses your usage of approaches such as I know the yes | rm trick before starting – Andrew Henle Nov 04 '15 at 20:24
  • @AndreyDoronin> it tells how not to end up having to ask how to interrupt the rm process for the reasons you gave in your question. – spectras Nov 06 '15 at 04:54