Finding a count of files in a directory

In Linux, you can count all of the files in a directory, of a given type.

To do this run:

>find . -maxdepth 1 -type f -name “*.gz” | wc -l

If you want to count all of the gzipped files in all directories below your current directory. Run the same command without the -maxdepth:

>find . -type f -name “*.gz” | wc -l

Everything relies on the wc command, which is short for word count. We are counting the line by line output from the find command.

If you want to count the number of directories, try this:

>find . -type d | wc -l

Just remember to subtract 1 from the output. Linux will include the current directory (.) as a directory.