How to see disk usage with less overhead in bash?

Solution 1:

As far as a more optimized version du, I am not aware of one. The things that come to mind are:

  1. Store these files on a ram disk or something like that.
  2. If only one application writes to this folder and its sub folder, then have the application keep track.
  3. If all these files are about the same size and evenly amount of them distributed amongst directories, you could just count the number of sub directories and multiply that by file per directory and then size per file. You could do this quickly by just using the hard link count for the directory if only you have only a one directory deep structure (stat -c '%h') - 2.
  4. Make all these files owned by a specific user and use the quota mechanism.
  5. Use a dedicated partition and just use df. A virtual filesystem (a file on a filesystem that is mounted via loopback) could do this too as well.

Out of all these, the quota and dedicated partition options are probably the easiest and most efficient.

Solution 2:

The problem is that 'du' has to enumerate every object in the sub-tree. This is a metadata intensive operation, and takes a while for most Linux filesystems. Some filesystems, NTFS and Novell's NSS come to mind, have the ability to keep track of directory sizes like this in the metadata which makes this operation vastly faster. Generally, if your file-system supports directory quotas of some kind, it has to keep track of this data internally for enforcement, every size-change is replicated up the directory-tree to the quota-point (NTFS) or every directory (NSS) when it happens, so getting a directory-tree size is very fast.

Unfortunately, there isn't a way to make du run faster, just work-arounds.

  • Run 'du' in batch-mode and live with non-live results
  • Create a new file-system and use 'df' instead
  • Create a large file that will hold your directory, loopback-mount it, format it, and use 'df' instead on that new mount-point. If you need more space, unmount the loopback mount, extend the file, and remount.