Reputation: 1263
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
Reputation: 96
Actually tree command is good alternative for find command.
Install tree
brew install tree
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
PS. Remove autogenerated 00Tree.html in all nested folders
find . -name \00Tree.html -type f -delete
Upvotes: 0
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
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
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
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