07 12 / 2011

Copy files from a text file to directory

My brother brought me an NTFS formatted drive to copy some… files… for him. The only non-server I have is running OS X, so had to mount the portable drive on my headless server. I thought I was in a world of sadness copying files one-by-one but I discovered some magic with xargs.

Essentially, I chuck a newline separated list of files (full path) into a file in /tmp/files_to_copy

/path/to/file_1.ext
/path/to/file_2.ext
/path/to/file\ 3.ext

Then pipe the text file through xargs:

xargs -a /tmp/files_to_copy mv -vt /path/to/dest/

This will then copy the files, one by one to /path/to/dest

Note that the files in /tmp/files_to_copy need to be valid, so escape any special characters / whitespace etc.

Then, just sit and wait for it all to finish.