Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that there are other ways to go about this, but I'm looking to be able to make wc interpret stdin as a file name or list of file names. For example,

ls JP*/std* | wc

would work the same as

wc JP*/std*

I am guessing it isn't possible to adjust this behavior, but I'm working on a script where this'd make my life a lot easier. Thanks.

share|improve this question
up vote 3 down vote accepted

Use find in conjunction with xargs. The only reason I am recommending find is to take advantage of the -print0 option, which separates file names by NULs; this helps avoid issues with file names containing spaces.

find .  -maxdepth 1 -type f -print0 | xargs -0 wc
share|improve this answer
The input may be coming from a file, but I should be able to pass the path for each to a find statement, so this should still work. Thanks! – TTT Oct 26 '12 at 15:46
@TTT The important bit is the xargs. You don't need the find, the find was just in place of your ls (with the added feature of -print0). Check the xargs manpage, you'll need to tell it the format of your file. – derobert Oct 26 '12 at 16:26

If you want to take the output of a command and use it as an input file list to wc, you could probably do something like this:

wc $(ls JP*/std*)

This runs ls JP*/std* and its output gets passed as arguments to wc.

xargs might also be useful for you here:

ls JP*/std* | xargs wc

See the xargs manpage for more detail about how it can be used, as it's quite flexible.

share|improve this answer
Not whitespace-safe. – Kevin Oct 26 '12 at 17:38
Don't parse the output of ls. – Gilles Oct 26 '12 at 22:34

After reading your comment

The input may be coming from a file,

If the list of files is already on file you have, you can just skip the find part and use just the xargs part.

See xargs man page.

Use -a option of xargs to pass the filename of the file where is the list that xargs has to read.

If the filenames inside the list is not a full filename and just passing this to wc is not enough because there is no directory you can edit the file, so that the full path is included (you should be able to do that without manually edit it, by using sed or awk or similar).

Or you can use the replace string mechanism in xargs (-I option in xargs) (note that if you use this last method, with -I replacement, each line will invoke a different program and that there are some limitations on the maximum length - see man page for details)

If your input is coming from a file (and hence you can not use the mechanism provided by the -0 option of xargs), you have to pay great attention if there are space or other strange characters in the filename. If there are you should find a way to avoid some unexpected result.

For example if you have a file named "my document.txt" and in your list is written as

my document.txt

and then it is passed like it is written in the file, you would get that what it is executed is

wc my document.txt

and this would result in wc program looking for two files, one named my and one named document.txt

You should be sure that what is passed is something like

wc "my document.txt"


wc my\ document.txt

(The first way may be easier if you are already using the replace mechanism, the second one if you are already editing your file.)

Well, if you can, maybe is just easier to edit the input file so that each item is separated by a null character and use the -0 option of xargs.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.