Wed Aug 12 19:23:54 PDT 2015

Copying a Set of Files from One Machine to Another

How do you set about saving your many and varied jpg and mp3 files from an old machine?

There are a variety of possible strategies. Assuming we neglect those that presume that you have your files in a carefully organized directory structure, or a nice secure backup, here is a pragmatic approach.

First - make sure that you are using Linux, Mac OS X, or Cygwin on Windows. Then use find to collect a list of the files and their complete files names that you wish to save. For example, if you wanted to save all files with the suffix 'mp3', you would use:

   find . -name "*.[mM][pP]3" -print > mp3files.txt

That will give you a list of mp3 flies in the text file 'mp3files.txt'. Then go over to the machine that you wish to copy the files to, and use a command like this to collect the files on the other machine:

   ssh User@1.2.3.4 "cd /cygdrive/c; tar -T mp3files.txt -cvf -" | tar xvof -

(With suitable replacesments for the username, the address of the remote machine, and the directory to change directory to on that machine). This command executes an ssh command to the machine that has the files, goes to the root directory, then tar's the files to standard out, making use of the mp3files.txt list. Meanwhile back on the receiving machine, tar reads standard in and extracts the files. Hence you create a faithful copy of the files and directory structure on the receiving machine.

Why didn't I use rsync, you might inquire. Well - I tried and I found that rsync, on this particular version of Cygwin had a habit of hanging. Meanwhile tar and ssh do the job just fine. This method has advantages too. For example, if you want, you can remove files that you do not want to copy from mp3files.txt prior to doing the copy. So you have a high degree of control over what gets copied and what gets left behind.


Posted by ZFS | Permanent link | File under: bash