移动已老化x分钟的文件 [英] Move file that has aged x minutes

查看:49
本文介绍了移动已老化x分钟的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个重复执行的过程,可以检查文件是否已老化x分钟.在有文件的情况下,我将文件移到新目录.

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

推荐答案

可以通过Bash的内置文件日期比较运算符 -ot 来检查文件的相对年龄.

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

请参阅帮助测试:

FILE1 -nt FILE2 如果file1比file2更新(根据修改日期),则为true.

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

FILE1 -ot FILE2 如果file1早于file2,则为true.

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"

这篇关于移动已老化x分钟的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆