I have a .txt file (called list.txt) containing a list of filenames, including their paths. Such as:


I want these copied to a folder on my desktop called "new-folder". I use this script in Terminal to do it:

cp `cat /Users/Me/Desktop/list.txt` /Users/Me/Desktop/new-folder/

And this works spendidly!
But now I want to expand this functionality.
First of all, it doesn't work if the paths contain spaces, which is not very practical.
Second, I'm not always sure of the paths. I would like Terminal to search in a certain folder and its subfolders for the file called "0001.pdf".

My knewledge of Terminal is pretty much limited to this one script, just so you guys know.
I hope someone can help me, and many thanks in advance!

  • 1
    That does not look like Ubuntu. – A.B. Nov 12 '15 at 10:12
  • You're right. I had found several leads towards my original script on this site, and I thought this was the right place for me to ask my question. I was wrong, please excuse me. – Hans Gravekamp Nov 16 '15 at 8:10

You can use xargs to quote each line.

xargs -a list.txt -I% cp "%" destination/

Your second requirement isn't very practical but assuming list is just a list of filenames, something like this could work:

xargs -a list.txt -L1 -I% find -name "%" -exec cp {} destination/ \; -quit

It'll pass successive lines to find which will find the first file (in the current directory, recursive) with that name and copy it.

  • 1
    Alternatively one can use --arg-file flag instead of redirection – Sergiy Kolodyazhnyy Nov 12 '15 at 10:45
  • This didn't work for me. Possibly because the xargs command is unfamiliar to me. I'm gonna read up more on it, use this as a lead to get further. Thank you so much for your input, – Hans Gravekamp Nov 16 '15 at 8:10
  • It was brought to my attention above that I have inadvertedly posted my question on the wrong site, as it has nothing to do with ubuntu. I appreciate the help, but I understand if this topic will be closed soon to keep this site clean. Thank you so much though! – Hans Gravekamp Nov 16 '15 at 8:12

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.