Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cat/Xargs/command VS for/bash/command

The page 38 of the book Linux 101 Hacks suggests:

cat url-list.txt | xargs wget –c

I usually do:

for i in `cat url-list.txt`
   do
      wget -c $i
   done 

Is there some thing, other than length, where the xargs-technique is superior to the old good for-loop-technique in bash?

Added

The C source code seems to have only one fork. In contrast, how many forks have the bash-combo? Please, elaborate on the issue.

like image 619
Léo Léopold Hertz 준영 Avatar asked Aug 15 '09 19:08

Léo Léopold Hertz 준영


3 Answers

Also consider:

xargs -I'{}' wget -c '{}' < url-list.txt

but wget provides an even better means for the same:

wget -c -i url-list.txt

With respect to the xargs versus loop consideration, i prefer xargs when the meaning and implementation are relatively "simple" and "clear", otherwise, i use loops.

like image 24
nicerobot Avatar answered Oct 31 '22 20:10

nicerobot


From the Rationale section of a UNIX manpage for xargs. (Interestingly this section doesn't appear in the OS X BSD version of xargs, nor in the GNU version.)

The classic application of the xargs utility is in conjunction with the find utility to reduce the number of processes launched by a simplistic use of the find -exec combination. The xargs utility is also used to enforce an upper limit on memory required to launch a process. With this basis in mind, this volume of POSIX.1-2008 selected only the minimal features required.

In your follow-up, you ask how many forks the other version will have. Jim already answered this: one per iteration. How many iterations are there? It's impossible to give an exact number, but easy to answer the general question. How many lines are there in your url-list.txt file?

There are other some other considerations. xargs requires extra care for filenames with spaces or other no-no characters, and -exec has an option (+), that groups processing into batches. So, not everyone prefers xargs, and perhaps it's not best for all situations.

See these links:

  • http://www.sunmanagers.org/pipermail/summaries/2005-March/006255.html
  • http://fahdshariff.blogspot.com/2009/05/find-exec-vs-xargs.html
like image 181
Telemachus Avatar answered Oct 31 '22 20:10

Telemachus


xargs will also allow you to have a huge list, which is not possible with the "for" version because the shell uses command lines limited in length.

like image 4
xilun Avatar answered Oct 31 '22 20:10

xilun