Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading xargs with input from cat

Tags:

bash

xargs

I have a text file files.txt on my server, on each line is a file with the full path, e.g. /home/lelouch/dir/randomfile.txt.

I want to loop through files.txt, and pass each filename to another script.

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.

Any ideas?

like image 593
Amir Avatar asked Dec 28 '22 05:12

Amir


1 Answers

You say that each line is a filepath, but you use the -0 option of xargs, which switches the separator from a newline to a null character. From the man page:

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 -0 option:

cat /home/lelouch/dir/files.txt | xargs -P 30 -n 1 /home/lelouch/bin/script
like image 162
Olathe Avatar answered Jan 11 '23 10:01

Olathe