Saturday, April 20, 2024
18
rated 0 times [  18] [ 0]  / answers: 1 / hits: 41310  / 3 Years ago, tue, august 24, 2021, 12:18:46

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.


More From » command-line

 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 {} +

[#29231] Thursday, August 26, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fenddy

Total Points: 361
Total Questions: 103
Total Answers: 113

Location: Turkmenistan
Member since Sun, Aug 2, 2020
4 Years ago
fenddy questions
Tue, Nov 22, 22, 10:11, 1 Year ago
Tue, Sep 27, 22, 09:16, 2 Years ago
Wed, Dec 28, 22, 13:09, 1 Year ago
Fri, Jun 18, 21, 14:04, 3 Years ago
;