linux - bash 错误 : find: I cannot figure out how to interpret ‘2018-26-01’ as a date or time

标签 linux bash shell find

我有一个日志备份,每天运行 5 次。

2.1G -rw-r--r-- 1 root root 2.1G Jan 25 03:50 xxxxxxx.jrnl.428612
514M -rw-r--r-- 1 root root 512M Jan 25 11:50 xxxxxxx.jrnl.428629
2.1G -rw-r--r-- 1 root root 2.1G Jan 25 19:50 xxxxxxx.jrnl.428664
 18M -rw-r--r-- 1 root root  18M Jan 25 20:32 xxxxxxx.jrnl.428699
194M -rw-r--r-- 1 root root 193M Jan 25 23:50 xxxxxxx.jrnl.428704

我有一个脚本的开头,该脚本将在 04:00 运行以复制在两者之间创建的文件 03:45 和 03:55 到标记为“1”的子目录。

 #!/bin/bash

 # Daily Journal Backup Number 1 

 # Establish target directory
DIRsource="/mnt/Backup/INCREMENTAL/*"
DIRdestination="/mnt/Backup/Yesterday/JOURNAL/1"

 # Calculate the current date, concatenate with specific target time
TIMESTART="$(date '+%Y'-'%d'-'%m') 03:45:00"    # $TIMESTART returns 2018-25-01 03:45:00
TIMESTOP="$(date '+%Y'-'%d'-'%m') 03:55:00" # $TIMESTOP  returns 2018-25-01 03:55:00

 # If the destination directory is not empty, delete all files in it.
if [ "$(ls -A $DIRdestination)" ]; then
    # "Take action $DIRdestination is not empty"
    rm -f $DIRdestination/*

 # Look for the files which have a modify time between 20:25:00 and 20:45:00 on the prior day
find $DIRsource -type f -newermt $TIMESTART ! -newermt $TIMESTOP -exec cp --preserve=timestamps {} $DIRdestination/ \;

脚本运行时出现此错误:

find: I cannot figure out how to interpret ‘2018-26-01’ as a date or time

我该如何解决这个解释问题?

最佳答案

日期的顺序应该是年-月-日,而不是年-日-月。所以将其更改为:

TIMESTART="$(date '+%Y-%m-%d 03:45:00')"
TIMESTOP="$(date '+%Y-%m-%d 03:55:00')"

关于linux - bash 错误 : find: I cannot figure out how to interpret ‘2018-26-01’ as a date or time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48466493/

相关文章:

linux - 实现可移植的文件锁定机制

linux - 如何从 jenkins shell 输出中隐藏密码

Linux shell su -c 'sed' 命令

linux - linux 上的脚本 shell 和 solaris 上的脚本 shell 之间的区别

java - 在没有 JRE 的计算机上运行 JAR。使其便携

c++ - read() 的不同行为取决于在写入不可写内存时表示文件、匿名管道或套接字的文件描述符

c - 如何获取物理 CPU 数 :s in Linux using C?

arrays - 将引用和未引用内容混合的文件读取到 bash 数组中,保留引号

arrays - 如何在 bash shell 脚本中将多行输入读取到数组中

Python。 from dateutil.relativedelta import * 在 shell 中有效,但在脚本中无效