4

I'm using rsync to copy data between my internal and external hard drives. It's done what I need, but I'm running out of room on my internal hard drive. So, is there any way to get rsync to function like mv instead of cp, but also leave the "skeleton" of empty directories in the original location, only erasing the files, so that I can put more files into the directories and rsync it again to add the new files to the external drive, without erasing the old ones?

Or, if there's a program other than rsync with Linux support that can accomplish this, that's fine too.

6

There is a special option in rsync:

   --remove-source-files
          This tells rsync to remove  from  the  sending  side  the  files
          (meaning  non-directories)  that  are a part of the transfer and
          have been successfully duplicated on the receiving side.

          Note that you should only use this option on source  files  that
          are quiescent.  If you are using this to move files that show up
          in a particular directory over to another host, make  sure  that
          the  finished  files  get renamed into the source directory, not
          directly written into it, so that rsync can’t possibly  transfer
          a  file that is not yet fully written.  If you can’t first write
          the files into a different directory, you should  use  a  naming
          idiom  that lets rsync avoid transferring files that are not yet
          finished (e.g. name the  file  "foo.new"  when  it  is  written,
          rename  it  to  "foo"  when  it is done, and then use the option
          --exclude='*.new' for the rsync transfer).

          Starting with 3.1.0, rsync will  skip  the  sender-side  removal
          (and  output an error) if the file’s size or modify time has not
          stayed unchanged.

File deletion at destionation depends on your options. But, afair, it will delete nothing while you do not use any --delete, --delete-before, etc options.

  • In case you, for any reason, also want to get rid of the empty directories, you can run the command: find . -depth -type d -empty -delete – Kristian Jun 17 '16 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.