Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file is over 30 days old WITHOUT the find command

Tags:

bash

shell

unix

I've written a bash shell script and part of it is checks if a file is over 30 days old using the find command, sadly when I uploaded it to my host it did not work as the find command is locked down.

Does anybody know how to check if a file is over 30 days old WITHOUT the find command?

I'm thinking I need to do an "ls -a $filename" then parse the date string out, convert it to a unix date and then compare it with todays date but I'm very rusty at unix.

Thanks

like image 342
The Frog Daddy Avatar asked Jan 24 '26 06:01

The Frog Daddy


2 Answers

stat -c %Z filename gives the last change time in unixtime

stat -c %Y filename if you want the last modification time.

like image 196
Kimvais Avatar answered Jan 27 '26 00:01

Kimvais


You can use if [ $file -ot $timestamp_file ] (-ot meaning "older than"). You would have to construct the appropriate file, for example with touch (which takes timestamp options). If this is a periodic task you can also create the timestamp file on each run to use next time.

like image 45
Ben Jackson Avatar answered Jan 27 '26 00:01

Ben Jackson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!