Searching for specific folders and getting folder size

Hi Guys

I have used this forum on many occasions, always finding what I wanted, until now that is.

I’m just working on a script that will do some analysis on our server.

What I’m wanting is a script that will search for specific folders and return the size of the total of those folders in Mb

So what I have so far is done in shell find /Volumes/Server/0*/0*/0*/0*/0*/Folder1 -type d

This will find the folders I require, but how do I find out how much disk space is being used by folders named Folder1

The folders I require are always the sixth level. The script will form part of an applescript, I just haven’t put the do shell script part into this post.

The ideal scenario would be to have log file that will detail the folder, the folder size and then the accumulation of the folder sizes in Mb

Your help would be much appreciated.


Well I hope this should get you started… there are a few things you requsted that aren’t here, but you should be able to work those out… If not I would be glad to help.

In my example I wanted to look at the size of all users ‘Desktop’ folders which would always be within /Users/*/Desktop so my code looked like this…

(* Note the need for sudo as I would not have permissions on other users home directories *)
set AdminName to "" -- An Administrator's short name
set pword to "" -- The Administrator's passsword

set x to do shell script "find /Users/*/Desktop -type d -maxdepth 0 -exec du -hd 0 '{}' \\;" user name AdminName password pword with administrator privileges

which in my case returns the following (without the dashes of course :))

21G /Users/jnierodzik/Desktop
36K /Users/localadmin/Desktop

Now to mod that to your example it would look something like this…

set x to do shell script "find /Volumes/Server/0*/0*/0*/0*/0*/Folder1 -type d -maxdepth 0 -exec du -hd 0 '{}' \\;"

Hope that helps.

Thanks James

Ran the script last night just in the terminal and it returned an error Argument List too long
I have reset the command going again only changing the maxdepth 0 to mindepth 5, as I only want to search at that level

Is that the right thing to do?

Also if I wanted to write the results to a log file is it as easy as >logfile at the end of the script?

Thanks for your help

Good morning Paolo,

Changing maxdepth to mindepth shouldn’t change much. maxdepth 0 is only going to look at the specified path level and not dig deeper… it wont dig up either since we are specifying our search path.

Did it error trying it both ways?

For giggles from the terminal what happens if you just do this?

find /Volumes/Server/0*/0*/0*/0*/0*/Folder1 -type d -maxdepth 0

And approximately how many results are you expecting? I recreated your file structure with a few dozen folders and had no problems.

And as for you logging it really depends how detailed you want to be. Do you want to create a new log each time? Or append to the same log each time with some time stamps? let us know what you want exactly and we can help you with the code. :smiley: