Say I need to find out how many words are in each file that has the word 'work' in its name.

I know that to find files with 'work' in the name, it would be ls work. And to figure out the number of words in a file it would be wc -w.

However I tried the following and it seems to be just displaying the number of files, not the number of words combined in all files (which I need):

ls work | wc -w

So say if there are 14 files that have 'work' in the name, it would display 14, not the number of words.


3 Answers 3


The syntax is wc -w [FILE]. If you don't use FILE but pipe in the output of ls work it will only count what it will read on stdin.

You need to pipe in the text itself:

cat *work* | wc -w

Alternative you could execute wc with find -exec. But be aware that this could show multiple "total" sums as find will call wc multiple times if there are lots of files.

find ./ -type f -name "*work*" -exec wc -w {} +
  • I used the find command to also be able to sum up the count of lines by using wc -l. Amazing!
    – cody.codes
    Nov 1, 2018 at 5:30

You can run wc with multiple files and then use shell built-in * which adds every non hidden files in working directory to wc's parameters.

wc -w *work*
  • If a directory's name contain work then this will show the output along with an error..a hack will be to redirect the STDERR 2>/dev/null..although you should use something like find to get only the files..
    – heemayl
    Apr 18, 2015 at 17:38
  • If the shell globs are not sufficient to locate the desired files (e.g. they're in deeply nested directories), you can also run find and pass the result to wc, like this: wc --words $(find . -type f -name '*work*').
    – waldyrious
    Dec 17, 2021 at 22:10

There are some good answers here but for this I like to use:

ls | grep work | xargs wc -w

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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