Assuming that your list of files is in the file filelist
cat filelist | tail -n 2 | tr '\r\n' ' ' | xargs -d " " -L1 -I{} cp {} "tdir/"
Note that if your code is generating a list you can pipe that straight in instead of writing it to filelist
first.
Tail will only pass on the last 2 lines of the file which contains the files you want to copy. Changing the -n 2
to -n 1
or -n 3
will change the number of files that will be copied.
Tr will convert any newlines to spaces, this allows xargs to split the new list nicely.
Xargs will call cp with each space separated value piped into it, placing them into tdir
You can add the --verbose
switch to xargs to see the calls it is making to cp.
Check out man pages for tail, tr and xargs for more details.