递归查找大小范围的文件

递归查找大小范围的文件

使用命令行,如何递归地找到从特定目录开始的所有文件,其中所有这些文件都在某个大小范围内?

另外列出按大小排序的结果。

答案1

您可以使用

find /PATH/TO/specific_directory -size +MIN -size -MAX

有关什么MINMAX可能是什么的准确信息,请检查man find

   -size n[cwbkMG]
          File uses n units of space, rounding up.  The following suffixes can be used:

          `b'    for 512-byte blocks (this is the default if no suffix is used)

          `c'    for bytes

          `w'    for two-byte words

          `k'    for kibibytes (KiB, units of 1024 bytes)

          `M'    for mebibytes (MiB, units of 1024 * 1024 = 1048576 bytes)

          `G'    for gibibytes (GiB, units of 1024 * 1024 * 1024 = 1073741824 bytes)

          The size is simply the st_size member of the struct stat populated by the lstat (or stat) system call, rounded
          up as shown above.  In other words, it's consistent with the result you get for ls -l.  Bear in mind that  the
          `%k'  and  `%b'  format  specifiers of -printf handle sparse files differently.  The `b' suffix always denotes
          512-byte blocks and never 1024-byte blocks, which is different to the behaviour of -ls.

          The + and - prefixes signify greater than and less than, as usual; i.e., an exact size of  n  units  does  not
          match.   Bear  in mind that the size is rounded up to the next unit.  Therefore -size -1M is not equivalent to
          -size -1048576c.  The former only matches empty files, the latter matches files from 0 to 1,048,575 bytes.

更新以满足您的新要求

find /PATH/TO/specific_directory -size +MIN -size -MAX -print0 | du --human-readable --files0-from=- | sort --human-numeric-sort

或者,以其简短形式:

find /PATH/TO/specific_directory -size +MIN -size -MAX -print0 | du -h --files0-from=- | sort -h

更新以满足您的新要求 (2)

find /PATH/TO/specific_directory -size +MIN -size -MAX -print0 | du --human-readable --bytes --files0-from=- | sort

答案2

zsh1K – 10M 之间的文件:

print -l /PATH/TO/DIR/**/*(.Lk+1Lm-10oL)
  • 递归全局:**/*
  • 对于普通文件:(.)
  • > 1KiB:(Lk+1)
  • 但 < 10MiB:(Lm-10)
  • 按大小升序排列:(oL)

相关内容