spin6lock
spin6lock

Reputation: 155

which command add the total in command ls|xargs wc -l?

ls just prints out the files in directory, and wc -l counts the line num for specific filename, xargs will send the ls output one by one to wc -l, but the final result comes with total, why?

sample output:

  14 doc.txt
 230 legion.c
 519 legion_agent.c
  70 legion_manager.c
  52 legion_privilege.c
 236 logic_agent.c
 349 protocol.c
1470 total

Upvotes: 3

Views: 3322

Answers (2)

Kent
Kent

Reputation: 195179

try this line, see if it gave you what you want:

grep -c '.*' *.*

Upvotes: 0

konsolebox
konsolebox

Reputation: 75548

wc combines the total lines of all files that were passed to is as arguments. xargs collects lines from input and puts them all at once as one set of multiple arguments to wc so you get the total of all those files.

As an example if I have files a and b, and I run:

wc -l a b

I would get

 28 a
 17 b
 45 total

Similarly if imitate the output of ls with (echo a; echo b;) added with xargs we'd get the same output:

(echo a; echo b;) | xargs wc -l

Output:

 28 a
 17 b
 45 total

If we add echo:

(echo a; echo b;) | xargs echo wc -l

Output:

wc -l a b

Upvotes: 8

Related Questions