How can I remove a file without asking the user if he agrees to delete the file? I am writing shell script and use rm function, but it asks "remove regular file?" and I really don't need this.
14 Answers
You might have rm aliased to rm -i so try this:
/bin/rm -f file.log
To see your aliases you can run alias.
- 2,157
- 1
- 15
- 11
-
10Alternatively, use
command rm ...or\rm ...to skip the alias/function – glenn jackman Oct 12 '11 at 20:26 -
6It's been argued that having
rmaliased torm -iis a bad idea. Thermcommand, by default, silently removes the named file. By aliasing it torm -i, you can get into the habit of not checking carefully before pressing Enter, depending on the interactive prompt to save you. Then you typerm some-important-filein an environment without the alias. – Keith Thompson Oct 12 '11 at 21:25 -
-
1If "rm" is a function (instead of an alias), this answer should work. And the bash
unsetcommand might be interesting – Xen2050 Feb 25 '15 at 14:15 -
May the force be with you - rm -f
- 2,178
-
I upvoted both the comment AND the answer just because someone actually found a sense of humor on SO... – Mac Apr 18 '19 at 16:53
the yes program repeatedly replies yes to any prompts. so you can pipe it into the interactive rm program to get the desired effect too.
yes | rm <filename>
conversely, if you want to not do something interactive, you can do
yes n | <something interactive>
and that will repeat 'n' on the input stream (effectively answering no to questions)
- 226
-
The
yes noption is not working for me. While I useyes n | rm file.txt, it actually removes the file even though the file is right protected. – iammilind Dec 08 '15 at 06:07
If you have the required permissions to delete the file and you don't want to be prompted, do the following (-f = force):
rm -f file
If you don't have permissions to the file, you will need to use:
sudo rm -f file
- 151
- 436
-
1The "remove regular file?" prompt implies that it's not a permissions problem. – Keith Thompson Oct 12 '11 at 21:23
Within a shell script, you would want to use rm -f <filename> but you also have the option of getting rid of the implicit -i option for your environment by entering unalias rm in your shell (or profile).
- 151
Apart of using -f parameter, alternatively you can use find command instead, e.g.
find -name file.log -delete
- 25,417
My favourite way to do this is simply use command command in bash, just the same way you use sudo. This will run your command without aliases, just like running it by /bin/rm (probably rm is aliased as rm -i).
Example:
command rm -f /tmp/file.txt
- 111
If your rm is aliased to rm -i, then use unalias rm;
Do not use rm -f directly unless you really want to remove a lot of write-protected files. There must be a very good reason to use -f.
However, if you have a lot of write-protected files, you might prefer to rsync -r --delete empty/ removed_dir/ for a faster speed.
- 101
rm -Rf <folder-to-be-deleted>
-R: Recursive f: force, no prompt
- 123
-
nooo. just no. if recursive was not already on the command... this can tank the whole system in some cases like a newbie trying to remove all hidden files with
rm */.*(one example) – Eric Sebasta Mar 20 '20 at 05:34
If there is a folder containing everything you want to delete, this can help cd directory find . -print |xargs rm -r
- 1
-
This seems more complex than some of the other answers. Why should someone use this instead? – Charles Kenyon Mar 23 '21 at 21:44
Another thing I noticed is that if I remove multiple files/dirs through in one go like the following then the rm command asks for confirmation.
sudo rm -rf dir1 dir2
If I remove them them one by one then it doesn't ask for confirmation.
sudo rm -rf dir1
sudo rm -rf dir2
- 101
Currently I am working at a system, where the bash shell recieved the definition of the rm command as a function in one of the global configuration files:
rm () { /bin/rm -i ${1+"$@"}; }
Hence, none of the above answers regarding aliases did work. To counter the annoying behaviour I unset the rm function in my .bashrc file
unset -f rm
I had a similar problem then the opener. However I did not found any answer that mentioned the possibility that rm is hidden by a shell function. So I added the answer here in the hope it would be of help for somebody facing the same type of problem.
Typing /bin/rm or rm -f all the time is inconvenient, and may have bad consequences (in the case of rm -f).
- 101
-
That may have solved your problem but it does not answer the question. – suspectus Feb 25 '15 at 13:57
-
Wouldn't the selected answer - calling
/bin/rmwork? If I have a function in bash, and an executable file with the same name in the current directory, just adding./in front of the name will call the file - not the function. PS - I added a comment to the selected answer about a function &unset– Xen2050 Feb 25 '15 at 14:12 -
Yes,
/bin/rmworks. Doing theunsetremoves the need for typing the full path. – daw Feb 25 '15 at 16:03
rm -f,yes | rmand so on, but this belongs to SU. – Oct 12 '11 at 19:30rmdoesn't show a "remove regular file?" prompt by default. You must have it aliased torm -i, or defined as a function. I'm surprised that the alias is visible inside your script. Are you executing the script (./foo.sh) or sourcing it (. foo.shorsource foo.sh)? – Keith Thompson Oct 12 '11 at 19:56