find 命令移动文件,但随后文件变得无法访问

发布于 2024-12-21 16:06:33 字数 778 浏览 5 评论 0原文

我在脚本的参数化版本中运行了以下命令:

Script1 as

Nooffiles=`find $1 -mmin $2 -type f -name "$3"|wc -l`
if test $Nooffiles -eq 0
then
exit 1
else
echo "Successful"
find $1 -mmin $2 -type f -name "$3" -exec mv '{}' $4 \;
fi

script1 工作正常。它将文件从 $1 目录移动到 $4。但是将文件移动到新目录后,我必须运行另一个脚本,如下所示:

Script2 as

for name in `find $1 -type f -name "$2"`
do
filename=`ls $name|xargs -n1 basename`
line=`tail -1 $filename | sed "s/Z/Z|$filename/"`
echo $line >> $3;
echo $filename | xargs -n1 basename;
done

这里,script2 正在从上一个脚本 script1 移动到的文件目录中读取。由于之前的移动脚本工作正常,因此它们存在于该目录中。 'ls' 命令显示它们。但上面的 script2 说:

File.txt: No such file or directory

尽管 ls 在目录中显示它们,但我收到这样的错误消息。

请帮忙。

I ran the following command in a parametrized version of a script:

Script1 as

Nooffiles=`find $1 -mmin $2 -type f -name "$3"|wc -l`
if test $Nooffiles -eq 0
then
exit 1
else
echo "Successful"
find $1 -mmin $2 -type f -name "$3" -exec mv '{}' $4 \;
fi

The script1 works fine. It moves the files from $1 directory to $4. But after it moves the files to the new directory, I have to run another script like this:

Script2 as

for name in `find $1 -type f -name "$2"`
do
filename=`ls $name|xargs -n1 basename`
line=`tail -1 $filename | sed "s/Z/Z|$filename/"`
echo $line >> $3;
echo $filename | xargs -n1 basename;
done

Here, script2 is reading from the directory where the files were moved to by the previous script, script1. They exists there in that directory since the previous moving script worked fine. 'ls' command displays them. But the above script2 says:

File.txt: No such file or directory

Despite ls shows them in the directory, I am getting an error message like this.

Please Help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巾帼英雄 2024-12-28 16:06:33

您的脚本确实一团糟,请注意,您永远不应该解析文件名(例如 ls 的输出,或不带 -print0 选项的 find )。请参阅 Bash 陷阱 #1

除此之外,我认为问题在于,在您的循环中,您使用 basename 截断了 find 的文件名输出,但随后使用 tail 调用基本文件名作为参数,其中文件实际上并不位于当前文件夹中。

我不明白你在那里做什么,但这是一些更正确的代码,也许可以做你想要的事情:

find "$1" -type f -name "$2" -print0 | while read -d '' name
do
    filename=`basename "$name"`
    tail -1 "$name" | sed "s/Z/Z|$filename/" >> "$3"
    echo "$filename"
done

但是,这个脚本中仍然存在陷阱。从 find 输入奇怪的文件名可能会失败。例如,如果您的文件名包含 sed 特有的字符。或者如果在某个时刻 $filename--help 等等。

Your script really is a mess and please be aware that you should NEVER parse filenames (like the output from ls, or find without -print0 option). See Bash Pitfalls #1.

Apart from that, I think the problem is that in your loop, you truncate the filenames output from find with basename, but then call tail with the base filename as argument, where the file really isn't located in the current folder.

I don't understand what you are doing there, but this is some more correct code that perhaps does next to what you want:

find "$1" -type f -name "$2" -print0 | while read -d '' name
do
    filename=`basename "$name"`
    tail -1 "$name" | sed "s/Z/Z|$filename/" >> "$3"
    echo "$filename"
done

But still, there are pitfalls in this script. It is likely to fail with queer filenames input from find. For example, if your filename contains characters that are special to sed. Or if at some point $filename is --help etc.etc.etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文