Copy over existing files without confirmation?

196,650

Solution 1

You do realise that RHEL and CentOS have tried to protect novice users by setting up aliases for the root user to prevent accidentally overwriting and deleting files?

alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'

The -i switch is what requires confirmation when modifying or removing existing files. Because alias expansion happens before execution of the command even the use of the --force -f switch will still require confirmation.

You can remove the alias permanently by editing the /root/.bashrc file, remove the alias for the duration of a session with unalias cp or for a single command use one of:

  • use the full path /bin/cp
  • use quotes "cp" or 'cp' around the command
  • use the command keyword e.g. command cp
  • escape the command \cp

Solution 2

You can do yes | cp -rf myxx, Or if you do it as root - your .bashrc or .profile has an alias of cp to cp -i, most modern systems do that to root profiles.

You can temporarily bypass an alias and use the non-aliased version of a command by prefixing it with \, e.g. \cp whatever

Share:
196,650

Related videos on Youtube

Itai Ganot
Author by

Itai Ganot

Updated on September 18, 2022

Comments

  • Itai Ganot
    Itai Ganot almost 2 years

    I need to copy and over-write a large amount of files, I've used the following command:

    # cp -Rf * ../
    

    But then whenever a file with the same name exists on the destination folder I get this question:

    cp: overwrite `../ibdata1'? 
    

    The Problem is that I have about 200 files which are going to be over-written and I don't think that pressing Y then Enter 200 times is the right way to do it.

    So, what is the right way to that?

  • Jpark822
    Jpark822 almost 10 years
    +1 for pointing at how to bypass an alias. Tempted to -1 for the brute force solution.
  • Andrew Newby
    Andrew Newby over 7 years
    +! for the yes pipe function - very handy!
  • tushar
    tushar about 4 years
    what does -rpf do? I have seen it used multiple times in scripts.