使用 bash 检查包裹是否超过 24 小时



我想检查我的最后一个文件是否早于 24 小时。 我在目录中有很多zip包,所以我必须用这部分代码"过滤"最后一个:ls -1 | sort -n | tail -n1

我在.sh文件中的代码:

#!/bin/bash
file="$HOME/path_directory ls -1 | sort -n | tail -n1"
current=`date +%s`;
last_modified=`stat -c "%Y" $file`;

if [ $(($current-$last_modified)) -gt 86400 ]; then
echo "File is older that 24 hours" | mailx noreply@address -s "Older than 24 hours" me@mailmail.com
else
echo "File is up to date.";
fi;

这是我得到的一个错误:

stat: invalid option -- '1'
Try 'stat --help' for more information.
/path_directory/imported_file.sh: line 9: 1538734802-: syntax error: operand expected (error token is "-")

如果有人做了类似的东西,请一些提示。

我建议你试试这个:

if test "`find file -mtime +1`"

但是,如果您坚持可以通过将其更改为以下内容来修复它:

#!/bin/bash
file="$HOME/path_directory ls -1 | sort -n | tail -n1"
current=$(date +%s);
last_modified=$(stat -c "%Y" $file);
if [ $((current - last_modified)) -gt 86400 ]; then
echo "File is older that 24 hours" | mailx noreply@address -s "Older than 24 hours" me@mailmail.com
else
echo "File is up to date.";
fi;

您可以获取目录中修改早于 1440 分钟(86400 秒(的文件列表,您可以使用find

find -maxdepth 1 -mmin +1440

因此,它将选择目录中的所有文件(无子目录(,更改时间(以分钟为单位(早于 1440。

+1440中的+很重要,否则您将获得恰好1440 分钟未修改的文件。

您还可以使用-mtime指定天数(以天为单位(:

find -maxdepth 1 -mtime +1

如果需要所有文件(在此目录和子目录中(,可以删除-maxdepth 1

如果您只想包含文件等,则可以添加-type f。有关更多标志和(过滤(选项,请阅读find的手册页。

文件变量格式不正确 我相信你想要这样的东西:

file=`find $HOME/path_directory | sort -n | tail -n1`

file=$( find $HOME/path_directory | sort -n | tail -n1)

如果你喜欢莫德姆的方式

最新更新