Thursday, December 20, 2012

Find files in a particular directory which are older than 30 min

My solution to the following problem:
How can I find files in a particular directory which are older than 30 min? I am not able to use several functions like -mmin, -stat, date -r . And I cant create a dummy file with touch command also,as it is to be done in a production environment.
Posted in: http://unix-simple.blogspot.com/2008/01/awkgawk-shell-script-question_31.html


#!/bin/bash

# Current time in seconds since the EPOCH
CURRENT_TIME=$(date +%s)

# 30 minutes in seconds
OFFSET=$((30 * 60))

# The record delimiter
IFS=$'\012'

# The Linux ls
for LINE in $(ls -l --time-style=+%s)
do
        FILE_TIME=$(echo ${LINE} | sed 's/[ ]\+/ /g' | cut -d " " -f 6)
        FILE_NAME=$(echo ${LINE} | sed 's/[ ]\+/ /g' | cut -d " " -f 7)
        TIME_DIFF=$((CURRENT_TIME - FILE_TIME))

        # Avoiding that annoying total count in ls -l
        if [ -n "${FILE_NAME}" ]
        then
                if [ ${TIME_DIFF} -gt ${OFFSET} ]
                then
                        echo "${FILE_NAME} ${FILE_TIME} ${CURRENT_TIME} ${TIME_DIFF}"
                fi
        fi
done

##END##

Chanukah Menorah

A Chanukah Menorah close to my office.