46

I think the title is pretty self-explanatory. All i want is bash to warn me whenever I attempt to overwrite an existing while using cp or mv. I'd really appreciate some help. :)

sayantankhan
  • 1,691
  • 3
  • 15
  • 27

2 Answers2

59

You should use the interactive mode which makes sure you get a 'prompt before overwrite'

cp --interactive
mv --interactive

Or in short

cp -i
mv -i

Type man cp or man mv on your command line to find out more.

don.joey
  • 28,402
  • 17
  • 82
  • 104
  • 3
    So i guess those to commands go as aliases in my .bashrc as well. Thanks a lot. Appreciate your help. :) – sayantankhan Jan 05 '13 at 11:33
  • 5
    Indeed! You can append something like `alias rm='rm -i'` to your .bashrc or better your .bash_aliases. Read more in the post [How do I create a permanent Bash alias?](http://askubuntu.com/questions/17536/how-do-i-create-a-permanent-bash-alias) – don.joey Jan 05 '13 at 11:40
  • 2
    But i just thought of something. What about some of the scripts i've written. Will they use the aliased cp and mv or the normal one? – sayantankhan Jan 06 '13 at 09:44
  • @Bolt64 It depends on where you put your alias definition, but usually it will use the normal one. – Jan Warchoł May 06 '16 at 09:12
16

You also want to put set -o noclobber in your .bashrc. This will raise an error if you try to overwrite an existing file by output redirection.

$ set -o noclobber
$ echo one > afile
$ echo two > afile
bash: afile: cannot overwrite existing file

You can force the redirection to work with special syntax:

$ echo two >| afile
$ cat afile
two

http://www.gnu.org/software/bash/manual/bashref.html#Redirecting-Output

glenn jackman
  • 17,625
  • 2
  • 37
  • 60