【问题标题】:how to check the directory size using find command in bash script如何在 bash 脚本中使用 find 命令检查目录大小
【发布时间】:2020-12-15 13:00:29
【问题描述】:

我需要找到大于特定值的目录大小,我为此编写了一个脚本。但是 find 命令不接受 -size 部分。谁能帮我解决这个问题

echo -e "This script will generate report for directories which consumes more size than the given value"
read -p " Enter the file system or directory full path: " path
read -p " This script will check the directories which is greater than the given size Please Enter the directory size in GB: " size
find $path  -type d -size +$sizeG -exec ls -ld {} \;

错误

find: invalid -size type `+'

【问题讨论】:

  • 你的命令寻找一个名为sizeG的变量;你想要${size}G,或"$size"G,或"${size}G"
  • 目录上的 -size 可能无法按预期工作。它不计算目录中文件的大小。您可能需要改用du $path 并过滤掉您想要的目录。
  • 根据@TedLyngmo 的评论,我以“不重复”的形式重新打开,因为修复变量名错误不会产生您想要的结果。
  • 这能回答你的问题吗? Check folder size in Bash
  • 如果您想要整个卷的大小,请使用 df 而不是 du

标签: linux bash shell


【解决方案1】:

为了消除 bash 脚本中的错误替换错误。用双倍配额包装可变大小。

    echo -e "This script will generate report for directories which consumes more size than the given value"

read -p " Enter the file system or directory full path: " path
read -p " This script will check the directories which is greater than the given size Please Enter the directory size in GB: " size

echo "+${size}G";

find $path  -type d -size "+${size}G" -exec ls -ld {} \;

【讨论】:

  • 我试过了,但没有用。为了缩小问题的范围,我尝试单独执行 find 命令,发现如果我们使用 -type d 它不会给出结果。查找 /root -size +10M -type d -exec ls -ld {} \;
  • 如果您收到权限被拒绝错误。尝试使用 Sudo 权限。
  • 来自 ubuntu 的示例:fatihsennik@fatihsennik:~$ find / -type d -size +10G -exec ls -ld {} \;查找:'/etc/polkit-1/localauthority':权限被拒绝
  • 没有。这不是权限被拒绝错误。 output 不会得到我们使用 type 的 id 但如果我们使用 type f 会得到结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多