Determining Oldest file and file age
Today I had to write a short shell script to help figure out the oldest file in a particular folder, how old that file was (in secs or minutes). This is a pretty common requirement when dealing with file(s) folders such as working directories, where you periodically need to sweep or clean up files. It also offers a metric to see if certain files are lingering too long and becoming stale..
Of course there are many other approaches to this problem, and if you simply want the file(s) to be erased after a specific period of time, UNIX has a special folder for this.. But for cases where you simply need to know what file was the OLDEST or NEWEST (simply modify) the script below.
The script works as follows:
$ age.sh [filename]
If no filename is provided it automatically seeks the oldest file in the current folder, if a filename is given it simply returns that file(s) age.
#!/bin/bash # get OLDEST (or NEWEST file age) from the current folder # spurce http://www.abrandao.com/category/software/code-software/ # if no parameters are specified find the OLdest file in current folder if [ $# -eq 0 ] then OLDEST=$(find . -maxdepth 1 -type f | cut -c3- |xargs ls -tr | head -1) #echo "OLDEST file: $OLDEST" else OLDEST="$1" #echo "File Age for: [$OLDEST]" fi #now lets do the date match and get back in seconds (since epoch) time time_now=`date +%s` old_filetime=`stat -c %Y $OLDEST` #new_filetime=`stat -c %Y $NEWEST` time_oldest=$(( (time_now - old_filetime) )) UNIT="secs" #echo "$OLDEST file is $time_oldest $UNIT old" #convert time to make it more readable in sec/min/hour if [ $time_oldest -gt 60 ] then time_oldest=$((time_oldest/60)) UNIT="min." if [ $time_oldest -gt 60 ] then time_oldest=$((time_oldest/60)) UNIT="hrs" fi fi # Display oldest filename followed by age in sec/min/hour echo "$OLDEST is $time_oldest $UNIT" #time_newest=$(( (time_now - new_filetime) )) #echo "$OLDEST file is $time_oldest $UNIT old" # echo "$NEWEST file is $time_newest secs. old"
For a more sophisticated version that offers command line options and searches both OLDEST and NEWEST file in a specific folder take a peek below.
#!/bin/bash # usage: age+.sh [old|new] [/path_to_folder/] # # defaults to oldest file in current folder usage() { cat << EOF usage: age+.sh [o|n] [/path_to_folder/] Displays news or oldest age of file in a given folde OPTIONS: -o displays oldest file -n newest file in folder -x exit EXAMPLES: age+.sh o /path_to_folder/ displays oldest file in folder age+.sh n /path_to_folder/ displays newest file in folder EOF } getage() { # let's make sure that the folder exists if [ -z "$1" ]; then echo "Error: you must specify a directory name" usage exit 1 fi time_now=`date +%s` filetime=`stat -c %Y $1` time_diff=$(( ($time_now - $filetime) )) UNIT="secs" if [ $time_diff -gt 60 ] then time_diff=$(($time_diff/60)) UNIT="min." if [ $time_diff -gt 60 ] then time_diff=$(($time_diff/60)) UNIT="hrs" fi fi #echo "$2 $time_oldest $UNIT" echo "$1 file [$2] is $time_diff $UNIT ago." exit 1; } while getopts ":o :n" opt; do case "$opt" in o) dirname="$2" echo "Finding in location $dirname" if [ -z "$dirname" ] then echo "[Current folder]" OLDEST=$(find . -maxdepth 1 -type f \( ! -iname ".*" \) | cut -c3- |xargs ls -tr | head -1) else OLDEST=$(find $dirname -maxdepth 1 -type f \( ! -iname ".*" \) | cut -c1- | xargs ls -tr | head -1) fi getage $OLDEST "oldest" ;; n) dirname="$2" echo "Finding in location [$dirname]" if [ -z "$dirname" ] then echo "[Current folder]" NEWEST=$(find . -maxdepth 1 -type f \( ! -iname ".*" \) | cut -c3- |xargs ls -t | head -1) else NEWEST=$(find $dirname -maxdepth 1 -type f \( ! -iname ".*" \) | cut -c1- | xargs ls -t | head -1) fi getage $NEWEST "newest" ;; x) exit echo "Exiting..." ;; \?) echo "$OPTARG is an unknown option" usage exit 1 ;; *) usage ;; esac done
Dear sir,
I am struggling with the task of finding the newest file in a particular folder in linux. I am a shell novice as well.
I think your script is very helpful (even it doesn’t run correctly if it call to get the newest file in the another directory other than the current directory. However, I think I can modify it a bit and make it fit my purpose. I would like to ask if I can reuse the code you listed in this site.
Have a nice day.
Best Regards,
Truong
Yes, of course, re-use the code and if you want to commit your changes to my github repo that’s fine too, let me know.