Tuesday, February 19, 2013

Sort folders by modification times of contents

This is a handy bash script that will take a folder or list of folders, and show you the date of the most recently modified file or folder that exists within it.

I use it to prune users from my server that are no longer active.

#!/bin/bash
# Take a list of folders, search each of 
# them for the most recently modified file
# output the top level folder name and date
while (( "$#" )); do
  FOLDER=$1
  shift

  NEWEST=`find $FOLDER -printf '%T@ %p\n' | 
   sort -k 1nr | sed 's/^[^ ]* //' | 
   head -n 1 `

  DATE=`stat -c %y "$NEWEST" | cut -f 1 -d " "`
  echo "$DATE:  $FOLDER"
done
# ~/scripts/find-inactive.sh `ls /home` | sort
2008-07-28:  lost+found
2012-01-06:  gshaughnessy
2012-01-19:  mwilliams
2012-01-21:  agreenfield
2012-01-22:  zsilva
2012-01-26:  lwomack
2012-01-26:  rmcknight
2012-01-27:  ckyzer
2012-01-29:  crowe
...

No comments:

Post a Comment