Remove file without asking

94

22

How can I remove file without asking user if he agrees to delete file? I am writing shell script and use rm function, but it asks "remove regular file?" and I really don't need this. Thank you.

hey

Posted 2011-10-12T19:28:12.043

Reputation: 1 616

5rm -f, yes | rm and so on, but this belongs to SU. – None – 2011-10-12T19:30:31.750

4rm doesn't show a "remove regular file?" prompt by default. You must have it aliased to rm -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.sh or source foo.sh)? – Keith Thompson – 2011-10-12T19:56:27.973

Answers

107

You might have rm aliased to rm -i so try this:

/bin/rm -f file.log

To see your aliases you can run alias.

chown

Posted 2011-10-12T19:28:12.043

Reputation: 1 679

1If "rm" is a function (instead of an alias), this answer should work. And the bash unset command might be interesting – Xen2050 – 2015-02-25T14:15:11.463

7Alternatively, use command rm ... or \rm ... to skip the alias/function – glenn jackman – 2011-10-12T20:26:45.553

6It's been argued that having rm aliased to rm -i is a bad idea. The rm command, by default, silently removes the named file. By aliasing it to rm -i, you can get into the habit of not checking carefully before pressing Enter, depending on the interactive prompt to save you. Then you type rm some-important-file in an environment without the alias. – Keith Thompson – 2011-10-12T21:25:34.190

@Keith That is very true, i only personally alias rm to rm -v – chown – 2011-10-12T21:33:50.963

61

The force flag removes all prompts;

rm -f {file}

lynks

Posted 2011-10-12T19:28:12.043

Reputation: 1 451

2This is what I am looking for, not the accepted answer. – gsamaras – 2015-12-14T13:12:13.463

34

May the force be with you - rm -f

manojlds

Posted 2011-10-12T19:28:12.043

Reputation: 2 068

5I have already upvoted lynks' answer, and this is exactly the same. Just upvoting for the sense of humour – rain_ – 2017-06-21T09:59:48.800

I upvoted both the comment AND the answer just because someone actually found a sense of humor on SO... – Mac – 2019-04-18T16:53:28.840

6

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)

mholm815

Posted 2011-10-12T19:28:12.043

Reputation: 196

The yes n option is not working for me. While I use yes n | rm file.txt, it actually removes the file even though the file is right protected. – iammilind – 2015-12-08T06:07:01.277

5

\rm file

Backslash \ bypasses aliases.

teknopaul

Posted 2011-10-12T19:28:12.043

Reputation: 298

1" \ " is a backslash, which do you mean? – Cand3r – 2016-07-28T12:32:50.087

Yes it does, to use rm without aliases resulting in unwanted confirm dialogues prefix with . – teknopaul – 2016-08-02T09:46:15.113

5

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

Kassym Dorsel

Posted 2011-10-12T19:28:12.043

Reputation: 436

1The "remove regular file?" prompt implies that it's not a permissions problem. – Keith Thompson – 2011-10-12T21:23:19.640

5

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).

Musannif Zahir

Posted 2011-10-12T19:28:12.043

Reputation: 151

unalias rm doesn't work in CentOS? Amazon Linux? – Suncatcher – 2019-10-21T20:42:51.900

1

Apart of using -f parameter, alternatively you can use find command instead, e.g.

find -name file.log -delete

kenorb

Posted 2011-10-12T19:28:12.043

Reputation: 16 795

0

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.

Zhu Jinxuan

Posted 2011-10-12T19:28:12.043

Reputation: 1

0

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

PatrykMilewski

Posted 2011-10-12T19:28:12.043

Reputation: 1

0

rm -Rf <folder-to-be-deleted>

-R: Recursive f: force, no prompt

mdivk

Posted 2011-10-12T19:28:12.043

Reputation: 113

0

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).

daw

Posted 2011-10-12T19:28:12.043

Reputation: 101

That may have solved your problem but it does not answer the question. – suspectus – 2015-02-25T13:57:37.443

Wouldn't the selected answer - calling /bin/rm work? 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 – 2015-02-25T14:12:22.223

Yes, /bin/rm works. Doing the unset removes the need for typing the full path. – daw – 2015-02-25T16:03:42.370