[英]How can I check if .tar (not tar.gz) file is corrupt or not, in Ubuntu?
[英]How can I tar gz files with file name date range?
我想制作一个压缩多个文件的 tar.gz 文件,由组成文件名称的确切日期范围选择。 例如,文件名是
system.2020-01-21.log
system.2020-01-22.log
system.2020-01-24.log
...
我想为选择要压缩的文件设置确切的日期范围。 输出文件将是
system.2020-01-20-2020-02-20.log.tar.gz
我在下面尝试了这个。
LAST_LOG_FILES=`find $LOG_HOME/system*.log -newerBt $LAST_LOG_Y_M_D ! -newerBt $CURR_LOG_Y_M_D`
LOG_ZIP="$LOG_HOME/stage.$LAST_LOG_Y_M_D-$CURR_LOG_Y_M_D.log.tar.gz"
tar -czf $LOG_ZIP $LAST_LOG_FILES
但是,这会在下面引发错误。
tar: Cowardly refusing to create an empty archive. Try `tar --help' or `tar --usage' for more information.
我认为这意味着该命令没有设置文件名范围,导致存档为空。
如果你知道更好的命令来做这个操作,你会告诉我吗? 我提前表示感谢。
如果要根据文件名选择要压缩的日志文件,则需要将文件名一一比较。 请您尝试以下操作:
start="2020-01-20"
end="2020-02-20"
log_home="log" # assign to your LOG_HOME
for f in "$log_home"/system.*.log; do
base=${f##*/} # strip leading directory name
if [[ $base > system.$start.log && $base < system.$end.log || $base = system
.$start.log || $base = system.$end.log ]]; then
# if start <= base <= end
files+=("$f") # then add the file to the list to compress
fi
done
if (( ${#files[@]} )); then
# if the list of the files is not empty
log_zip="$log_home/system.$start-$end.log.tar.gz"
tar -czf "$log_zip" "${files[@]}"
fi
${#files[@]}
表示数组files
的元素数。 条件测试避免了列表为空的情况。"${files[@]}"
将数组的元素展开为: "system.2020-01-21.log" "system.2020-01-22.log" ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.