繁体   English   中英

Bash:无此类文件/目录错误

[英]Bash: No such file/directory error

我在下面创建了脚本,该脚本将在两个目录中找到超过5天的文件,并将压缩文件移动到另一个目录。 但是,如果在这2个目录中没有+5天的文件,我就遇到了问题

find: `/home/folder1/*.*': No such file or directory
find: `/home/folder2/*.*': No such file or directory
mv: cannot stat `/home/folder1/*.Z': No such file or directory
mv: cannot stat `/home/folder2/*.Z': No such file or directory

我的脚本是:

#!/bin/bash

find /home/folder1/*.* /home/folder2/*.* -type -f -mtime +5 -exec compress {} \;
mv /home/folder1/*.Z /home/folder1/archive

mv /home/folder2/*.Z /home/folder2/archive

find并不需要这些glob来做您想要的事情(除非您试图专门忽略名称中没有.文件)。 您可以放下它们。

find /home/folder1 /home/folder2 -type -f -mtime +5 -exec compress {} \;

然后,不要盲目地在blob上为可能存在或不存在的文件使用mv ,首先对其进行测试(或使错误静音)。

# nullglob makes the globs result in empty strings instead of staying the glob when they don't match any files.
shopt -s nullglob

f1files=(/home/folder1/*.Z)
if [ "${#f1files[@]}" -gt 0 ]; then
    mv "${f1files[@]}" /home/folder1/archive
fi

f2files=(/home/folder2/*.Z)
if [ "${#f1files[@]}" -gt 0 ]; then
    mv "${f1files[@]}" /home/folder2/archive
fi

这一切都这样说,你find命令是找到.Z要创建在存档文件中/home/folder#/archive ,并在重新压缩archive文件夹。 (除非compress足够聪明,它不对.Z文件执行任何操作,但仍将在这些文件上运行。)

几乎可以肯定您希望这样做,因此您需要使用不在您调用find的文件夹下的存档目录,或者将其专门从find命令中排除,以进行处理。

暂无
暂无

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

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