117

I'm looking for a solution to be used as a response to "rm: remove write-protected regular file [x] ?"

I was thinking of issuing a character followed by carriage return for several amount of times, in bashrc. How do we do that?

6
  • You can define a function. This thrad may help you: stackoverflow.com/questions/756756/….
    – alpert
    Commented Apr 18, 2013 at 12:07
  • Why do you want to do this? It's a very un-obvious task to want to print carriage returns (as opposed to newlines) in any case, and in .bashrc?
    – l0b0
    Commented Apr 18, 2013 at 12:59
  • @l0b0 question edited to be more specific
    – Iancovici
    Commented Apr 18, 2013 at 13:06
  • Why would you want to remove files in .bashrc? The original task is still not clear.
    – l0b0
    Commented Apr 18, 2013 at 13:09
  • 3
    @echadromani It's very common on SO/SE to try to understand the original problem to provide an optimal solution which may be applicable to other situations.
    – l0b0
    Commented Apr 18, 2013 at 13:14

5 Answers 5

141

Edit based on updated question:

To avoid being asked about removing files, add the -f ("force") option:

rm -f /path/to/file

This has one side effect you should be aware of: If any of the given paths do not exist, it will not report this, and it will return successfully:

$ rm -f /nonexistent/path
$ echo $?
0

Original answer:

Here's one simple solution:

yes "$string" | head -n $number | tr $'\n' $'\r'

yes repeats any string you give it infinitely, separated by newlines. head stops it after $number times, and tr translates the newlines to carriage returns. You might not see any output because of the carriage returns, but passing it to this command (in bash) should illustrate it:

printf %q "$(yes "$string" | head -n $number | tr $'\n' $'\r')"

Users without bash can pipe the result to od, hexdump or xxd to see the actual characters returned.

28

rm is hardcoded to ask interactively on write protected files. "interactively" means it will print a question and then wait for user input.

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 want rm to never be interactive but still want to handle errors.

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


bonus: if writing script and the filename to delete comes from outside you should use sanitary measures to protect against funky filenames and malicious input.

rm -r --interactive=never -- "./$filename"

the important parts: double dash (--) marking end of options to protect against filenames starting with dash; quotes to protect against spaces in filename; and the leading dot slash (./) to doubly protect against filenames starting with dash and also force all files to be relative from working directory.

see here for more info: https://www.cyberciti.biz/faq/unix-linux-remove-strange-names-files/


for an answer the stated question ("How to avoid the need to issue “y” several times when removing protected file") see https://askubuntu.com/questions/338857/automatically-enter-input-in-command-line/338860#338860

1
  • Great answer, better than the accepted one! In particular I like demonstrating the difference between options. Commented Mar 16 at 11:05
11

The other issue I've run into from time to time is that rm is aliased to rm -i, something like this in the /etc/bashrc:

alias rm='rm -i'

In that case you can either unalias rm or you can use this trick that I found out years ago, put a backslash in front of a command that's been aliased, to ignore the alias just that one time, for example:

\rm somefile

You can learn more about aliases through an article at Nixcraft.

3

just give yes to all your commands!

yes | rm -r /path/
yes | <command>

Anyways you can always force using -f:

rm -r -f /path
-2

I too ran into same issue. The above answer is just for one file but if you want to ignore lots of yes.

You can use

sudo rm -r /path/to/directory

to remove all write protected regular file

3
  • This is being downvoted because it removes the entire directory. Presumably OP only wants to remove the file in question, not every single file in the same directory subtree.
    – Jake
    Commented Aug 26, 2017 at 9:53
  • Hey.... May be you are right. Commented Aug 27, 2017 at 11:09
  • Hey.... May be you are right. But not in this case.. As if a user have lots of patience and time, then he also have time to give 'yes' several time... I have assumed that he want to remove all file with one command.. As write protected file always ask for confirmation before deleting this type of file.. One such example is in the case when you create virtualenv and later you want to remove in python.. Please try it and you will find rm command asking for confirmation and each time you have to supply an answer.. I have resolved the situation in this way. Commented Aug 27, 2017 at 11:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .