Bash:如何尾巴然后复制多个文件(例如使用xargs)? [英] Bash: How to tail then copy multiple files (eg using xargs)?
问题描述
我一直在尝试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:
- 我有一个源代码目录,可以说/ foo / source,混合使用不同的文件
- 我想将源代码中找到的csv文件复制到不同的目的地,比如/ foo / dest
- 我也同时需要删除232标题行(例如使用尾巴)
我已经知道我需要管道找到xargs的结果,然后可以在每个查找结果上运行命令。但是我正在努力去追求复制。如果我管尾巴到CP,CP似乎并没有收到文件(缺少文件操作数)。以下是我迄今为止所尝试的一些示例:
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:缺少文件操作数
cp: missing file operand
find /foo/source -name "*.csv" | xargs -I '{}' sh -c 'tail -n +232 {} | cp -t /foo/dest'
结果:
Result:
cp:无法访问'/ 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/{}'
/ foo / dest /foo/source/0001.csv:没有这样的文件或目录...
sh: /foo/dest/foo/source/0001.csv: No such file or directory ...
任何指针都会非常感谢!
Any pointers would be really appreciated!
谢谢
Thanks
推荐答案
您的最后一条命令是关闭的, code> {} 被替换为完整的路径名,而不仅仅是文件名。使用 basename
命令从中提取文件名。
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 {})'
这篇关于Bash:如何尾巴然后复制多个文件(例如使用xargs)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!