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??

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...

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

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:

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.

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...

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.