bash 使用 cat 输入的多线程 xargs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9356095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multithreading xargs with input from cat
提问by Amir
I have a text file files.txton my server, on each line is a file with the full path, e.g. /home/lelouch/dir/randomfile.txt.
我的files.txt服务器上有一个文本文件,每一行都是一个带有完整路径的文件,例如/home/lelouch/dir/randomfile.txt.
I want to loop through files.txt, and pass each filename to another script.
我想遍历 files.txt,并将每个文件名传递给另一个脚本。
I have gotten this to work like this:
我已经让它像这样工作:
cat /home/lelouch/dir/files.txt | xargs -0 -n 1 -P 30 /home/lelouch/bin/script.
The problem is, although I want to process it 30 files at a time, it's only happening 1 at a time. I've tried a few other ways, but I haven't gotten it to work like I want.
问题是,虽然我想一次处理 30 个文件,但一次只处理 1 个。我已经尝试了其他一些方法,但我还没有让它像我想要的那样工作。
Any ideas?
有任何想法吗?
回答by Olathe
You say that each lineis a filepath, but you use the -0option of xargs, which switches the separator from a newline to a null character. From the manpage:
您说每一行都是一个文件路径,但是您使用了-0选项xargs,它将分隔符从换行符切换为空字符。从man页面:
Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally)....
输入项由空字符而不是空格终止,引号和反斜杠并不特殊(每个字符都按字面意思)...
Don't use the -0option:
不要使用该-0选项:
cat /home/lelouch/dir/files.txt | xargs -P 30 -n 1 /home/lelouch/bin/script
回答by John Zwinck
I think you want GNU Parallel.
我想你想要GNU Parallel。
回答by Ade YU
--max-argsis the right option you need
--max-args是您需要的正确选择
cat /home/lelouch/dir/files.txt | xargs --max-args=30 /home/lelouch/bin/script

