LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using stat to determine last file access and comparing to current date in IF statemnt (https://www.linuxquestions.org/questions/linux-newbie-8/using-stat-to-determine-last-file-access-and-comparing-to-current-date-in-if-statemnt-784045/)

danpaluska 01-22-2010 10:42 AM

using stat to determine last file access and comparing to current date in IF statemnt
 
this was some code a friend wrote that worked on my mac os x machine
Code:

THIS_RUN=`date +%s`

if [ `stat -f "%Sm" -t "%s" "$f"` -lt $(($THIS_RUN - 120)) ]

...blah blah do some stuff ...

now i'm on a fedora 11 machine and i want to duplicate. but i can't seem to get stat to do anything useful for me. when i type "man stat" i get all the options in the form %X, %a, etc. but whenever i try from the command line, it seems to think those are supposed to be files not options. what am i missing?

Code:

[museotwvee@museotwvee Webcam]$ stat -t %X last_run.txt
stat: cannot stat `%X': No such file or directory
last_run.txt 11 8 81b4 500 500 fd00 132769 1 0 0 1264177619 1264177618 1264177618 4096
[museotwvee@museotwvee Webcam]$ echo $SHELL
/bin/bash
[museotwvee@museotwvee Webcam]$ stat -t "%X" last_run.txt
stat: cannot stat `%X': No such file or directory
last_run.txt 11 8 81b4 500 500 fd00 132769 1 0 0 1264177925 1264177924 1264177924 4096
You have new mail in /var/spool/mail/museotwvee
[museotwvee@museotwvee Webcam]$ stat -t '%X' last_run.txt
stat: cannot stat `%X': No such file or directory
last_run.txt 11 8 81b4 500 500 fd00 132769 1 0 0 1264177925 1264177924 1264177924 4096
[museotwvee@museotwvee Webcam]$ stat `%X` last_run.txt
bash: fg: %X: no such job
  File: `last_run.txt'
  Size: 11                Blocks: 8          IO Block: 4096  regular file
Device: fd00h/64768d        Inode: 132769      Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  500/museotwvee)  Gid: (  500/museotwvee)
Access: 2010-01-22 12:32:05.462012869 -0400
Modify: 2010-01-22 12:32:04.447017975 -0400
Change: 2010-01-22 12:32:04.447017975 -0400
[museotwvee@museotwvee Webcam]$ stat -t `%X` last_run.txt
bash: fg: %X: no such job
last_run.txt 11 8 81b4 500 500 fd00 132769 1 0 0 1264177925 1264177924 1264177924 4096


danpaluska 01-22-2010 11:01 AM

oh, i got it. -c was what i was looking for...

THIS_RUN=`date +%s`
FILE_TIME=`stat -c '%X' $f`
if [ $FILE_TIME -lt $(($THIS_RUN - 120)) ]; then
echo "$FILE_TIME is less than $THIS_RUN - 120"
else
echo "$FILE_TIME is not less than $THIS_RUN -120"

fi

thanks and sorry for bandwidth waste but maybe someone else will find it useful ;)

danpaluska 02-16-2010 11:52 AM

arghh...

so this doesn't work now that i'm on a ubuntu machine. or at least it doesn't work on this new machine that happens to be running ubuntu 9.10

[CODE]
...
FILE_TIME=`stat -c ‘%X’ $f`
if [ $FILE_TIME -lt $(($THIS_RUN - 120)) ]; then
echo “Uploading $f”
...

[\CODE]
and when i run it...
[CODE]
axiomtwvee@axiomtwvee:~/Videos/Webcam$ ./videochecker.sh

./videochecker.sh: line 93: [: ‘1266337825’: integer expression expected

[\CODE]

so something about my syntax is slightly off. i've tried a lot of permutation here but can't seem to find it.
i tried various parentheses modification in the variable definition and the IF statement. feels like i'm grasping for straws.

there is a decent amount of variability in the references i find online for these

another thing that used to work that i had to change was this line [CODE]
original that worked fine before
SEARCH_DIR="/home/axiomtwvee/Videos/Webcam"

changed to
SEARCH_DIR=/home/axiomtwvee/Videos/Webcam
[\CODE]

something about my shell environment is different?
[CODE]
axiomtwvee@axiomtwvee:~/Videos/Webcam$ echo $SHELL
/bin/bash
[\CODE]
but it seems like the usual...

any tips?
thanks.


All times are GMT -5. The time now is 06:08 PM.