How can I find and count the number of files matching a given string?

I want to find and count all the files on my system that start with some line, for example "foo" , using only one line in bash.

I am new to bash, so I would like to avoid scripts if possible - how can I do this using only simple bash commands and possibly laying out only one line?

So far I have used find / -name foo* . This returns a list of files, but I do not know what to add for the actual file count.

+4
source share
3 answers

you can use

 find / -type f -name 'foo*' | wc -l 
  • Use single quotation marks to prevent the shell from expanding with an asterisk.
  • Use -type f to include only files (not links or directories).
  • wc -l means "number of words, only lines." Since find will display one file per line, this returns the number of files found.
+12
source

find / -name foo* | wc -l find / -name foo* | wc -l should do this. Here is a link to man wc . wc -l counts the number of lines

+3
source

You can connect it to wc

 find / -name foo * | wc -l 
0
source

Source: https://habr.com/ru/post/1397847/


All Articles