Extension attribute to report the size of a user's Desktop folder

howie_isaacks
Valued Contributor II

My company uses a cloud file service called Egnyte. We deploy it for our clients as well. One of the functions of Egnyte is that it will sync a user's Desktop and/or Documents folder with the user's private folder in Egnyte. This is very useful if we need to give the user a temporary Mac while theirs is being repaired. All we need to do is sign in their Egnyte account, and all of the files that they're hoarding on the Desktop will appear within the amount of time it takes for the files to sync over from Egnyte. I am just now learning how to create extension attributes, but this particular scenario has so far eluded me. The first thing I do is find out who the current logged in user is:

currentuser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`

echo $currentuser

Next, I tried entering: size=du -h /Users/$currentuser/Desktop

echo $size

That command is meant output the size of the Desktop folder for the current logged in user. If I type:

du -h /Users/$currentuser/Desktop/

It outputs this: 418M /Users/howie/Desktop/

If I type:

echo $size

It outputs this which is useless: 

du -h Desktop

The script will error out stating that "-h" command not found. There are other errors that come up. I think I'm simply entering these commands with the wrong syntax. Maybe I need quotes and/or brackets. Because I can't get the output to come out correctly without errors, I can't even move on to the <result>XXX</result> command and wrap this up. This seemed so simple when I thought of it 🤔

Help please!

1 ACCEPTED SOLUTION

DBrowning
Valued Contributor II

This will do that:

#!/bin/bash
#Who is the current logged in user?
currentuser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
#Size of the user's Desktop folder
size=$(du -h /Users/$currentuser/Desktop)
folderSize=$(echo $size | awk '{print $1}')
echo "<result>$folderSize</result>"

View solution in original post

16 REPLIES 16

SCCM
Contributor III

Maybe try something like whats in here:

Extension Attribute for Document Folder Size - Jamf Nation Community - 65326

but change the folder to the desktop folder if thats what your after

howie_isaacks
Valued Contributor II

Thanks. I think I saw that post yesterday. I didn't try the idea posted, but I love having more ideas to try. My post that I made just when you replied seems to be the solution. I just went through the whole scripting series in the Jamf training catalog and I thought maybe a for loop might provide the solution. It did 😊

howie_isaacks
Valued Contributor II

I may have figured this out. Here's my extension attribute:

 

#!/bin/bash
#Who is the current logged in user?
currentuser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
#Size of the user's Desktop folder
cd /Users/$currentuser/
for size in $(du -h /Users/$currentuser/Desktop)
do
echo $size
echo "<result>$size</result>"
done
 
The above resulted in this:
Screen Shot 2021-07-30 at 11.16.52 AM.png
If I enter the for loop manually and continue the commands, I get a final output that looks like this:
Screen Shot 2021-07-30 at 11.19.45 AM.png
I setup the extension attribute to use data type integer. Since I'm new to this, I figured that the results in my Mac's inventory reflect only the portion of the result that is in integer form. If I change the data type to string, I get the same results after running another inventory.

DBrowning
Valued Contributor II

If you are looking to get the full string "28K /Users/howie/Desktop" as the value in the EA, I would set the Data type to string and use this:

#!/bin/bash
#Who is the current logged in user?
currentuser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
#Size of the user's Desktop folder
size=$(du -h /Users/$currentuser/Desktop)
echo "<result>$size</result>"

howie_isaacks
Valued Contributor II

Actually, what I wanted was just the size of the Desktop folder. I'm trying to figure out how to avoid having the extra results output. Fortunately, what is being displayed in the machine record is only the size of the Desktop folder. I appreciate any help I can get. 

DBrowning
Valued Contributor II

This will do that:

#!/bin/bash
#Who is the current logged in user?
currentuser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
#Size of the user's Desktop folder
size=$(du -h /Users/$currentuser/Desktop)
folderSize=$(echo $size | awk '{print $1}')
echo "<result>$folderSize</result>"

howie_isaacks
Valued Contributor II

Your script is way better than mine! I kept using the "du -h" command and I would get errors. I knew it had to be that I was using it incorrectly or using the wrong syntax. I decided to use a for loop simply because I saw a for loop being used in one of the scripting series videos. They showed how to run a command at the beginning of the for loop, and I thought it might solve my problem. It did, but you have done a better job. Thanks for your help! I have already thought of other ideas on how to use what you have posted here.

sgiesbrecht
Contributor III

I know this is some what related but here is an extension attribute to get the files and folder in the home users directory.  I hope this is helpful

#!/bin/zsh

# Gets currently logged in user
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕 && ! /loginwindow/ { print $3 }' )
the 😕 = : / without the space

# Gets the files
DSKTP=$( find /Users/$loggedInUser/Desktop -type f | sed 's/.DS_Store//g ' | sed 's/.localized//g ' | Sort )
DOCS=$( find /Users/$loggedInUser/Documents -type f | sed 's/.DS_Store//g ' | sed 's/.localized//g ' | Sort )
DWNLD=$( find /Users/$loggedInUser/Downloads -type f | sed 's/.DS_Store//g ' | sed 's/.localized//g ' | Sort )

# Adds the output into Jamf
OUTPUT="$DSKTP $DWNLD $DOCS"
/usr/bin/printf "<result>$OUTPUT</result>"

Thanks! I like this. I have been trying (unsuccessfully) for years to get users to stop storing everything on the desktop. It can and often does cause problems as does hoarding downloads. I recently created a configuration profile that forces on the setting to empty trash after 30 days.

can you share the empty trash profile.  I created one in policies but does not seem to work properly

Here's a screenshot of the Applications & Custom Settings payload in the profile.

Screen Shot 2021-07-30 at 2.26.26 PM.png

Here's the plist text.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FXRemoveOldTra**bleep**ems</key>
<true/>
<key>_FXSortFoldersFirst</key>
<true/>
<key>_FXSortFoldersFirstOnDesktop</key>
<true/>
</dict>
</plist>
After creating the plist, I ran this command to convert it to xml for important into the profile:

plutil -convert xml1 PlistName.plist

For some reason, some text is being replaced after I save the post! 😱 I guess it's because there's a cuss word in the text. The screenshot has the correct text though.

Yes, it looks like "OldTrashItems" gave our filter a run for its money. Thanks for posting the screenshot. Here it is again in case someone misses it above.

Screen Shot 2021-07-30 at 3.41.12 PM.png

howie_isaacks
Valued Contributor II

There's another post on the thread that received an emoji to replace some text. Thanks for working on this.

howie_isaacks
Valued Contributor II

I created a feature request about this filter. It either needs to be more intelligent, or removed altogether. We post scripts in Jamf Nation a lot, and we don't need our text being replaced.

DBrowning
Valued Contributor II

If you click on the ... in the post window, you can then click on </> and it will allow you to input code. 

 

 

it will show up like this:  😕 

 

 

well that is interesting it even did it inside my code window....thats a first!!!

 

howie_isaacks
Valued Contributor II

That's annoying. The new Jamf Nation looks good but this filter is just too obnoxious. This needs to be fixed. Now.