Results 1 to 4 of 4

Thread: Copying: how to replace file if both have same name but size is not the same?

  1. #1
    Join Date
    Mar 2011
    Location
    New Zealand
    Beans
    444
    Distro
    Ubuntu 12.04 Precise Pangolin

    Lightbulb Copying: how to replace file if both have same name but size is not the same?

    I'm looking for a way to copy files: that will skip the ones with the same name and same size but replaces files that have the same name but a different size.

  2. #2
    Join Date
    Oct 2010
    Location
    London
    Beans
    482
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Copying: how to replace file if both have same name but size is not the same?

    Are you actually just trying to copy files that have been modified, leaving those that haven't changed alone? I'm sure there are backup tools that can do that, but one quick & dirty solution (if you are OK with the command line)

    Code:
    for i in *; do if [ "$i" -nt /path/to/destination/"$i" ]; then cp "$i" /path/to/destination/"$i"; fi; done
    This will copy everything in the directory that has a newer modification time than an existing file in the destination.

    It won't copy over anything that doesn't have a same-named counterpart, but a simple

    cp -n * destination/

    will sort out anything that doesn't have a direct counterpart.

  3. #3
    Join Date
    Mar 2011
    Location
    New Zealand
    Beans
    444
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Copying: how to replace file if both have same name but size is not the same?

    Nice!
    I was thinking I'll have to make a script or something, but I don't know enough shell script.... yet
    Thank you!

  4. #4
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Copying: how to replace file if both have same name but size is not the same?

    Ho jonnyboysmithy.

    It looks like a job for 'rsync'.

    By default rsync will copy and update files based on modification time and size. However, you can specify only to check file size.

    For instance:
    Code:
    rsync -av --size-only  source/  destination/
    I hope that helps. Let us know how it goes.
    Regards.

Bookmarks

Posting Permissions

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