2

The default behaviour of the GNU rm command when asked to delete write-protected files is to interactively ask whether the user wants to delete each of the files. This is quite inconvenient in many situations.

A crude general way to supply an interactive command invocation with no meaningful input is to use a here-string. I expected that in this case,

touch wpf && chmod -w wpf rm wpf <<< "" 

would result in the command being aborted, i.e. the file still being there afterwards, much like when I try rm wpf and just hit enter or ctrl+d upon the prompt.

To my surprise however, it actually results in the file being deleted.
(Ubuntu 18.04, GNU Bash 4.4.20, coreutils 8.28)

What's going on here, and does it also have ramifications for similar situations?

11
  • Unsurprisingly, echo "" | rm file will also delete it. Looks like rm detects whether its input is linked to a terminal or not. Commented Sep 12, 2022 at 14:05
  • according to man rm Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or the -i or --interactive=always option is given, rm prompts the user for whether to remove the file. Commented Sep 12, 2022 at 14:18
  • my understanding is that If one condition is not met, rm will silently delete Commented Sep 12, 2022 at 14:19
  • 2
    @Archemar sounds like an answer! I think you're right and that's where it's documented. In this case, standard input is not a terminal. Commented Sep 12, 2022 at 14:22
  • I'm still amazed by information found (sometime) in man :D Commented Sep 12, 2022 at 14:23

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.