James Allen James Allen - 1 year ago 177
Linux Question

Bash: How to tail then copy multiple files (eg using xargs)?

I've been trying various combinations of xargs and piping but I just can't get the right result. Previous questions don't quite cover exactly what I want to do:

  • I have a source directory somewhere, lets say /foo/source, with a mix of different files

  • I want to copy just the csv files found in source to a different destination, say /foo/dest

  • But I ALSO at the same time need to remove 232 header rows (eg using tail)

I've figured out that I need to pipe the results of find into xargs, which can then run commands on each find result. But I'm struggling to tail then copy. If I pipe tail into cp, cp does not seem to receive the file (missing file operand). Here's some examples of what I've tried so far:

find /foo/source -name "*.csv" | xargs -I '{}' sh -c 'tail -n +232 | cp -t /foo/dest'

cp: missing file operand

find /foo/source -name "*.csv" | xargs -I '{}' sh -c 'tail -n +232 {} | cp -t /foo/dest'


cp: failed to access '/foo/dest': No such file or directory ...

find /foo/source -name "*.csv" | xargs -I '{}' sh -c 'tail -n +232 {} > /foo/dest/{}'

sh: /foo/dest/foo/source/0001.csv: No such file or directory ...

Any pointers would be really appreciated!


Answer Source

Your last command is close, but the problem is that {} is replaced with the full pathname, not just the filename. Use the basename command to extract the filename from it.

find /foo/source -name "*.csv" | xargs -I '{}' sh -c 'tail -n +232 {} > /foo/dest/$(basename {})'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download