0

I am trying to compare timestamp of the latest modified file towards current time and if the diff is more than two hours I am trying to print that out. How can this be done?

The following command shows the timestamp of the file and it has to be compared to the current time.

root:/# aws s3 ls --endpoint=https://localhost s3://files/ | tail -n 1 |  awk {'print $1 " "  $2'}
2020-01-22 08:19:00
  • 1
    Doesn't AWS have some sort of find command or equivalent tool? – Kusalananda Jan 22 at 9:08
  • @Kusalananda not as far as I know. – user3270211 Jan 22 at 9:27
  • 2
    Note that you can simplify your awk to awk '{print $1,$2}', it will add a space for you. – terdon Jan 22 at 9:52
  • Thanks @terdon. How would you check the diff of the current timestamp and the timestamp I get out of the file? If the timestamp is greater than two hours it should print out something. I believe an if should do it? – user3270211 Jan 22 at 10:04
  • You can further simplify your tail | awk to awk 'END {print $1,$2}', which will consider only the last line of the input stream. – roaima Jan 22 at 11:46
1

You haven't specified your OS, but assuming you have GNU date, you can convert the date to a timestamp and then perform a simple calculation to determine if it's older than two hours ago:

datetime=$(
    aws s3 ls --endpoint=https://localhost s3://files/ |
    awk 'END {print $1, $2}'
)
timestamp=$(date --date "$datetime" +'%s')
timeAgo=$(date --date "2 hours ago" +'%s')

if [[ $timestamp -lt $timeAgo ]]
then
    echo "It's a really old file"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.