I'm well aware of how to simply copy a list of files to another location using bash, but the problem is that my list of files contains filenames that contain just about EVERY character that needs to be escaped in bash. There are tons of spaces, !, ",", "'", ?, and just about every other character you could think of that needs to be escaped.

I initially tired going through the text file and escaping all of these characters but it's... exhausting, even with search and replace (however that DOES work) (I tested it with a much smaller list of files.) I also tried adding single quotes in the text file before and after the path, but that doesn't work. I also tried adding single quotes before an after the % after cp, but obviously I have no idea what I'm doing. :) (I do know I'd have to escape single quotes in the text file if I were to somehow surround the file path with single quotes.)

I'm using this command to copy the list of files into another location, but again, it doesn't work with special characters.

cat file.txt | xargs -J % cp % ./folderToMoveto/

I know that there are other commands that do the same thing without having to pipe anything into xargs, and some answers here say that xargs can do it itself, but I haven't gotten those to work, while the above one does work.

Also, I'm on OS X.


GNU xargs has a -0 option to allow it to read null-byte delimited data. On Linux, you can do:

tr '\n' '\0' < file.txt | xargs -0 cp -t /folder/to/move/to

Sadly, Apple has taken their legacy Darwin man pages site off-line, so I can't check if there's a MacOS equivalent.

  • You can find on-line documentation for MacOS commands at https://ss64.com/osx/. From the home page https://ss64.com/ you can find documentation for a number of scripting environments. – AFH Jun 6 '18 at 21:49
  • I mean, honestly the spaces (null byte) aren't really the problem. The problem is the rest of the 20+ characters that need to be escaped. I can fix the space problem with a simple search and replace. I'm looking for a way to add single quotes to the xargs command so I don't have to bother escaping anything but a single quote. (Or am I being stupid here and not understanding your response?) – user2634928 Jun 6 '18 at 21:54
  • I don't understand why you need to do any escaping with this recipe. Can you cut'n'paste your attempts into the question? – glenn jackman Jun 6 '18 at 22:01
  • @user2634928 the -0 in xargs -0 make sure that no characters need special treatment, as only \0 will be taken at end of filename, all the rest will be used as is. xargs does not use the shell and will call cp direcly passing it the filename as is with any characters that would have needed special treatment in the shell. – Patrick Mevzek Jun 6 '18 at 23:13
  • @glennjackman What would you like me to copy and paste? When I try my original code with a plain text file with a full path on every line, CP will stumble over the spaces initially, then when I escape the spaces, CP will stumble over all of the other bad characters (!, ?, , , ', ", etc.) As for your solution, os X cp does not have the -t option, or anything similar. – user2634928 Jun 7 '18 at 0:43

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.