如何计算使用“查找”命令搜索的文件的总大小

如何计算使用“查找”命令搜索的文件的总大小

首先,我必须使用 FIND 命令搜索文件,该命令将仅显示 2012 年 12 月的文件

其次我需要查看搜索到的文件的总大小

我用了这个命令

find /storage/backup/rman/ -mtime +90 -mtime -120 -exec ls -lrth {} \;

但它也给了我11月的文件,我不想要

-rw-r--r-- 1 oraprod dba 8.7K Dec  1 22:40 /storage/backup/rman/full_011212.log
-rw-r----- 1 oraprod dba 3.3G Dec 11 22:34 /storage/backup/rman/BKPPROD_aknskgvb.F_bkp
-rw-r----- 1 oraprod dba 34M Dec  1 22:40 /storage/backup/rman/ctrl_011223
-rw-r----- 1 oraprod dba 1.3M Dec  1 22:33 /storage/backup/rman/BKPPROD_8lnrq765.F_bkp
-rw-r--r-- 1 oraprod dba 8.0K Dec 20 22:45 /storage/backup/rman/full_201212.log
-rw-r----- 1 oraprod dba 34M Dec  2 22:42 /storage/backup/rman/ctrl_021223
-rw-r----- 1 oraprod dba 2.5G Dec 11 22:38 /storage/backup/rman/BKPPROD_ajnskgvb.F_bkp
-rw-r----- 1 oraprod dba 34M Dec 13 22:46 /storage/backup/rman/ctrl_131223
-rw-r--r-- 1 oraprod dba 8.0K Dec 17 22:44 /storage/backup/rman/full_171212.log
-rw-r--r-- 1 oraprod dba 8.0K Dec 10 22:38 /storage/backup/rman/full_101212.log
-rw-r--r-- 1 oraprod dba 8.0K Dec 13 22:46 /storage/backup/rman/full_131212.log
-rw-r--r-- 1 oraprod dba 1.4K Nov 30 22:00 /storage/backup/rman/full_301112.log
-rw-r----- 1 oraprod dba 34M Dec  8 22:46 /storage/backup/rman/ctrl_081223
-rw-r--r-- 1 oraprod dba 9.2K Dec 12 22:43 /storage/backup/rman/full_121212.log

当我在命令末尾传递时,du -ch结果是:

8.0K    ./.gnome/gnome-vfs
12K     ./.gnome
24K     ./lsat-0.9.7.1/changelog
920K    ./lsat-0.9.7.1
8.0K    ./.gconf/desktop/gnome/accessibility/keyboard
12K     ./.gconf/desktop/gnome/accessibility
8.0K    ./.gconf/desktop/gnome/peripherals/keyboard/host-mdfr-prod/0
12K     ./.gconf/desktop/gnome/peripherals/keyboard/host-mdfr-prod
16K     ./.gconf/desktop/gnome/peripherals/keyboard
20K     ./.gconf/desktop/gnome/peripherals
36K     ./.gconf/desktop/gnome
40K     ./.gconf/desktop
8.0K    ./.gconf/apps/panel/applets/clock/prefs
12K     ./.gconf/apps/panel/applets/clock
8.0K    ./.gconf/apps/panel/applets/workspace_switcher/prefs
12K     ./.gconf/apps/panel/applets/workspace_switcher
8.0K    ./.gconf/apps/panel/applets/window_list/prefs
12K     ./.gconf/apps/panel/applets/window_list
40K     ./.gconf/apps/panel/applets
44K     ./.gconf/apps/panel
8.0K    ./.gconf/apps/puplet
56K     ./.gconf/apps
100K    ./.gconf
8.0K    ./.nautilus/metafiles
12K     ./.nautilus
4.0K    ./.eggcups
196K    ./.gstreamer-0.10
4.0K    ./.redhat/esc
8.0K    ./.redhat
8.0K    ./.gnome2/share/cursor-fonts
8.0K    ./.gnome2/share/fonts
20K     ./.gnome2/share
4.0K    ./.gnome2/keyrings
4.0K    ./.gnome2/accels
4.0K    ./.gnome2/nautilus-scripts
36K     ./.gnome2
12K     ./.metacity/sessions
16K     ./.metacity
4.0K    ./.Trash
4.0K    ./.gnome2_private
68K     ./.gconfd
4.0K    ./.mozilla/extensions
4.0K    ./.mozilla/plugins
12K     ./.mozilla
4.0K    ./Desktop
2.5M    .
2.5M    total

答案1

某些版本find(非嵌入式 Linux、Cygwin、OSX、FreeBSD)允许您使用运算符将​​文件的修改时间与参考日期进行比较-newermt

find /storage/backup/rman -newermt '2012-12-01' ! -newermt '2013-01-01'

您不能用来-mtime判断文件是否在特定日期被修改,因为该运算符与您运行命令的时间相关find。如果您find没有-newermt运算符,请创建参考文件并使用-newer运算符。

touch -t 201212010000 start
touch -t 201301010000 end
find /storage/backup/rman -newer start ! -newer end

要获取文件的总大小,请du -c仅使用并保留最后一行(“总计”)。您需要排除目录,因为当您将目录传递给 时du,它会将该目录下的所有文件的大小相加。

find /storage/backup/rman -type f -newermt '2012-12-01' ! -newermt '2013-01-01' -exec du -c {} + | tail -n 1

如果您有大量文件,命令行的长度可能太大,因此find会运行du多次,并且上面的命令只会列出最后一批的输出。在这种情况下,您需要将每次运行的金额相加。这是执行此操作的一种方法。

find /storage/backup/rman -type f -newermt '2012-12-01' ! -newermt '2013-01-01' \
     -exec sh -c 'du "$@" | tail -n 1' _ {} + |
awk '{total += $1} END {print total}'

练习:下面的命令有什么问题? (即在什么不寻常但可能的情况下它会报告错误的数字?)

find /storage/backup/rman -type f -newermt '2012-12-01' ! -newermt '2013-01-01' \
     -exec du {} + |
awk '$2 == "total" {total += $1} END {print total}'

答案2

首先检索文件大小(以字节为单位),然后将它们相加:

sed 's/\s\+/+/g' <<<$(find /storage/backup/rman/ -mtime +90 -mtime -120 -exec stat -c "%s" {} \;) | bc

编辑

要查看这些文件,

du -chs $(find /storage/backup/rman/ -mtime +90 -mtime -120)

答案3

https://stackoverflow.com/questions/64649/how-do-i-get-the-unix-find-command-to-print-out-the-file-size-with-the-file-name

find . -name '*.ear' -exec ls -lh {} \;

只是 jer.drab.org 回复中的 h 额外内容。节省在心里转换为 MB 的时间。

相关资源

答案4

对于您的第二个请求(总结文件大小),我正在使用:

find /storage/backup/rman/ -mtime +90 -mtime -120 -printf '%s\n' \
 | awk '{total += $1} END {printf "%.17g\n", total}'
  • -printf '%s\n'仅打印大小(需要 GNU 实现find
  • awk 进行数学计算
  • printf "%.17g\n"避免科学记数法

验证它是否有效:

fallocate -l 1M ./1m.txt
fallocate -l 2M ./2m.txt
echo $(($(stat -c '%s' ./1m.txt) + $(stat -c '%s' ./2m.txt)))
3145728
find ~/ -name '?m.txt' -printf '%s\n' \
 | awk '{total += $1} END {printf "%.17g\n", total}'
3145728

参考:

相关内容