Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a file is older than 30 minutes from /bin/sh?

Tags:

linux

unix

sh

How do I write a script to determine if a file is older than 30 minutes in /bin/sh?

Unfortunately does not the stat command exist in the system. It is an old Unix system, http://en.wikipedia.org/wiki/Interactive_Unix

Perl is unfortunately not installed on the system and the customer does not want to install it, and nothing else either.

like image 347
magol Avatar asked Jan 05 '10 09:01

magol


People also ask

How can you tell the age of a file in Linux?

We can search for files based on their modified, accessed, or changed time using the find tool with the -mmin, -amin, and -cmin options, respectively, for these timestamps.

How do you check if a file has been modified?

You can use the stat command on a file to check access and modification times or set up RCS to track changes. You can use MD5 or sum to get the current state of the file, copy that value to a file and then that file to verify that the original file wasn't changed.

How can I see the Mtime of a file?

Modified timestamp (mtime) indicates the last time the contents of a file were modified. For example, if new contents were added, deleted, or replaced in a file, the modified timestamp is changed. To view the modified timestamp, we can simple use the ls command with -l option.


2 Answers

Here's one way using find.

if test "`find file -mmin +30`" 

The find command must be quoted in case the file in question contains spaces or special characters.

like image 174
Schwern Avatar answered Oct 07 '22 20:10

Schwern


The following gives you the file age in seconds:

echo $(( `date +%s` - `stat -L --format %Y $filename` )) 

which means this should give a true/false value (1/0) for files older than 30 minutes:

echo $(( (`date +%s` - `stat -L --format %Y $filename`) > (30*60) )) 

30*60 -- 60 seconds in a minute, don't precalculate, let the CPU do the work for you!

like image 20
slebetman Avatar answered Oct 07 '22 19:10

slebetman