Skip to main content
Question

Extension Attribute Scripting


Forum|alt.badge.img+9

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

Forum|alt.badge.img+24
  • Valued Contributor
  • January 19, 2011

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


Forum|alt.badge.img+31
  • Honored Contributor
  • January 19, 2011

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


Forum|alt.badge.img+9
  • Contributor
  • January 19, 2011

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


Forum|alt.badge.img+31
  • Honored Contributor
  • January 19, 2011

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
Forum|alt.badge.img+36

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


Forum|alt.badge.img+31
  • Honored Contributor
  • January 19, 2011

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.


Forum|alt.badge.img+11
  • Valued Contributor
  • August 12, 2020

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


pete_c
Forum|alt.badge.img+16
  • Honored Contributor
  • August 13, 2020

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings