linux - 使用 find 命令时,‘ls’ 被信号 13 终止

标签 linux bash shell server-administration

<分区>

所以我尝试运行一个脚本,该脚本在 df -h 的内容中搜索超过阈值的目录,基本上是在该目录上运行查找命令以获取前十个最大的文件,然后停止运行查找。但是,当它按预期给出前十个文件时,它会多次吐出:

find: ‘ls’ terminated by signal 13

我的问题很简单,我该如何阻止这些?我想我明白这些是由于我的 find 命令和 head -n 10 命令同时运行,因为 head 在 find 之后通过管道传输,但如果有人能详细说明,那就太好了。我的目标是将其提交给正在工作的权力机构(沃尔玛),以便它开始在我们的测试服务器上运行。

另请注意,find 命令中的大小仅为 5M,因为我的测试服务器上没有 10 个在该目录中那么大的文件。

这是脚本:

#!/bin/bash

#defines what exceeds threshold

threshold="90%|91%|92%|93%|94%|95%|96%|97%|98%|99%|100%|6%"

#puts the output of df -h  into output_df

df -h > output_df

cat output_df | awk -v VAR=$threshold '{if($5~VAR)print $6}' > exceeds_thresh

LINES=()
while IFS= read -r exceeds_thresh
do
find $exceeds_thresh -xdev -size +5M -exec ls -lah {} \; | head -n 10
done < "exceeds_thresh"

#cleaning up the files the script created

rm output_df exceeds_thresh

这是一个示例输出:

-rw-r-----+ 1 root systemd-journal 16M Jun  1 19:18 /var/log/journal/a237b5bc574941af85c796e15b0ce420/system.journal
-rw-r-----+ 1 root systemd-journal 8.0M May 29 05:38 /var/log/journal/a237b5bc574941af85c796e15b0ce420/system@00056d51a41389f0-0c1bef27b9d68ad6.journal~
-rw-r-----+ 1 root systemd-journal 104M Jun  1 05:55 /var/log/journal/a237b5bc574941af85c796e15b0ce420/system@45697f9ed4b84f07b92c5fcbc8a945bd-0000000000000001-00056d51a40f2f0c.journal
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13

最佳答案

这没什么好担心的。这是一个损坏的管道,因为 head 将在所有行写入 stdout 之前完成读取前 10 行。

您可以在末尾使用 >/dev/null 2>&1 或使用 2>/dev/null 使错误静音。我还看到了将 tail -n +1 添加到管道中的另一个技巧:

find $exceeds_thresh -xdev -size +5M -exec ls -lah {} \; | tail -n +1 | head -n 10

这会花费您一些时间,但不会改变结果。

关于linux - 使用 find 命令时,‘ls’ 被信号 13 终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50649866/

相关文章:

c++ - 在没有/proc/self/exe 的情况下查找当前可执行文件的路径

ruby-on-rails - 在 Heroku 的远程存储库之间切换

bash - 匹配后替换行

bash - 将多个文件移动到新目录保留路径

bash - 将输出保存到变量中

linux - grep 命令来计算 perl 中的警报

bash - BSD sed 无法全部替换 ",0,"

linux - linux shell 中的减法 if

Python 脚本未在 cron 中运行

python - iPython 给我一个语法错误