xianbei xianbei -4 years ago 98
MySQL Question

How to check if file is newer than 15 min in for loop

I'm looking to process files that have been modified within 15 minutes, ignoring the rest. I want to do this in a for loop, like:

for f in *; do
if test 'find $f -mtime -0h15m'
mysql -e "load data local infile '"$f"'
into table "$MYSQL_TABLE"
fields terminated by '\t'
lines terminated by '\n'
ignore 1 lines" -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DB --local-infile

When I run the above, all files are imported, even those older than 15 minutes.

Answer Source

You have two problems.

First, the argument to -mtime is a number of days, not minutes, you need to use --mmin to specify a the difference in minutes.

Second, you're using the wrong type of quotes. To substitute the result of a string into a command, you have to use backticks or $(). Since quotes just return a literal string, they don't execute the command.

You should also quote it to prevent problems if the filename has spaces or other special characters.

So it should be:

if test "$(find "$f" -mmin -15)"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download