ksh :需要快速可靠地删除多个目录

发布于 2024-08-31 01:44:21 字数 320 浏览 5 评论 0原文

我有很多目录,需要以最短的时间定期删除它们。 另外,对于每个目录的删除状态需要知道,即是否删除成功。 我需要写在 ksh 上。你能帮我一下吗?

我正在使用的示例代码(我尝试在后台启动 rm-rf)无法正常工作。

for var1 in 1...10
rm -rf <DIR> &
pid[var1]=$!
done

my_var=1
for my_var in 1...var1
wait $pid[my_var]
if [ $? -eq 1 ]
then
  echo falied
else
 echo passed
fi
done

I have many directories and need to delete them periodically with minimum time.
Additionally for each directories delete status need to know i.e whether deleted successfully or not.
I need to write on the ksh . Could you please help me out.

The sample code which I am using, in which I tried launching rm-rf in the background, is not working.

for var1 in 1...10
rm -rf <DIR> &
pid[var1]=$!
done

my_var=1
for my_var in 1...var1
wait $pid[my_var]
if [ $? -eq 1 ]
then
  echo falied
else
 echo passed
fi
done

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

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

发布评论

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

评论(2

如梦 2024-09-07 01:44:21

你运气不好。 这里的瓶颈是文件系统,并且您不太可能找到一个并行执行原子操作(如目录删除)的文件系统。无论对 shell 代码进行多少修改,都不会让操作系统或文件系统更快地完成其工作。只有一张磁盘,如果每次删除都需要写入磁盘,速度会很慢。

最好的选择是切换到可以快速删除的日志文件系统。我很幸运地使用 XFS 快速删除了大文件(10-40GB),但我还没有尝试过删除目录。无论如何,提高性能的途径在于找到正确的文件系统,而不是正确的 shell 脚本。

You are out of luck. The bottleneck here is the filesystem, and you are very unlikely to find a filesystem that performs atomic operations (like directory deletion) in parallel. No amount of fiddling with shell code is going to make the OS or the filesystem do its job faster. There is only one disk, and if every deletion requires a write to disk, it is going to be slow.

Your best bet is to switch to a journaling filesystem that does deletions quickly. I have had good luck with XFS deleting large files (10-40GB) quickly, but I have not tried deleting directories. In any case, your path to improved performance lies in finding the right filesystem, not the right shell script.

梦冥 2024-09-07 01:44:21

这通常是您的脚本应采用的形式,并对语法中的严重错误进行更正。然而,正如诺曼指出的那样,它不会做你想做的事。另外, wait 不会像您预期的那样在循环中工作。

# this script still won't work
find . -type d | while read dir
# or: for dir in ${dirs[@]}
do
    rm -rf $dir &
    pid[++var1]=$!
done

for my_var in {1...$var1}
do
    wait ${pid[my_var]}
    if [ $? -eq 1 ]
    then
        echo failed
    else
        echo passed
    fi
done

This is generally the form that your script should take, with corrections for the serious errors in syntax. However, as Norman noted, it's not going to do what you want. Also, wait isn't going to work in a loop like you seem to intend.

# this script still won't work
find . -type d | while read dir
# or: for dir in ${dirs[@]}
do
    rm -rf $dir &
    pid[++var1]=$!
done

for my_var in {1...$var1}
do
    wait ${pid[my_var]}
    if [ $? -eq 1 ]
    then
        echo failed
    else
        echo passed
    fi
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文