首页 > 解决方案 > 如何将inode计数从大到小排序

问题描述

我使用下面的代码来显示 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 和磁盘大小计数。

标签: shellsortinginode

解决方案


不是在循环中处理每个文件夹,而是考虑结合利用“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

推荐阅读