简体   繁体   English

如何确定运行rm -rf时释放的空间量

[英]How to determine the amount of space that comes free when running rm -rf

Here is a nice system-administrator problem I ran into today, perhaps somebody here knows a solution. 这是我今天遇到的一个很好的系统管理员问题,也许这里有人知道解决方案。 The problem is quite simple: I want to know, beforehand, how much disk space is freed when performing an rm -rf command on Linux. 问题很简单:我想事先知道在Linux上执行rm -rf命令时释放了多少磁盘空间。

Notice that simply using du will not work, because there may be files that are hard-linked from directories that are not removed by the rm -rf command. 请注意,简单地使用du将不起作用,因为可能存在从rm -rf命令未删除的目录中硬链接的文件。

du will generally only count a hard linked file once, so du -sk some/directory should work fine. du通常只计算一次硬链接文件,所以du -sk some/directory应该可以正常工作。

# ls -li
total 2048
131346 -rw-r--r-- 2 root root 1048576 Mar  6 13:12 file1
131346 -rw-r--r-- 2 root root 1048576 Mar  6 13:12 file2
# du -sk file1
1024    file1
# du -sk file2
1024    file2
# du -sk .
1028    .

The extra 4k is for the directory itself, but note that it does not claim that 2M is used by this directory... 额外的4k用于目录本身,但请注意,它并未声称此目录使用了2M ...

As you observe, though, this will not take into account hard links that are outside your target. 但是,正如您所观察到的,这不会考虑目标之外的硬链接。 There's probably not an easy way around this without writing a script that walks the entire directory hierarchy to find all the other links to a file... du will report an upper bound, though - you will get back some amount of space that is less than or equal to what du reports. 如果没有编写一个遍历整个目录层次结构的脚本来查找文件的所有其他链接,那么可能没有一个简单的方法... du会报告一个上限,但是 - 你会得到一些较少的空间超过或等于du报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM