Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Say I need to find out how many words are in each file that have the word 'work' in it.

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

However I tried this 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 follow the 'work' prereq, it would display 14, not the number of words.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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 ./ -name "*work*" -exec wc -w {} +
share|improve this answer

Your Answer

 
discard

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.