1

I have a recurring process that runs to check to see if a file has aged x mins. In a situation where it has, I move the file over to a new directory.

However, I noticed that files are being moved instantly. Any ideas what could be causing the issue?

# Expected age time = 10 minutes
EXPECTED_AGE_TIME=10
# How long the file has actually aged
ACTUAL_AGE_TIME=$((`date +%s` - `stat -L --format %Y $FILE`))

if [[ $ACTUAL_AGE_TIME > $((EXPECTED_AGE_TIME * 60)) ]]; then
   mv $FILE ./loaded/$FILE
fi
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • Could you use `find`? Like this: `find . -mmin +10 -type f`. Then process only these files. How about `find . -mmin +10 -name "myfile.log"`? – stephanmg Nov 12 '19 at 12:21
  • 1
    Thanks, but I only need to run the check against one file at a time. Is this possible with `find`. – methuselah Nov 12 '19 at 12:24
  • 1
    Can you try to use `if (( $ACTUAL_AGE_TIME > $((EXPECTED_AGE_TIME*60)) )); then ... fi `? What are the values of `ACTUAL_AGE_TIME` and `EXPECTED_AGE_TIME` if you `echo` them before the comparison? – stephanmg Nov 12 '19 at 12:28

2 Answers2

3

Building on comment to use find in comments above. Apply find to a single find:

find $FILE -mmin +10 -exec mv '{}' ../loaded/ \;

This will eliminate messy date math, formatting of dates, ...

dash-o
  • 13,723
  • 1
  • 10
  • 37
  • Will this actually search? Because, if the directory contains many files, the result will take a while. – stephanmg Nov 12 '19 at 14:40
  • 1
    There is no recursive directory scan, as the $File is specified as the starting point. Search is only performed when the starting points are directories. – dash-o Nov 12 '19 at 14:46
  • But if I would have specified `-name` $FILE I will invoke scan. Correct me if I'm wrong. – stephanmg Nov 12 '19 at 15:04
  • 1
    Short answer: NO. Long answer: Everytime you refer to a file (even in `cp a b`), there is a 'scan' in the current folder to locate 'a', and 'b'. This is true for `open`, `stat`, and many calls. This access much faster (and optimized) vs. a "directory scan" (`find . -name filename ...`, usually, opendir/readdir), which will usually result in large number of repeated stat. quick "proof": Run 'strace -c' and compare 'find file -mtime -100' vs 'find . -name file -mtime 100' in a folder with large number of files. – dash-o Nov 12 '19 at 18:26
  • 1
    @stephanmg but this is a good topic for a separate question. – dash-o Nov 12 '19 at 18:27
2

Checking relative age of files can be done by Bash's built-in file date comparison operator -ot.

See help test:

FILE1 -nt FILE2 True if file1 is newer than file2 (according to modification date).

FILE1 -ot FILE2 True if file1 is older than file2.

#!/usr/bin/env bash

declare -- TIME_FILE
TIME_FILE="$(mktemp)" || exit 1 # Failed to create temp-file

trap 'rm -- "$TIME_FILE"' EXIT # Purge the temp-file on exit

declare -i EXPECTED_AGE_TIME=10

# Set the time of the referrence $TIME_FILE to $EXPECTED_AGE_TIME minutes
touch --date "$((EXPECTED_AGE_TIME)) min ago" "$TIME_FILE"

# If $FILE is older than $TIME_FILE, then move it
[[ "$FILE" -ot "$TIME_FILE" ]] && mv -- "$FILE" "./loaded/$FILE"
Community
  • 1
  • 1
Léa Gris
  • 17,497
  • 4
  • 32
  • 41
  • What does `declare --` do? – stephanmg Nov 12 '19 at 13:21
  • 1
    @stephanmg it declares a variable without any flag set. ( `--` is the standard option flags terminator, indicating further command parameters are not option flags, and preventing option flags from being injected from variable parameters content) – Léa Gris Nov 12 '19 at 13:25