简体   繁体   English

按日期查找和复制特定文件

[英]Find and copy specific files by date

I've been trying to get a script working to backup some files from one machine to another but have been running into an issue.我一直在尝试使脚本能够将一些文件从一台机器备份到另一台机器,但遇到了问题。

Basically what I want to do is copy two files, one.log and one (or more).dmp.基本上我想要做的是复制两个文件,一个.log 和一个(或多个).dmp。 Their format is always as follows:它们的格式始终如下:

something_2022_01_24.log something_2022_01_24.dmp something_2022_01_24.log something_2022_01_24.dmp

I want to do three things with these files:我想对这些文件做三件事:

  • find the second to last one.log file (ie something_2022_01_24.log is the latest,I want to find the one before that say something_2022_01_22.log)找到倒数第二个.log文件(即something_2022_01_24.log是最新的,我想找到之前的那个说something_2022_01_22.log)
  • get a substring with just the date (2022_01_22)得到一个 substring 与日期 (2022_01_22)
  • copy every.dmp that matches the date (ie something_2022_01_24.dmp, something01_2022_01_24.dmp)复制与日期匹配的every.dmp(即something_2022_01_24.dmp、something01_2022_01_24.dmp)

For the first one from what I could find the best way is to do: ls -t *.log |对于第一个我能找到的最好方法是: ls -t *.log | head-2 as it displays the second to last file created. head-2 因为它显示创建的倒数第二个文件。

As for the second one I'm more at a loss because I'm not sure how to parse the output of the first command.至于第二个我更不知所措,因为我不确定如何解析第一个命令的 output 。

The third one I think I could manage with something of the sort:第三个我认为我可以用这样的东西来管理:

[ -f "/var/www/my_folder/*$capturedate.dmp" ] && cp "/var/www/my_folder/*$capturedate.dmp" /tmp/

What do you guys think is there any way to do this?你们认为有什么办法可以做到这一点? How can I compare the substring?如何比较 substring?

Thanks!谢谢!

Would you please try the following:请您尝试以下方法:

#!/bin/bash

dir="/var/www/my_folder"

second=$(ls -t "$dir/"*.log | head -n 2 | tail -n 1)
if [[ $second =~ .*_([0-9]{4}_[0-9]{2}_[0-9]{2})\.log ]]; then
    capturedate=${BASH_REMATCH[1]}
    cp -p "$dir/"*"$capturedate".dmp /tmp
fi
  • second=$(ls -t "$dir"/*.log | head -n 2 | tail -n 1) will pick the second to last log file. second=$(ls -t "$dir"/*.log | head -n 2 | tail -n 1)将选择倒数第二个日志文件。 Please note it assumes that the timestamp of the file is not modified since it is created and the filename does not contain special characters such as a newline.请注意,它假定文件的时间戳自创建以来未修改,并且文件名不包含特殊字符,例如换行符。 This is an easy solution and we may need more improvement for the robustness.这是一个简单的解决方案,我们可能需要对鲁棒性进行更多改进。
  • The regex .*_([0-9]{4}_[0-9]{2}_[0-9]{2})\.log will match the log filename.正则表达式.*_([0-9]{4}_[0-9]{2}_[0-9]{2})\.log将匹配日志文件名。 It extracts the date substring (enclosed with the parentheses) and assigns the bash variable ${BASH_REMATCH[1]} to it.它提取日期 substring(用括号括起来)并将 bash 变量${BASH_REMATCH[1]}分配给它。
  • Then the next cp command will do the job.然后下一个cp命令将完成这项工作。 Please be cateful not to include the widlcard * within the double quotes so that the wildcard is properly expanded.请注意不要在双引号中包含通配符* ,以便正确扩展通配符。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM