Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Count all files in a folder hierarchy UNIX
In the Finder, it's relatively easy to see how many files are within one level of a folder, but I'm not aware of a way to see a count for all of the files in that folder hierarchy (anyone?).

Someone asked a similar question on the Macworld forums, and with some help from a friend, we managed to come up with a UNIX command string that will return the answer. Say you wish to count all the files in the folder "Stuff" stored on your desktop. Simply type the following into the Terminal:
  % cd ~/Desktop
% find "Stuff/" | wc -l
This will return a count all files, including invisibles and the top level "Stuff" folder itself. If you just want to see the visible files, use:
  % find "Stuff/" \! -name ".*" | wc -l
I'm not sure if this is the most efficient means of answering the question, but it seems to work reasonably quickly and it was accurate on all the folders I tested it on. Any other alternatives, in either the Finder or the Terminal?

If you'd like to know more about how the command works, or how to create a shortcut to make it easier to use in the future, read the rest of the article.

Here's how these commands work to return the number of files. Keep in mind I'm relatively inexperienced with UNIX, so please, help me correct any errors in my explanation!

There are two commands being used here, combined to get the result we're looking for. First, find returns a list of all files in the specified folder that match our criteria. In the first example, there are no criteria, so a list of all files is returned. In the more complex second example, the criteria is \! -name ".*". The \! means "not" (the \ is needed so the shell will not try to interpret the !). The -name ".*" means "name starts with a dot". Taken together, the critera is "not name starts with dot", so basically, return a list of all visible files.

This result is passed (via the pipe (|) symbol) to wc, or word count. With the -l option, wc simply returns the number of lines in what it received. Since it received a listing of filenames, it returns the number of lines in that listing.

To make the command easier to use in the future, simply make an alias out of it (see this hint for an explanation of aliases in OS X; read the comments for the proper directory structure). The alias version of the command is slightly different, as you need to double-escape the "!" that is being used for "not":
alias fc 'find \!:1 \\! -name ".*" | wc -l'
You can change fc to whatever you like, of course. Store this in aliases.mine in ~/Library/init/tcsh, and then (after opening a new Terminal) you can just type "fc name_of_folder" (assuming you are in the parent directory which contains "name_of_folder").

I'm open to suggestions to make this easier or faster ... anyone want to wrap a quick Cocoa shell around this to make a drag-and-drop GUI app for counting files??
  • Currently 2.60 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)

Count all files in a folder hierarchy | 11 comments | Create New Account
Click here to return to the 'Count all files in a folder hierarchy' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Authored by: baba on Apr 10, '02 02:08:42PM

The alias works fine, of course, but I'd just as soon do 'open . ' to pop open the gui folder and read it from there.

[ Reply to This | # ]
Total all files?
Authored by: robg on Apr 10, '02 02:27:29PM

Unless I'm missing something, that will only give you the total of the files in the top level folder that you opened -- at least, that's what I get when I do "open ." in the terminal and look at the total file count in the Finder window. It doesn't include subdirectories...


[ Reply to This | # ]
File Count using Terminal
Authored by: davidbwolff on Apr 10, '02 02:20:13PM

You can also use the terminal and the recursive flag from ls and then piping that output to wc.

ls -R MyDir | wc -l

[ Reply to This | # ]
File Count using Terminal
Authored by: ashevin on Apr 10, '02 03:16:19PM
Hi, "ls -R | wc -l" will not work because one, the contents of each directory are printed multiple entries per line, and two, ls includes information and blank lines that leads to an incorrect total. The closest I can get with a simple ls variant is "ls -1R | grep -v ^$ | grep -v :$ | wc -l", but this only works if none of the files or subdirectories end with a colon. - Avi

[ Reply to This | # ]
Re: File Count using Terminal
Authored by: sjk on Apr 11, '02 04:56:27PM
Sorry, trivial examples disprove that. The default style for ls is one-entry-per-line when output isn't sent to the terminal and you can force that using the -1 (minus one) option; read the man page. Also look at -A, noting the difference between it and -a.

A fast way to count files is by utilizing the locate command. Simple example to count one user's files:

locate /Users/me/ | wc -l

See my comment in Finding files with 'locate' about locate.db permission issues which affect results of this.

[ Reply to This | # ]
Try du
Authored by: Leibowitzn on Apr 10, '02 06:01:03PM

The command "du" is really handy.
Say you want the size of the current folder
in KB, then type "du -ks".

Another good command is "df"
Which shows you home much free disk space
you have left.

[ Reply to This | # ]
GUI option - XRay does this too
Authored by: metafeather on Apr 10, '02 07:18:18PM
For a GUI solution, XRay 1.0 [here] will give you all this information, including whether the files are visible or invisible - it is under the Folder Information tab.

[ Reply to This | # ]
GUXRay = :P ; FileXaminer = :)
Authored by: ZooP on Apr 11, '02 09:05:16AM
FileXaminer is another utility that does this.

[ Reply to This | # ]
The GUI method
Authored by: EvanE on Apr 10, '02 11:19:12PM
This works in all Mac OSes that have List View. View the folder in List View. Notice the count in the status bar ("N items, N.n GB available"). Hit Command-A to select all items in the window, then hit Option(alt)-[right arrow]*. All disclosure triangles will open recursively, and once the Finder figures out how many items it's displaying, the total count (including folders) is displayed.

This doesn't give you a count of hidden files, but as far as the Finder's concerned There Are No Hidden Files.

*just [right arrow] would, of course, have only opened the first level of folders, but you've already figured that out.

[ Reply to This | # ]
Whoops! App bundles...
Authored by: robg on Apr 10, '02 11:43:17PM

Hmm, I just realized that my solution will actually break apart and count all the files and folders within any OS X application bundles that exist in the directory tree. That's probably not ideal ;-), so this would probably qualify as something to be used on non-application folders only.

The GUI method listed elsewhere in the comments doesn't have that problem...


[ Reply to This | # ]
Count all files in a folder hierarchy
Authored by: johnsawyercjs on Nov 09, '04 04:29:30PM

The shareware utility "Super Get Info" also offers a nice, easy GUI method to accomplish this, without the bugs and hassles. It allows you to specify a keyboard command that you can use after selecting a folder in the Finder, to display a Get Info window that shows the total number of files and folders inside that folder, as well as other info the Finder's Get Info doesn't show. This utility also lets you select more than one item in the Finder, and do a Get Info on it, to bring up several Get Info windows, one for each item you select, as pre- OS X Finders did, instead of the OS X Finder's method of adding up the Get Info figures for all the items you select and displaying them lumped together in one window. Super Get Info's default keyboard command is Command-Option-I, but it lets you change this to the Finder's Command-I if you wish.

[ Reply to This | # ]