Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2001
    Posts
    72

    cp -r without overwrite prompt

    I need to:

    cp -r dirname/* dirname2/

    and have it overwrite existing files and directories without asking me if it should each time. Can anyone tell me how I do that?

    Thank you,

    Chris

  2. #2
    Join Date
    Oct 2002
    Location
    /roof/ledge
    Posts
    28,090
    Make that -rf

  3. #3
    Join Date
    Oct 2001
    Posts
    72
    That's easy! Thank you!

  4. #4
    Join Date
    Oct 2001
    Posts
    72
    oooppss, spoke to soon. I'm still getting:

    cp: overwrite 'path/to/file'?

    and there are hundreds of them.......

  5. #5
    Join Date
    Aug 2000
    Posts
    1,167
    Open your .bashrc file with pico and look for the line that reads:

    alias cp='cp -i'

    and comment it out:
    #alias cp='cp -i'

    Log out, then log in again and you shouldn't get the prompt. This is a kind of safety net, so it's probably a good idea to put the command back as it was when you're done.

  6. #6
    Join Date
    Oct 2001
    Posts
    72
    That's the ticket Thank you both,

    Chris

  7. #7
    Join Date
    Oct 2002
    Location
    /roof/ledge
    Posts
    28,090
    Hmm, learned something here. Elements in the .bashrc file can't be overridden locally. Is there no way to do this for a single domain/user, so the entire server isn't affected?
    Thanks alchiba.

  8. #8
    Join Date
    Aug 2000
    Posts
    1,167
    /etc/skel/.bashrc is the shell template. Edit that to your liking and all subsequent new accounts will inherit those shell settings.

  9. #9
    Join Date
    Aug 2000
    Posts
    1,167
    There's a .bashrc in the home directory for each user. If you edit that one it only affects that user.

  10. #10
    You can just use '/bin/cp -r' to do this without doing anything to the alias commands.

  11. #11
    Join Date
    May 2012
    Location
    Linux World
    Posts
    1,137
    @ above, thats right. calling by full path is the widely used method.
    Kevin Cheri : Senior Server Administrator / Freelancer : 13+ years Exp, reach me out for any help
    Server Optimization Expert / Mysql Guru / Migration Specialist
    Skype : lynxmaestro
    Gmail : cheri.kevin@gmail.com

  12. #12
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    1,180
    As others have confirmed already, simply using /bin/cp instead of "cp" will ignore the alias and therefore allow forcing to overwrite files. In scripts it is always recommended to use the full paths to avoid issues. And by the way instead of copying files with the cp command, I recommend to use rsync with "-av" parameters locally, because it will only copy/replace files that are actually different while keeping permissions and ownership (so syncing instead of copying), instead of copying everything over. This solution will be faster and safer imo.
    Inbound Marketing & real SEO for web hosting providers
    ✎ Get in touch with me: co<at>infinitnet.de

  13. #13
    Join Date
    Apr 2008
    Location
    Oregon
    Posts
    295
    You can also pipe. IE:
    yes | cp -r asdf/* xyz/
    -Jacob
    iWebFusion [AS397373] - 6 US datacenters offering shared, reseller, dedicated, VPS and Colo
    California (x2) | Oregon | Iowa | North Carolina | Nevada

  14. #14
    cp -af usually works too
    Tara Roberts
    www.whmxtra.com

  15. #15
    Join Date
    Mar 2014
    Location
    Prague
    Posts
    132
    Note that without -i you can possibly harm your system. Be careful what you type. I remember that once I ran rm -rf /* instead of rm -rf ./* :/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •