Bash 脚本IFF中的执行操作指定文件已在7天内修改
我试图做的是将名为myFile的文件从目录a复制到目录B。在此之后,我对刚刚复制到目录B的文件执行一些操作。这很好。但是,如果目录A中的文件在过去7天内被修改,我希望脚本运行所有操作。否则它就什么也做不了。所以基本上我想要:Bash 脚本IFF中的执行操作指定文件已在7天内修改,bash,conditional-statements,Bash,Conditional Statements,我试图做的是将名为myFile的文件从目录a复制到目录B。在此之后,我对刚刚复制到目录B的文件执行一些操作。这很好。但是,如果目录A中的文件在过去7天内被修改,我希望脚本运行所有操作。否则它就什么也做不了。所以基本上我想要: #!/bin/sh if ((modification date of myFile in dir A) >= (current date minus 7 days)) DO STUFF else DO NOTHING end 因此,要执行
#!/bin/sh
if ((modification date of myFile in dir A) >= (current date minus 7 days))
DO STUFF
else
DO NOTHING
end
因此,要执行的操作已经启动并正在运行。我只需要上述伪代码中描述的条件结构。有人知道如何为bash脚本构造它吗?您可以通过编写以下代码来进行测试:
filepath="your/file/path"
if [[ $(find ${filepath} -mtime -7 | wc -l) ]]; then
# modified within past 7 days
else
# not modified within the last 7 days
fi
从人工查找:
-mtime n
File's data was last modified n*24 hours ago. See the comments
for -atime to understand how rounding affects the interpretation
of file modification times.
Numeric arguments can be specified as
+n for greater than n,
-n for less than n,
n for exactly n.
您可以通过以下方式进行测试:
filepath="your/file/path"
if [[ $(find ${filepath} -mtime -7 | wc -l) ]]; then
# modified within past 7 days
else
# not modified within the last 7 days
fi
从人工查找:
-mtime n
File's data was last modified n*24 hours ago. See the comments
for -atime to understand how rounding affects the interpretation
of file modification times.
Numeric arguments can be specified as
+n for greater than n,
-n for less than n,
n for exactly n.
看看find
实用程序,它提供了对ctime
等的搜索操作。find
如果只需要单个文件的修改时间,那就太麻烦了。改为使用stat
查看find
实用程序,它提供了对ctime
等的搜索操作。find
如果只需要单个文件的修改时间,那就太麻烦了。使用stat
代替。谢谢鲁本斯,这正是我需要的。工作起来很有魅力!谢谢鲁本斯,这正是我需要的。工作起来很有魅力!