how to stop cp: overwrite './xxx' ? prompt

34,844

Solution 1

In addition to calling /bin/cp, you could do one of:

\cp -f ...
command cp -f ...

However, I agree that you should not get accustomed to using an alias like cp -i or rm -i -- if you sit down at a different shell, you won't have the safety net you've become dependent on.

Solution 2

After seeing this solution. I could see that bashes alias feature was causing the problems. http://systembash.com/content/prompt-to-confirm-copy-even-with-cp-f/

which cp  
alias cp='cp -i'
/bin/cp
which cp | grep cp
alias cp='cp -i'
/bin/cp

He recommends

unalias cp

I still want to keep the alias I just don't want it to apply to this instance. My solution is to use the binary with a full path, so that bashes alias function does not take over. That works quite well.

/bin/cp -f /media/somedir/somefiles* .  

Solution 3

Unfortunately on Linux the copy "cp" command doesn’t have an option to automatically answer this question with a "y" or "n" answer. There’s more than one solution to this problem depending on what you want to do. One solution is to use the Unix "yes" command. This command will output a string repeatedly until killed.

If we want to overwrite all of the files in the destination directory you can use the "yes" command to answer all questions with a "y". "y" is a default value so it doesn't have to be specified.

yes | cp source/*.txt destination/.

If we want to avoid overwriting any of the files in the destination directory you can use the "yes" command to answer all questions with a no "n".

yes n | cp source/*.txt destination/.

Use "man yes" for more information about the "yes" command.

Share:
34,844

Related videos on Youtube

nelaaro
Author by

nelaaro

Linux admin, tech enthusiast. opensource evangelist.

Updated on September 18, 2022

Comments

  • nelaaro
    nelaaro over 1 year

    How can I stop the cp command from prompting to overwrite. I want to overwrite all the files with out having to keep going back to the terminal. As these are large files and take some time to complete.

    I tried using the -f option. It still ask if I want to overwrite.

       -f, --force
              if an existing destination file cannot be opened, remove it and
              try again (redundant if the -n option is used)
    
    cp -f /media/somedir/somefiles* .  
    cp: overwrite `./somefilesxxx'? y
    
  • AnthonyB
    AnthonyB over 5 years
    A whereis cp will show where is the command. So you'll be able to call the real command instead of the alias.