按大小范围搜索文件

J.S*_*ith 6 bash scripts find

我必须按大小查找文件。尺寸是参数。找到文件的结果必须保存到文件中。我已经得到了这个:

touch result.txt 
find /var/log -type f -size $1 -size $2 -exec ls {} \; > result.txt 
Run Code Online (Sandbox Code Playgroud)

脚本向我展示了一些结果,但我不确定它们是否正确并且没有任何内容保存到文件中。
有人可以帮忙吗?

pl_*_*ock 10

使用像:

find /var/log -type f -size -10M -size +1M -exec ls {} \; > result.txt
Run Code Online (Sandbox Code Playgroud)

它将存储大小大于 1Mb 且小于 10Mb 的文件名。

cat result.txt
/var/log/wtmp
/var/log/audit/audit.log.1
/var/log/audit/audit.log
/var/log/anaconda/journal.log
/var/log/mongo/mongod-11.0.0.11.log
Run Code Online (Sandbox Code Playgroud)

如果您作为输入参数传递,则使用如下:

find /var/log -type f -size -"$1"M -size +"$2"M -exec ls {} \; > result.txt
Run Code Online (Sandbox Code Playgroud)

以下是可用的尺寸单位。

  -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)
Run Code Online (Sandbox Code Playgroud)