EA: If home folder creation/birth date is past 3 years

New Contributor

Hi all, 

Extension attribute: 

I need to get the creation/birthdate of the currently logged-in user on a machine then print a statement if the folder is older than 3 years old. Since we were on Fleetsmith and had new enrollments this might be the best way to determine how long someone has actually been on a machine. Here is what I have so far but it is not working too well on Big Sur for some reason. 




# get logged in user
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name 😕 && ! /loginwindow/ { print $3 }' )

# get user home folder date info
userHome=`dscl . -read /users/$loggedInUser NFSHomeDirectory | cut -d " " -f 2`

# get user home folder creation date
home_date=$(getfileinfo -d $userHome | awk '{print $1}')

# create experation date 3 years back of current date
exp_date=$(date -v-1095d +%m-%d-%Y)

# loop to create value of laptop upgrade status
if [[ "$home_date" -ge "$exp_date" ]]; then
echo "<result>No upgrade needed</result>"
echo "<result>Needs upgrade</result>"
exit 0


New Contributor III

Hi. The problem is you get two dates as MM/DD/YY and tries to compare them with  -eq which is for comparing numbers. It is better if you get the two dates as timestamps (seconds from a certain date)

homeDate=$(stat -f "%B" "$userHome")

will set homeDate to birthdate (B) of users home (seconds after 1970-01-01)

expdate=$(date -v-1095d +%s)

will set expdate to similar. The day 3 years ago as seconds after 1970-01-01

Now you can compare these

New Contributor

Thanks @johandahl 

That worked! actually had it before but must have missed something. Anyway curious how would I print out to output the MM/DD/YY in the below? 

homedate=$(stat -f "%B" $userHome") 



New Contributor III

To convert a timestamp to a date you can do it like this

date -r "$homedate"

or if you want it formatted as MM/DD/YY

date -r "$homedate" "+%D"

Legendary Contributor III

You can also use the find command to locate these for you. Something like this perhaps, though not thoroughly tested.

/usr/bin/find /Users -maxdepth 1 -mindepth 1 -type d -Btime +1095 | egrep -v "Shared"

This should find all home folders within /Users that have an inode creation date of 1095 days (3 years) or more, and filters out the Shared folder, since I don't think you'd want to know about that directory. You may need to add in additional folders to exclude as well.

Honored Contributor

You can use a metadata one-liner which is fast, performant and just gives you the data in human readable date time stamps

# command

mdls /Users/user-name -name kMDItemFSCreationDate -raw

# example results

mdls /Users/<user> -name kMDItemFSCreationDate -raw

2019-01-16 01:09:17 +0000




Legendary Contributor III

Yes, mdls is quite a handy utility! I sometimes forget about it.

The only thing is, based on what the OP was looking to do here, they may need to then convert that date into a timestamp and do the comparison against 3 years ago to see if a creation date falls into that group or not. I don't believe they were just looking for the date only, but I may be wrong.

Either that or their EA can be set up in a date format and then create a Smart Group for anything older than 1095 days. That would work.

Honored Contributor

there is an epoch time stamp in the user's `dscl` record you can also parse, but you can also convert time stamps into other things and do simple math. You can also diff the date against today's date as well. 


Working on what @tlarkin posted, you can use the following in an EA. It should determine the current user then fun the command to determine the creation date of the home folder. Doing so also strips everything out, except for the date in the format that the EA is expecting.



# Determines the Current User
CURRENTUSER=$(ls -l /dev/console | awk '{print $3}')

# If there is not a user logged in, then abort.
if [[ -z "$CURRENTUSER" ]] || [[  "$CURRENTUSER" == 'root' ]] || [[ "$CURRENTUSER" == "loginwindow" ]] ; 
        exit 0

# Retrieve the creation date of the home directory. 
result=$(mdls /Users/${CURRENTUSER} -name kMDItemFSCreationDate | awk '/kMDItemFSCreationDate =/{print $3}')


After thinking about this, I tweaked the code a little. I added a check to see if the $CURRENTUSER is a real account. If not it should not change the value in the inventory. 

I hope this helps. I like the idea and I might work on this to maybe scan all home directories and retrieve the date of the oldest. 

Honored Contributor

@techjason you can just simply use the -raw argument with Spotlight and not have to pipe to awk. So, it is even easier, so you can even change your code to this

# Retrieve the creation date of the home directory. 
result=$(mdls /Users/${CURRENTUSER} -name kMDItemFSCreationDate -raw)