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, sad开发者_JAVA百科ly 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
stat -c %Z filename
gives the last change time in unixtime
stat -c %Y filename
if you want the last modification time.
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.
Let ls
output a unix timestamp, like this:
ls -l --time-style='+%s'
Then get the timestamp from the output, and simlpy calculate whether there are 30 × 24 × 60 × 60 seconds between the current time and the timestamp from ls
.
You can get the current timestamp using
date '+%s';
That doesn't work in Linux: no -v option in date
t=`date +%Y%m%d%H%M`
let u=$t-1000000
touch -t "$u" $DATEFILE
if [ $SERVERFILE -ot $DATEFILE ]; then
rm $SERVERFILE
fi
OK this is how I did it in the end...
- I touched a file with a date of today - 1 month.
touch -t "$(date -v -1m +%Y%m%d%H%M)" ${DATEFILE}
- Did an older than test on the 2 files.
if [ ${SERVERFILE} -ot ${DATEFILE} ]
Thanks everybody.
精彩评论