LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 01-22-2010, 11:42 AM   #1
danpaluska
LQ Newbie
 
Registered: Dec 2009
Location: new york city and sometimes boston and hopefully sometime somewhere warmer
Distribution: i'm trying to remain agnostic. but fedora and ubuntu so far.
Posts: 20

Rep: Reputation: 0
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
 
Old 01-22-2010, 12:01 PM   #2
danpaluska
LQ Newbie
 
Registered: Dec 2009
Location: new york city and sometimes boston and hopefully sometime somewhere warmer
Distribution: i'm trying to remain agnostic. but fedora and ubuntu so far.
Posts: 20

Original Poster
Rep: Reputation: 0
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
 
Old 02-16-2010, 12:52 PM   #3
danpaluska
LQ Newbie
 
Registered: Dec 2009
Location: new york city and sometimes boston and hopefully sometime somewhere warmer
Distribution: i'm trying to remain agnostic. but fedora and ubuntu so far.
Posts: 20

Original Poster
Rep: Reputation: 0
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.
 
  


Reply

Tags
bash, script, shell, stat


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
current date - yesterday's date newbie_adm Linux - Newbie 4 09-04-2006 04:56 PM
How do I determine my current kernel? Brnzwngs Debian 5 08-17-2005 03:48 PM
Determine date when linux was installed twantrd Linux - General 4 05-16-2005 11:00 PM
the format of the date in stat in wrong rhuser Programming 9 03-16-2003 01:46 PM
Appending current date and time to a file frankietomatoes Linux - General 5 11-18-2002 03:09 PM


All times are GMT -5. The time now is 05:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration