Linux shell 中 "find" 命令的"-newer $file"选项如何工作?



我有以下linux命令。

find ${MOUNT_POINT} -type f -name "VM*" -newer $SENTFILE -print0 | xargs -0 -i cp {} ${TMP_DIR}

我很难理解选项-newer $SENTFILE。有人能解释一下这个选项吗?

From man find:

-newer file
    File  was  modified  more recently than file.  If file is a sym‐
    bolic link and the -H option or the -L option is in effect,  the
    modification time of the file it points to is always used.

如果$SENTFILE用空格展开,您可能会遇到不好的事情(传递给find的几个参数)。我建议像这样引用

 find ${MOUNT_POINT} -type f -name "VM*" -newer "$SENTFILE" -print0  

否则,-newer像pfnuesel一样工作。我猜find正在使用stat(2)(然后比较st_mtime字段为-newer)

当然,SENTFILE是由调用shell(或一些外部脚本)设置的。它应该包含某个文件名。

如果这是某个shell脚本的一部分,请尝试调试该shell脚本,也许可以使用

#!/bin/bash -vx

作为第一行,或者添加

echo SENTFILE is $SENTFILE 2>&1

或者(参见logger(1),然后查看系统日志文件,可能在/var/log/下)

logger -s SENTFILE is $SENTFILE 

您对基本的shell脚本不够熟悉。因此,请阅读高级Bash脚本指南或更好的内容。

最新更新