0

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.

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.

peter p
  • 131
  • 1
  • 10
  • Impossible. You can only guess since the same, identical physical file may have any number of links. If you delete (= `unlink`) one of them, no space is freed. (Worded differently, what `rm` deletes is just a "name", only when no more names that refer to a file remain, it is really deleted.) – Damon Mar 06 '15 at 15:11
  • But `ls -lsai` will show you the number of hard-links for each file, as well as the inode number of the file. This should be enough information for a script to determine whether a file will be really deleted or not (if done in all the directories affected by the `rm -rf` command recursively). But of course, I'd prefer a simpler solution than a script :) – peter p Mar 06 '15 at 15:25
  • 1
    `ls -R1is | awk -M '{inodes[$1] = $2; } END {blocks = 0; for (i in inodes) { blocks += inodes[i]; } print blocks * 1024}'` will be pretty close, though it doesn't work across mount points since the inode numbers arn't unique in that case – nos Mar 06 '15 at 15:50
  • When a program has a handle open to a logfile, the space won't come free after deleting the file. Do you also want `fuser` in your solution, which might has changed write after counting the totals? – Walter A Mar 06 '15 at 21:20
  • @Walter A: Interesting question, but no, for me transient handles are not of importance at this moment. So just consider that I want to know the freed disk space "after I reboot my computer". – peter p Apr 03 '15 at 10:23

1 Answers1

0

du will generally only count a hard linked file once, so du -sk some/directory should work fine.

# 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...

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.

twalberg
  • 59,951
  • 11
  • 89
  • 84