-1

I am using a command:

xargs -a file_list.txt cp -t /path/to/dest

but the filenames with whitespace get cut and therefore are not copied.

what can be done?

Rami
  • 1
  • 2

3 Answers3

3

Xargs is nice/fast/etc but isn't easy to understand for novices in shell scripting.

I think that's will be much more readable (and more clean than for with subshell).

#!/bin/bash

while read filename; do
    cp -a "$filename" /path/to/dest
done < file_list.txt
2

You can do this

IFS=$'\n' ; for file in `cat file_list.txt` ; do cp $file /path/ ; done

Explanation

  1. Set internal field separator to newline
  2. Read file_list.txt and loop through each line assigning contents of line to variable called $file
  3. On each loop, execute cp command on $file (you can add more steps too)

If it were a shell script and nicely formatted

#!/bin/bash

IFS=$'\n'

for file in `cat file_list.txt` ; do {
  cp $file /path/
} done
Ryan Babchishin
  • 6,260
  • 2
  • 17
  • 37
1
cat file_list.txt | tr '\n' '\0' | xargs -r0i  cp -t /path/to/dest/ "{}"
HBruijn
  • 77,029
  • 24
  • 135
  • 201
Anubioz
  • 3,677
  • 18
  • 23