48

I'm running zsh from Cygwin. One of my shell functions contains a statement

rm -f somedir/*

(I want to remove all non-hidden files in somedir, but not the directory itself). However, I am always asked:

zsh: sure you want to delete all the files in ... [yn]?

The wording of this message (note the "zsh:" at the beginning) suggests that the question comes from zsh, not rm. However, rm is an external command:

$ type rm
rm is /usr/bin/rm

By the way, the prompt also occurs if I explicitly invoke rm as

$ command rm -f somedir/*

Is there something within zsh, which tries to be too clever?

Braiam
  • 1
  • 11
  • 47
  • 78
user1934428
  • 19,864
  • 7
  • 42
  • 87

2 Answers2

75

It seems that the RM_STAR_SILENT is NOT in effect.
You could do setopt rmstarsilent either in the command line or in ~/.zshrc to tell zsh to not confirm a rm *.

The shell option RM_STAR_SILENT is:

Do not query the user before executing rm * or rm path/*.

-- zshoptions(1): RM_STAR_SILENT


If you want to make the setopt effect temporally just in that shell function only, you could use it in conjunction with the localoptions like below:

my-test () {
  setopt localoptions rmstarsilent
  ...
}
Les Nightingill
  • 5,662
  • 1
  • 29
  • 32
hchbaw
  • 4,894
  • 18
  • 16
  • 1
    Thanks for adding the bit about `localoptions`! That is exactly what I needed. I like this feature, except in specific functions where I'm clearing log files, I don't want to be prompted then! – verboze May 10 '18 at 16:59
  • Ii is still unclear to me, how to call rm command without prompt. BTW in which language the code above is written? – vahotm Jan 03 '19 at 09:43
  • How to make this change permanent? I need to do this every time I start a new shell. – philippos Jun 23 '20 at 15:14
  • 6
    To use locally, just run `setopt localoptions rmstarsilent` in the terminal. To make permanent, add that line to `~/.zshrc` and reload the terminal. The example in the answer is showing using it in a shell function. – d3vkit Oct 24 '20 at 22:18
  • 3
    It is absolutely unclear to me how this is not a bug of zsh, when `-f` is explicitly wanted. The doc on `rm` `-f` says: **never prompt**. zsh should not overwrite this in my opinion. At least it does not promt when called from a script. – kuga May 13 '22 at 15:51
  • Instead of `setopt localoptions rmstarsilent` can we do: `setopt rmstarsilent`? – alper Jan 30 '23 at 13:19
-10

You need to recursively set permissions on all files and folders within the directory somedir to rw, and then execute:

sudo rm -rf somedir

Here's a video of my successful use of sudo for this command in a ZSH shell.

James Bush
  • 1,485
  • 14
  • 19
  • 1. `sudo` has nothing to do with the `rm` confirmation 2. if you're running a command with `sudo`, you don't need to set permissions as the superuser can delete anything anyways – Felds Liscia Feb 20 '22 at 23:03
  • @FeldsLiscia - Have you tried both your way and mine? If not, you might be as surprised as I was that “super doing” is not always the same as “super being” (at least not in this case)… – James Bush Feb 22 '22 at 01:40
  • 2
    The reason `sudo` behaves differently than just being root is that `sudo` doesn't execute commands in a shell. Running `rm ./*` in `zsh` will generate the warning even if you're root, but using `sudo` means no shell is started under the superuser, so there's no `zsh` between your input and the system to generate the warning. See https://stackoverflow.com/a/37817783 – eritbh Mar 09 '22 at 01:35
  • Post a video of what you’re talking about, and I’ll do the same. – James Bush Mar 10 '22 at 02:36
  • @eritbh See my amended answer, which now links to a video showing my solution at work. – James Bush Mar 10 '22 at 03:10
  • I never said your method didn't work, it's just a bad answer because you're becoming root for something that doesn't require you to be root. Your method works, not because of the elevated permissions, but because `sudo` doesn't run its input under a shell. There are other mechanisms for executing programs without a shell that don't require root; for example, `sudo -u $USER` will have the same effect as `sudo` here (though it still doesn't seem like a better option than the accepted answer). – eritbh Mar 11 '22 at 03:55
  • @eritbh So, you watched the video I posted, then? At what specific point did I make an error or a necessary maneuver? – James Bush Mar 12 '22 at 05:46