简体   繁体   English

如何检查我的文件是否在10天内用Shell脚本创建

[英]how to check my file is created within 10 days in shell script

I have a bunch of log files which are named according to their creation dates. 我有一堆根据其创建日期命名的日志文件。 For example; 例如; if my log file is created on 12 March 2018, the name of the logfile is log-2018-03-12.log 如果我的日志文件创建于2018年3月12日,则日志文件的名称为log-2018-03-12.log

Here is what I want to do: From today's date, I want to check the name of my log files and zip the log files which are created in last 10 days. 这是我想做的事情:从今天起,我要检查日志文件的名称并压缩最近10天创建的日志文件。

Here is my code that zip all log files in a specific directory: 这是将所有日志文件压缩到特定目录中的代码:

#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/opt/MyStore/action_scripts/deneme/

tendaysbefore= date -d "$now - 10 days" '+%F'

for file in $scripthome;
do
    find "$(basename "$file")" | zip -R $backupfile.zip "log-2*.log"
done

But I want to zip last 10 days log file, not all log files, and also I want to continue doing it for every 10 days after this. 但是我想压缩最近10天的日志文件,而不是所有日志文件,而且我想在此之后每10天继续压缩一次。 Also, after having zip file, I want to delete old log files. 另外,在拥有zip文件之后,我想删除旧的日志文件。 In other words, I am trying to write a log-backup script. 换句话说,我正在尝试编写一个日志备份脚本。 Can you help me please? 你能帮我吗?

Thank you very much! 非常感谢你!

#!/bin/bash
END=10
for ((i=1;i<=END;i++)); do
   file=log-`date -d "$i days ago" +"%F"`.log
   echo $file
done

With the above script you have file names for last 10 days. 使用上述脚本,您可以获得最近10天的文件名。 Later(inside loop) you can do whatever you want like adding it to existing zip or searching for its existence. 稍后(在内部循环中),您可以执行任何想要的操作,例如将其添加到现有zip或搜索其存在性。

Edit: 编辑:

Following code may be useful according to your requirement 根据您的要求,以下代码可能会有用

#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/home/bhanu/opt/MyStore/action_scripts/deneme/

tendaysbefore=`date -d "$now - 10 days" '+%F'`
for file in $scripthome;
do
        zip -r -tt $now -t $tendaysbefore "$backupfile.zip" $scripthome/log-*.log > add.log 2>&1
        zip "$backupfile.zip" -d "*" -tt $tendaysbefore > delete.log 2>&1
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Shell脚本中修改文件名? - How to modify a file name within a shell script? shell 脚本来检查文件的准确性 - shell script to check accuracy of the file 如何将一个 shell 的 pid 传递给在父 shell 中创建的另一个 shell,并检查父进程是否正在运行或被终止? - How can I pass the pid of one shell to another shell created within the parent shell and check if the parent process is running or killed? 如何检查文件是否存在但是否在Shell脚本的路径中间泛化? - How to check if file exists but generalized in the middle of a path in a shell script? 如何检查Linux文件系统是否在Shell脚本中挂起 - How to Check if Linux File System is Hanging in Shell Script 如何在shell中检查调用脚本 - How to check for the calling script in shell 如何在 Windows 10 中使用 python3.9 检查文件是否在我的 python 脚本之外的另一个进程中主动打开? - How can I check if a file is actively open in another process outside of my python script with python3.9 in Windows 10? 我试图在 Linux 中查找过去 10 天内创建或修改的最大文件 - im trying to find the largest files created or modified within the last 10 days within Linux 使用Shell脚本对照当前日期/时间检查文件夹创建日期 - Shell Script to check the folder created date against the current date/time 使用shell脚本检查文件是否为jpeg格式 - check if a file is jpeg format using shell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM