Extension Attribute Scripting

jafuller
Contributor

I would like to get the size of a specific directory that exists on some of our imaged computers. I'm still very much a noob when it comes to bash, so I'm looking for some help from my fellow Casper gurus.

I know I can use the "du" command to return a human readable size. However, what if the directory doesn't exist on that specific asset in inventory. How do I check for the directory first, then return the size if it exists?

I think this should return the size of the directory:
du -hs "/Volumes/Macintosh HD/Users/Shared/Active Jobs" | awk '{print $1}'
returns: 69G

But I want it to return 0 if the directory doesn't exist. Thoughts?
--
James Fuller | Starbucks Coffee Company | Technology Application Services | application developer II

8 REPLIES 8

jarednichols
Honored Contributor

if [ -d /Users/Shared/Active Jobs ];
then Echo "<result>Du -hs /Users/Active Jobs | awk '{print $1}'</result>
Else Echo "<result>Not found</result>" fi

The -d flag in the if statement checks for the presence of a directory. If
that directory exists, execute this block of code. Otherwise, skip over
and just echo a result that it wasn't found.
j
-- Jared F. Nichols
Desktop Engineer, Client Services
Information Services Department
MIT Lincoln Laboratory
244 Wood Street
Lexington, Massachusetts 02420
781.981.5436

tlarkin
Honored Contributor

built in function if bash

if [[ -e /path/to /folder ]]

This would be if it exists

if [[ -!e /path/to/folder ]]

This would be, if does not exist

jafuller
Contributor

Thanks Guys for the quick replies!
--
James Fuller | Starbucks Coffee Company | Technology Application Services
| application developer II | Coffee Master
E: jafuller at starbucks.com | V: 206.318.7153 | F: 206.318.0155

tlarkin
Honored Contributor

The only difference betwee -e and -d is that -e will check if it exists regardless of what it is and -d expects it to be a directory. So, if -d points to a file and not a directory it will fail. Otherwise either method works

talkingmoose
Moderator
Moderator

Adding to this a bit:
On 1/19/11 11:49 AM, "Thomas Larkin" <tlarki at kckps.org> wrote:

-d tests that an item exists and is a directory
-e tests that an item exists and is a file or directory
-f tests that an item exists and is a file

--

William Smith
Technical Analyst
Merrill Communications LLC
(651) 632-1492

tlarkin
Honored Contributor

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

pretty much sums up all your options, but do note that conditional statements can only process one thing at a time. So, if you need to test a set of paths you would need to create an array of paths or a loop that would output all your paths you wanted to check.

swapple
Contributor III

how would we convert that to a number where it joins a smart group based on file size?

pete_c
Contributor III

Make sure the Extension Attribute (EA) is defined as returning an Integer. Add a tail to only get the last line, in case that directory ever somehow ends up with any subdirectories, and sed 's/.$//' to remove the last character (sorry, my awk-fu is not great when I haven't had much coffee):

du -H /path/to/your/dir | awk 'END { print $1 } ' | sed 's/.$//'

(someone who is slightly awake, please consolidate that awk for me)

Then, create a Smart Group > Show Advanced and choose that EA, and from there you can use More Than as an operator. (If More Than/Less Than are not available, go back and edit the EA's Data Type.)