Newsgroups: comp.unix.shell Date: Sat, 19 Nov 2005 18:50:48 +0000
> I wish there is a '-n' switch for rm (to default answer no) so as > not to remove write-protected file. How can I do that? Why "yes no | rm *" > won't work? > > touch a b c d > touch aa bb cc dd > chmod a-w ?? > > $ rm * > rm: remove write-protected regular empty file `aa'? ^C > > $ yes no | rm * > > $ ls | wc -l > 0 > > The yes command provide "no" to rm, but why it still deleted all my files? [...]
Because rm detects that it is not speaking to a human (because its standard input is not a terminal), so doesn't ask any question.
With zsh,
rm *(.Uw)
will only remove regular (.) files owned by yourself (U) and writable by yourself (w).
Stephane CHAZELAS
use find, like:
find -perm 644 -print0 | xargs -0r rm
You can have more options on setting up the permission bits with "find", for example:
find -perm +w -type f -print0 | xargs -0r rm
this delete all files having "write" at 'u','g', or 'o' bit
find -perm +w -type f -prune -o -print0 | xargs -0r rm
this selects all files skipped by the first command and then delete:
XC - xicheng