3

I want rm to prompt me when recursively deleting a directory, as in:

$ rm -r <dir>
Do you want to remove the dir(Y/N)?

Is there an argument I can pass to rm that will do this?

migrated from stackoverflow.com Oct 29 '14 at 14:31

This question came from our site for professional and enthusiast programmers.

  • And what if you give several arguments, some dirs, and others regular files? – gniourf_gniourf Oct 28 '14 at 21:08
  • Try rm -i command – anubhava Oct 28 '14 at 21:09
  • Well zsh shell has this kind of warning for rm -rf * – user2991100 Oct 28 '14 at 21:12
  • 4
    "I usually like to alias rm to rm -rf, so that it gets easier to remove a directory" -- right, because rm -f isn't quite dangerous enough. – Keith Thompson Oct 28 '14 at 21:17
  • In general, "please write this thing for me" questions aren't welcome on StackOverflow -- and neither are "how do I configure this non-programming-related tool that comes with my operating system?" – Charles Duffy Oct 28 '14 at 21:20
10

What you want is uppercase -I, as in:

$ rm -rI tg/
rm: remove all arguments recursively?

As noted in the comments, this is something nice GNU's rm is giving you, and not required based on the posix spec for rm.

  • 1
    Which platform is this valid on? (It's not in the POSIX spec for rm; is it GNU? Mac? ...?) – Charles Duffy Oct 28 '14 at 21:40
  • @CharlesDuffy GNU does it. I don't know about other systems, though. – cmaster Oct 28 '14 at 21:43
  • @CharlesDuffy thanks! I've updated my answer – ErlVolton Oct 28 '14 at 21:47
  • 1
    I highly recommend that you don't alias either one of these, but instead build yourself the habit of using the option you want. If you do an alias, and get used to it, and then you're on a system that doesn't have the alias, you'll be in for an unpleasant surprise when your rm -r suddenly removes everything, without asking. – lindes-hw Oct 30 '14 at 17:57
  • I agree, however I think rmdir is sort of OK because on another system it would just refuse to remove anything at all. – ErlVolton Oct 30 '14 at 17:59
1

Depending on what OS you're on, there's rm -i (which will prompt for each individual file) or perhaps rm -I (part of GNU rm), which, per the man page, will "prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes"

I highly recommend against creating an alias for this, and instead build the habit of using one or the other of the above options. Being on a system without the alias you've gotten used to can create much grief (removing files you didn't intend to remove). Using the habitual option when you actually don't want it just means a minor annoyance (being prompted a few times, and either answering yes to each prompt, or breaking out (C-c) and starting again without the option). The latter is much less painful, I find.

  • He doesn't want lowercase -i, he specifically asked for a single prompt. Also you'll notice the question was updated a while ago to remove everything about aliases – ErlVolton Oct 30 '14 at 18:05
  • 2
    Well, he may not have the option of rm -I, so that's why I included it in my answer. I see you've updated you answer to remove the aliasing (I guess thanks to my comment; maybe I should have left it at that; I never saw it in the question), so this answer becomes a bit redundant now. Perhaps I'll withdraw it; certainly, I'll now upvote your answer, now that it doesn't suggest using an alias. – lindes-hw Oct 30 '14 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.