Linux 使用 find 计算超过 180 天的文件的总已用磁盘空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17419337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:21:59  来源:igfitidea点击:

calculate total used disk space by files older than 180 days using find

linuxunixcommand-line

提问by dig_123

I am trying to find the total disk space used by files older than 180 days in a particular directory. This is what I'm using:

我正在尝试查找特定目录中超过 180 天的文件使用的总磁盘空间。这是我正在使用的:

    find . -mtime +180 -exec du -sh {} \;

but the above is quiet evidently giving me disk space used by every file that is found. I want only the total added disk space used by the files. Can this be done using findand execcommand ?

但上面很安静,显然给了我找到的每个文件使用的磁盘空间。我只想要文件使用的总添加磁盘空间。这可以使用findexec命令来完成吗?

Please note I simply don't want to use a script for this, it will be great if there could be a one liner for this. Any help is highly appreciated.

请注意,我只是不想为此使用脚本,如果可以有一个单衬,那就太好了。任何帮助都受到高度赞赏。

采纳答案by k-h

@PeterT is right. Almost all these answers invoke a command (du) for each file, which is very resource intensive and slow and unnecessary. The simplest and fastest way is this:

@PeterT 是对的。几乎所有这些答案都会为每个文件调用一个命令 (du),这非常耗费资源,而且速度缓慢且不必要。最简单快捷的方法是这样的:

find . -type f -mtime +356 -printf '%s\n' | awk '{total=total+}END{print total/1024}'

回答by devnull

duwouldn't summarize if you pass a list of files to it.

du如果您将文件列表传递给它,则不会进行总结。

Instead, pipe the output to cutand let awksum it up. So you can say:

相反,将输出通过管道传输到cut并对其进行awk总结。所以你可以说:

find . -mtime +180 -exec du -ks {} \; | cut -f1 | awk '{total=total+}END{print total/1024}'

Note that the option -hto display the result in human-readable format has been replaced by -kwhich is equivalent to block size of 1K. The result is presented in MB (see total/1024above).

请注意,-h以人类可读格式显示结果的选项已被替换-k为相当于 1K 的块大小。结果以 MB 表示(见total/1024上文)。

回答by rpet

Be careful not to take into account the disk usage by the directories. For example, I have a lot of files in my ~/tmpdirectory:

注意不要考虑目录的磁盘使用情况。例如,我的目录中有很多文件~/tmp

$ du -sh ~/tmp
3,7G    /home/rpet/tmp

Running the first part of example posted by devnullto find the files modified in the last 24 hours, we can see that awkwill sum the whole disk usage of the ~/tmpdirectory:

运行devnull发布的示例的第一部分以查找在过去 24 小时内修改的文件,我们可以看到awk汇总~/tmp目录的整个磁盘使用情况:

$ find ~/tmp -mtime 0 -exec du -ks {} \; | cut -f1
3849848
84
80

But there is only one file modified in that period of time, with very little disk usage:

但是那段时间只修改了一个文件,磁盘占用很少:

$ find ~/tmp -mtime 0
/home/rpet/tmp
/home/rpet/tmp/kk
/home/rpet/tmp/kk/test.png

$ du -sh ~/tmp/kk
84K /home/rpet/tmp/kk

So we need to take into account only the files and exclude the directories:

所以我们只需要考虑文件并排除目录:

$ find ~/tmp -type f -mtime 0 -exec du -ks {} \; | cut -f1 | awk '{total=total+}END{print total/1024}'
0.078125

You can also specify date ranges using the -newermtparameter. For example:

您还可以使用-newermt参数指定日期范围。例如:

$ find . -type f -newermt "2014-01-01" ! -newermt "2014-06-01"

See http://www.commandlinefu.com/commands/view/8721/find-files-in-a-date-range

http://www.commandlinefu.com/commands/view/8721/find-files-in-a-date-range

回答by user2059857

Why not this?

为什么不是这个?

find /path/to/search/in -mtime +180 -print0 | du -hc --files0-from - | tail -n 1

回答by alanwaring

You can print file size with findusing the -printfoption, but you still need awkto sum.

您可以find使用该-printf选项打印文件大小,但您仍然需要awk求和。

For example, total size of all files older than 365 days:

例如,超过 365 天的所有文件的总大小:

find . -type f -mtime +356 -printf '%s\n' \
     | awk '{a+=;} END {printf "%.1f GB\n", a/2**30;}'