【问题标题】:How to sort the inode count from largest to smallest如何将inode计数从大到小排序
【发布时间】:2020-01-30 00:43:40
【问题描述】:

我使用下面的代码来显示 inode 和磁盘空间。它工作正常,但我想从最大到最小对计数进行排序。我需要做哪些改变?

我尝试添加sort | uniq -c | sort -rn,但它不起作用。

for DIR in `find $CURDIR -maxdepth 1 -type d |grep -xv $CURDIR |sort`; do
    COUNT=$(GET_COUNT $DIR)
    SIZE=$(GET_SIZE $DIR)

    # Check if exclude arg was used, and if so only output directories above exclude inode count
    if [[ -z $exclude ]] || [[ -n $exclude && $COUNT -gt $exclude ]]
    then
        printf "$format" "  $COUNT" "  $SIZE" "`basename $DIR`"
    fi

我需要从最大到最小的 inode 和磁盘大小计数。

【问题讨论】:

  • “它不工作”是什么意思?如果您有未排序的数据并将其通过管道传输到sort,它将被排序。鉴于您没有展示 GET_COUNTGET_SIZE 做了什么,或者您当前的输出是什么,也没有说明您为什么不只是使用 df,很难说为什么您不能只将您的数据通过管道传输到 sort .
  • github.com/tripflex/inodes/blob/master/inodes 我正在使用这个 bash 脚本。但是当我尝试添加 uniq -c | sort -rn 在现有脚本中,我得到如下错误: du: cannot access ‘ 1 /home/new/cache': No such file or directory

标签: shell sorting inode


【解决方案1】:

不是在循环中处理每个文件夹,而是考虑结合利用“find ... -printf”,将其与适当的表达式结合起来进行过滤(用于排除规则)

find $CURDIR -mindepth 1 -maxdepth 1 - type d -links +${exclude-0} -printf '%n %s %f\n'

在哪里

  • mindepth 将排除顶层目录,
  • ${exclude-0} 将强制表达式为数字(如果未设置排除,则会导致“-links +0”)。
  • printf 用于输出链接计数、文件大小(字节)和基本文件名。

例如:

exclude=2
find . -mindepth 1 -maxdepth 1 -type d  -links +${exclude+0} -printf '%n %s %f\n' 

Output:
3 4096 a
3 4096 b

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多