Suppose you have a tree of files and directories, and you want to pack them up into a tar archive. Here is how to find the size of the resulting archive. This is useful if you want to stream an archive out to a browser, but want to send the correct Content-Length header first.
- let F = number of files in the tree
- let D = number of directories in the tree, counting the root
- if there are any non-ordinary files, stop here. results undefined.
- let S = 0, H = 0
- for each f in F, S += ceil(size(f)/512.0)*512, H += 512 – that’s the size of f rounded up to 512-byte blocks
- for each d in D, H += 512
- for each p in (F D), if the full path and name of p (as stored in the archive) is greater than 100 characters, H += 1024
- The size of the archive is S + H + 1024 bytes of zero-fill at the end of the tar
- Round up to (tar blocking factor) * 512 byte records. GNU tar defaults to blocking factor 20. Blocking factor 1 makes the math easier ;)
No, you cannot use ‘du’. I have tried.
Update! Added #7.