Milos
Milos

Reputation: 1263

Counting number of files in a directory with an OSX terminal command

I'm looking for a specific directory file count that returns a number. I would type it into the terminal and it can give me the specified directory's file count.

I've already tried echo find "'directory' | wc -l" but that didn't work, any ideas?

Upvotes: 56

Views: 55184

Answers (6)

dmpost
dmpost

Reputation: 96

Actually tree command is good alternative for find command.

  1. Install tree

    brew install tree
    
  2. Run command

    tree -R -L 99
    

    or

    tree -R -L 99 -a
    

    where:

  • -R option to run recursively in folders

  • -L specify level of the dir tree

  • -a to show all files, included hidden

  1. Scroll up to check which files have been counted.

PS. Remove autogenerated 00Tree.html in all nested folders

find . -name \00Tree.html -type f -delete

Upvotes: 0

Xavier Artot
Xavier Artot

Reputation: 11

I'm using tree, this is the way : tree ph

Upvotes: -1

Waxhaw
Waxhaw

Reputation: 829

I don't understand why folks are using 'find' because for me it's a lot easier to just pipe in 'ls' like so:

ls *.png | wc -l

to find the number of png images in the current directory.

Upvotes: 8

Pétur Ingi Egilsson
Pétur Ingi Egilsson

Reputation: 4493

The fastest way to obtain the number of files within a directory is by obtaining the value of that directory's kMDItemFSNodeCount metadata attribute.

mdls -name kMDItemFSNodeCount directory_name -raw|xargs

The above command has a major advantage over find . -type f | wc -l in that it returns the count almost instantly, even for directories which contain millions of files.

Please note that the command obtains the number of files, not just regular files.

Upvotes: 4

Max Brandt
Max Brandt

Reputation: 331

Open the terminal and switch to the location of the directory.

Type in:

find . -type f | wc -l

This searches inside the current directory (that's what the . stands for) for all files, and counts them.

Upvotes: 33

Mureinik
Mureinik

Reputation: 311393

You seem to have the right idea. I'd use -type f to find only files:

$ find some_directory -type f | wc -l

If you only want files directly under this directory and not to search recursively through subdirectories, you could add the -maxdepth flag:

$ find some_directory -maxdepth 1 -type f | wc -l

Upvotes: 82

Related Questions