How can I tell if a file is older than 30 minutes

2020-05-24 19:08发布

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.

标签: linux unix sh
11条回答
兄弟一词,经得起流年.
2楼-- · 2020-05-24 19:48

Here's my variation on find:

if [ `find cache/nodes.csv -mmin +10 | egrep '.*'` ]

Find always returns status code 0 unless it fails; however, egrep returns 1 is no match is found`. So this combination passes if that file is older than 10 minutes.

Try it:

touch /tmp/foo; sleep 61; 
find /tmp/foo -mmin +1  | egrep '.*'; echo $?
find /tmp/foo -mmin +10 | egrep '.*'; echo $?

Should print 0 and then 1 after the file's path.

My function using this:

##  Usage: if isFileOlderThanMinutes "$NODES_FILE_RAW" $NODES_INFO_EXPIRY; then ...
function isFileOlderThanMinutes {
  if [ "" == "$1" ] ; then serr "isFileOlderThanMinutes() usage: isFileOlderThanMinutes <file> <minutes>"; exit; fi
  if [ "" == "$2" ] ; then serr "isFileOlderThanMinutes() usage: isFileOlderThanMinutes <file> <minutes>"; exit; fi
  ##  Does not exist -> "older"
  if [ ! -f "$1" ] ; then return 0; fi
  ##  The file older than $2 is found...
  find "$1" -mmin +$2 | egrep '.*'  > /dev/null 2>&1;
  if [ $? == 0 ] ; then return 0; fi  ## So it is older.
  return 1;  ## Else it not older.
}
查看更多
迷人小祖宗
3楼-- · 2020-05-24 19:52

For those like myself, who don't like back ticks, based on answer by @slebetman:

echo $(( $(date +%s) - $(stat -L --format %Y $filename) > (30*60) ))
查看更多
聊天终结者
4楼-- · 2020-05-24 19:53

You can use the find command.
For example, to search for files in current dir that are older than 30 min:

find . -type f -mmin +30 

You can read up about the find command HERE

查看更多
倾城 Initia
5楼-- · 2020-05-24 19:54

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!

查看更多
时光不老,我们不散
6楼-- · 2020-05-24 19:54

What do you mean by older than 30 minutes: modified more than 30 minutes ago, or created more than 30 minutes ago? Hopefully it's the former, as the answers so far are correct for that interpretation. In the latter case, you have problems since unix file systems do not track the creation time of a file. (The ctime file attribute records when the inode contents last changed, ie, something like chmod or chown happened).

If you really need to know if file was created more than 30 minutes ago, you'll either have to scan the relevant part of the file system repeatedly with something like find or use something platform-dependent like linux's inotify.

查看更多
\"骚年 ilove
7楼-- · 2020-05-24 20:01

If you're writing a sh script, the most useful way is to use test with the already mentioned stat trick:

if [ `stat --format=%Y $file` -le $(( `date +%s` - 1800 )) ]; then 
    do stuff with your 30-minutes-old $file
fi

Note that [ is a symbolic link (or otherwise equivalent) to test; see man test, but keep in mind that test and [ are also bash builtins and thus can have slightly different behavior. (Also note the [[ bash compound command).

查看更多
登录 后发表回答