How to make wc interpret standard in as a file list

7,154

Solution 1

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

Solution 2

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:
7,154

Related videos on Youtube

TTT
Author by

TTT

Updated on July 19, 2022

Comments

  • TTT
    TTT 6 days

    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.

  • TTT
    TTT over 9 years
    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!
  • derobert
    derobert over 9 years
    @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.
  • Kevin
    Kevin over 9 years
    Not whitespace-safe.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 9 years