Link to home
Start Free TrialLog in
Avatar of Silas2
Silas2

asked on

Linux Script File Attributes

Im trying to run a shell script on a cron (Ubuntu 16.01) which iterates all files in a folder, and checks that their last modified time is greater than 5mins. I think last modified time is one of the three file date/times stored by Linux. So far I've got :
for file in $(ls folder/*.wav)
do
    name = $(file%%.wav)
    chmod 777 $name.wav
//I want to do this:
    if($name.wav).LastModified< now-5mins)
    {
        do something
    }

Open in new window

And I just want to check that the last modified date is more than 5mins ago, I'm not sure of the syntax...anyone?
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

I think your best bet is to use the "stat" command to get the last modification time, and "date +%s" to get the current time (both in seconds since the epoch, 1st Jan 1970). Then if current time minus the last modified time is greater than 300 (5 minutes, in seconds), then "do something"
if [ $(expr $(date '+%s') - $(stat -c "%Y" "${name}.wav")) -gt 300 ]; then
  do something
fi

Open in new window


Edit: clarified the text a bit, and put curly braces round the "name" variable - not strictly necessary here, because BASH knows that the name of the variable stops at the dot, but it makes it clearer to the human reader that the variable name is just "name". Note also that the whole "${name}.wav" has double quotes round it - that is in case the name has spaces in it. Also, I could have used "$(( $(date...) - $(stat...) ))" rather than the "expr" version, but I thought I already had enough parentheses in the statement!
Avatar of Silas2
Silas2

ASKER

Thanks for that, I just found on StackExchange this:
if test `find "text.txt" -mmin +120`
(http://stackoverflow.com/questions/552724/how-do-i-check-in-bash-whether-a-file-was-created-more-than-x-time-ago)
That does look a lot simpler, what makes you prefer the stat?
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Silas2

ASKER

Thanks v. much. I'm a Linux newbie.
That's fine, we all had to start! Welcome to the world of Linux :-)