2

I want to delete a directory with its files and I want to do that as follows:

rm -r dirToDelete\

Unfortunately, I always get asked for EACH single file if I want to delete this because it is write-protected.

Is there a way to suppress this feedback message so that the whole directory with its contents disappears?

1
  • 1
    rm -rf is the popular answer (and not wrong). rm -r --interactive=never is the correct answer. technically correct. the best kind of correct.
    – Lesmana
    Apr 18, 2018 at 10:53

8 Answers 8

8

A lot of distros alias "rm" to "rm -i". Personally, I think that's the stupidest idea ever, so first do a "alias rm" to see if that's the case, or just verify if "/bin/rm" behaves differently. If it is, look in /etc/profile, /etc/profile.d, /etc/bashrc or your own .profile or .bashrc to see where it's set and remove it.

3
  • 2
    Might be an idea to elaborate /why/ it's stupid, for example, that doesn't add the same protection against accidential use to, for example, system calls that delete files, other programs that also delete files, and so on.
    – Arafangion
    Sep 28, 2010 at 14:01
  • 1
    @Arafanion: Foremost this is a bad idea because if you are used to the interactive behavior you can screw up easily on other hosts with sane "rm".
    – user16115
    Sep 28, 2010 at 14:10
  • 6
    The main reason it's stupid is that it changes the default behaviour of a standard command. So either rm doesn't work the way you expect it because it's interactive, or it stops working the way you expect it when you go to another system because you started expecting it to be interactive. Sep 28, 2010 at 14:25
6

You want rm -rf.

From the rm man page:

-f, --force
              ignore nonexistent files, never prompt
2
  • You probably also want super-user or ubuntu.
    – Kendrick
    Sep 28, 2010 at 13:53
  • @Kendrick you might also want to do --no-preserve-root on Ubuntu as well, just to be safe </sarcasm>.
    – Nitrodist
    May 11, 2011 at 16:00
4

rm asks for confirmation because it's aliased to rm -i. To bypass the alias for a single command, add a backslash in front of the command:

\rm -r dirToDelete

For rm, you can also cancel out the -i with a -f:

rm -rf dirToDelete
3

Add -f e.g. rm -rf dirToDelete - but be careful to get the directory name right.

Note: this question isn't really programming related so really should be asked on superuser instead.

2

I hope that rather soon distros which do

alias rm='rm -i`

switch to

alias rm='rm -I'

from the man page:

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

I suggest you modify your .bashrc to still protect you from mistakes ;-)

1

I understand this is specifically a Linux question, however it's the first I found when searching for this question and I am on macOS 10.15.

I could not get any of the above mentioned methods to work with zsh:

rm -rf

rm -r

rm -r -interactive=never

i.e., I was given the interactive mode regardless.

Gilles's answer led me to the solution in my case...

\rm -r dirToDelete

The shell was using its builtin rm instead of /bin/rm, so using /bin/rm bypassed the shell builtin and allowed me to turn off interactive mode with /bin/rm -r.

Oddly prefixing a backslash, \rm, did not work as I have read that it does (can't recall where I read this on stack exchange and couldn't find it with a search).

It's possible \rm works with bash and not zsh - but I have not tested that.

0

rm is hardcoded to ask "interactively" (prompt waiting for user input) on write protected files. there are two methods to prevent rm from asking:

rm -rf somedir

and

rm -r --interactive=never somedir

(both also work without -r when deleting files instead of dirs)

explanation:

-f makes rm to "ignore nonexistent files and arguments, never prompt".

--interactive=never does what it says: never be interactive. in other words: never prompt.

the difference between -f and --interactive=never is this part: "ignore nonexistent files and arguments".

compare:

$ rm -rf nonexistingname
$ echo $?
0

and

$ rm -r --interactive=never nonexistingname
rm: cannot remove 'nonexistingname': No such file or directory
$ echo $?
1

the difference is mainly interesting when writing scripts where you never want rm to be interactive but still want to handle errors.

summary: on command line use rm -rf. in scripts use rm -r --interactive=never.

0

The only way I can execute this non-interactively on Ubuntu 20.04 with Oh-my-Zsh is using dash:

sh -c 'rm -rf my_directory/*'

You must log in to answer this question.