Skip to main content
Question

Extension Attribute for number of files on desktop

  • April 18, 2017
  • 8 replies
  • 40 views

Forum|alt.badge.img+7

Has anyone managed to pull this off? I'd like a number of files listed on the desktop so I can smart group those offenders with tons of junk on their desktop, or even in their downloads folder.

I thought

ls | wc -l ~/Desktop

might work but it doesn't let me designate the Desktop or other folders

8 replies

Forum|alt.badge.img+7
  • Author
  • Contributor
  • April 18, 2017

Thought I had it with

user=`ls -l /dev/console | cut -d " " -f 4`
ls -F /Users/$user/Desktop | wc -l

but that's not pulling anything, hmmph!


Forum|alt.badge.img+10
  • New Contributor
  • April 18, 2017

Close... add on as you see fit

user=`ls -l /dev/console | cut -d " " -f 4`
find /Users/$user/Desktop -print | wc -l | awk '{ print $1 }'

Also remember this will be recursive through any folder within the target. e.g. ~/Downloads/whatever/folder/has/files


Forum|alt.badge.img+12
  • Valued Contributor
  • April 18, 2017

@rgranholm i'd be interested in a way to do this, i was trying to do a similar thing to tell if a folder was empty or not but i couldn't get ls or find to ignore the hidden files properly.

Find may do what you need with something like this, drop the -maxdepth 1 if you want to count in folders

#!/bin/sh

loggedInUser=$( stat -f%Su /dev/console )

count=$( find /Users/$loggedInUser/Desktop -maxdepth 1  | wc -l )

echo "<result>$count</result>"

Forum|alt.badge.img+12
  • Valued Contributor
  • April 18, 2017

@millersc

Your way works well,
do you know how to exclude all hidden files in the count ?

@rgranholm sorry to hijack your thread !


Forum|alt.badge.img+10
  • New Contributor
  • April 18, 2017

@May It's counting the "." files in my directories. So you should test on what your users are doing for hiding. If they are putting the "." or use chflags command.

#!/bin/sh
#
# JSS EA for finding file count in one specific directory "Desktop"
# Change "Desktop" as needed for directory you want
#
loggedInUser=$( ls -l /dev/console | cut -d " " -f 4 )

count=$( find /Users/$loggedInUser/Desktop -print -maxdepth 1 | wc -l | awk '{ print $1 }' )

echo "<result>$count</result>"

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • April 18, 2017
#!/bin/sh

loggedInUser=$(stat -f%Su /dev/console)

count=$(find /Users/${loggedInUser}/Desktop -maxdepth 1 ( ! -iname ".*" ) | sed '1d' | awk 'END{print NR}')

echo "<result>$count</result>"

EDIT: Just wanted to mention that the only issue with any of the above solutions is, if the recon runs while a Mac is sitting at the login window, it won't return any result since it will see the owner of console as "root" and the find will try to search a directory that doesn't exist.


Forum|alt.badge.img+12
  • Valued Contributor
  • April 18, 2017

Thanks @millersc @mm2270

this works great!

i had a hidden Icon? file in my test directory which was being counted so i added that to the -iname,
i normally add a check to make sure the user is logged in and not root or our local admins

loggedInUser=$(stat -f%Su /dev/console)


if [[ $loggedInUser = "root" ]] || [[ $loggedInUser = "localadmin1" ]] || [[ $loggedInUser = "localadmin2" ]]; then
echo "No user logged in - exiting script"
exit 0
fi

count=$(find /Users/$loggedInUser/Creative Cloud Files/ -maxdepth 1 ( ! -iname ".*" ) ( ! -iname "Icon?" ) | sed '1d' | awk 'END{print NR}')

echo "<result>$count</result>"

Forum|alt.badge.img+16
  • Honored Contributor
  • April 18, 2017

The ones above are great, I just thought I'd show an example of doing this in Python.

#!/usr/bin/python

import os
from SystemConfiguration import SCDynamicStoreCopyConsoleUser

lastloggedinuser = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]
lastloggedinuser = [lastloggedinuser,""][lastloggedinuser in [u"loginwindow", None, u""]]

if lastloggedinuser:
    user_desktop =  os.path.join('/Users/', lastloggedinuser, 'Desktop')
    files = [f for f in os.listdir(user_desktop) if os.path.isfile(os.path.join(user_desktop, f))]
    print "<result>{}</result>".format(len(files))
else:
    print"<result>User not logged in.</result>"