How can I recursively copy a directory into another and replace only the files that have not changed?

前端 未结 3 664
迷失自我
迷失自我 2021-02-02 12:00

I am looking to do a specific copy in Fedora.

I have two folders:

  • \'webroot\': holding ALL web files/images etc

  • \'export\': folder cont

相关标签:
3条回答
  • 2021-02-02 12:32

    It might, but any time the corresponding files in export and webroot have the same content but different modification times, you'd wind up performing an unnecessary copy operation. You'd probably get slightly smarter behavior from rsync:

    rsync -pr ./export /path/to/webroot
    

    Besides, rsync can copy files from one host to another over an SSH connection, if you ever have a need to do that. Plus, it has a zillion options you can specify to tweak its behavior - look in the man page for details.

    EDIT: with respect to your clarification about what you mean by preserving permissions: you'd probably want to leave off the -p option.

    0 讨论(0)
  • 2021-02-02 12:35

    Sounds like a job for cpio (and hence, probably, GNU tar can do it too):

    cd export
    find . -print | cpio -pvdm /path/to/webroot
    

    If you need owners preserved, you have to do it as root, of course. The -p option is 'pass mode', meaning copy between locations; -v is verbose (but not interactive; there's a difference); -d means create directories as necessary; -m means preserve modification time. By default, without the -u option, cpio won't overwrite files in the target area that are newer than the one from the source area.

    0 讨论(0)
  • 2021-02-02 12:43
    1. -u overwrites existing files folder if the destination is older than source
    2. -p perserves the permission and dates
    3. -f turns off verbosity
    4. -r makes the copy recursive

    So looks like you got all the correct args to cp

    0 讨论(0)
提交回复
热议问题