I have a text file that has a list of files with full paths, with each line representing each file. For example, the first line from the file is:

/mnt/data/data4/1998/reports/First Quarter.docx

All the files share the same path up to the "data4" part (i.e. /mnt/data/data4). All I'm trying to do is to move each of these files to a different drive. So, for example, the above file would be moved to

/mnt/data/data11/1998/reports/First Quarter.docx

The "data11" part of the path would be constant and replace "data4" in the existing paths for all files. There are spaces in the text file and file names. In some cases, the part of the target path following /data11 will already exist, but in other cases it will not.

I imagine this can be done in a one liner using sed, xargsand mv but can't quite seem to figure it out. Any suggestions would be most appreciated.

  • Are there any single quotes in the filenames? – icarus Aug 20 '19 at 18:50
  • Yes, quite a few. – user415851 Aug 20 '19 at 18:57
  • You want those single quotes to be removed? You didn't mention this in the sample input file content – Inian Aug 20 '19 at 19:04
  • Sorry about that. No, I was hoping to preserve the filenames as is. – user415851 Aug 20 '19 at 19:44

Using rsync:

sed 's,^/mnt/data/data4/,,' filelist.txt > "$synclist"
rsync -avh --files-from="$synclist" --remove-source-files /mnt/data/data4/ /mnt/data/data11/
rm "$synclist"
  1. Create a temporary file and assign it to variable synclist.
  2. Remove prefix /mnt/data/data4/ from all lines in filelist.txt and save the output in the temporary file.
  3. Sync the files (which are now relative paths) in $synclist from /mnt/data/data4/ to /mnt/data/data11/. Option --remove-source-files removes the source files after transfer (similar to mv).
  4. Delete temporary file.
  • Thanks! I will give it a try. – user415851 Aug 20 '19 at 21:55
  • Worked like a charm. Thanks again. – user415851 Aug 20 '19 at 22:59

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.