Hi All,
I am looking for a terminal command to get the exact same output as the file count you recieve when using Get Info in finder.
The closest i can get is using the find command with flags:
find 'path/to/folder' -not -path '*/\.*' -and -not -path '*\.key/*' -and -not -path '*\.numbers/*' -and -not -path '*\.pages/*' -and -not -path '*__MACOSX/*' -and -not -path '*\.pdf/*' -and -not -path '*\.app/*' -and -not -path '*\.rtfd/*' | wc -l
I will be searching on an external volume that sometimes produces keynote save files that finder sometimes sees as a package and sometimes sees as a folder. If a folder finder counts the items contained if a package it doesn't, I need the command or script to mimic this behaviour.
In the example of the screenshot get info on the top folder produces a count of 14 and the find command produces a count of 23.
There are also other behaviours that differ the file count between them but i'm not sure what causes them.
Any help on a solution it being a command or script would be much apreciated.
Thanks, James
I don’t think you’ll be able to replicate the Finder’s algorithm with 100% fidelity. Finder shows the user a view of the system that’s is far removed from the on-disk reality. In some cases those differences are obvious — for example, Finder unifies /Applications
and /System/Applications
, it treats packages as a single item, and don’t get me started on the Trash (-: — but there are lots of more subtle variations.
You can use AppleScript to interact with Finder’s view of the file system. For example:
tell application "Finder" set finderCount to (count of items of folder "Applications" of startup disk) end tell set actualCount to (do shell script "ls /Applications | wc -l") as number {finderCount, actualCount}
On my Mac this returns {126, 84}
because finderCount
includes the apps in /System/Applications
.
Getting this to work for a big folder hierarchy is likely to be tricky. One nice feature is the entire contents
property:
tell application "Finder" entire contents of folder "Test" of home end tell
In theory you should be able to combine this with count
but it doesn’t work )-: count of entire contents of folder "Test" of home
always seems to return 0 )-: You could get the full list and then do the count in AppleScript:
tell application "Finder" set allTheStuff to entire contents of folder "Test" of home end tell count of allTheStuff
but that’ll be expensive for a large directory hierarchy because the Apple event response will be huge.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"