Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

counting

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

I'm new to bash so I'd like to avoid scripting if possible - how can I do this using only simple bash commands and maybe piping in just one line?

So far I've been using find / -name foo*. This returns the list of files, but I don't know what to add to actually count the files.

like image 366
Katherine Rix Avatar asked Feb 22 '12 15:02

Katherine Rix


People also ask

How do I count files using grep?

If you want to count only files and NOT include symbolic links (just an example of what else you could do), you could use ls -l | grep -v ^l | wc -l (that's an "L" not a "1" this time, we want a "long" listing here). grep checks for any line beginning with "l" (indicating a link), and discards that line (-v).

How do I find out how many files are in a file?

The total number of items (both files and folders) stored inside is displayed in the lower-left corner of File Explorer's user interface. If you want to count only some of the files or folders stored inside your folder, select all of them and look at the bottom left side of the File Explorer interface.

Is there a way to count the number of files in a folder?

View Properties Just locate the folder, or the subfolder, for which you need to count the subfolders or files, right-click on it, and then click on Properties from the context menu. You can also press ALT+Enter to open the Properties of a folder or a subfolder.


1 Answers

You can use

find / -type f -name 'foo*' | wc -l
  • Use the single-quotes to prevent the shell from expanding the asterisk.
  • Use -type f to include only files (not links or directories).
  • wc -l means "word count, lines only." Since find will list one file per line, this returns the number of files it found.
like image 61
Adam Liss Avatar answered Oct 15 '22 22:10

Adam Liss