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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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
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')"
bash can pipe the result to
xxd to see the actual characters returned.
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:
You can learn more about aliases through an article at Nixcraft.
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
rm -r --interactive=never somedir
(both also work without
-r when deleting files instead of dirs)
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
--interactive=never is this part: "ignore nonexistent files and arguments".
$ rm -rf nonexistingname $ echo $? 0
$ 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.
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