8
du -csh /

The above will take huge amount of time to calculate,is there a way to see the less accurate result with less overhead?

UPDATE

What I want to know is the total size under a specific directory.

13 Answers 13

9

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.

6

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.
1
4
du -h --max-depth=1

Gives you the sizes in human KB/MB/GB format from your current working directory.

2
  • I think they want to know why it takes so long for du to generate and answer. I think windows acts the same too. If you right click a directory then you have to wait while it calculates the size of every subdirectory. It's just the way the filesystem works. Apr 11, 2010 at 18:20
  • Yeah, unless they run an indexing service or something. Apr 11, 2010 at 19:13
3

With the standard tools you have to get the size of every file in the directory each time you want to know the total size. A possibly more efficient way to do it would be to have a "directory size monitor", which keeps track of the current size of the directory. There is no such thing (that I know of), but you could implement one with inotify. Possibly not with bash (and inotify-tools), but you could probably use python and pyinotify, for example.

2

You might find gt5 to be useful. It stores the most recent du information and does a diff against that the next time it's run. It displays its output using a text-mode browser such as links.

2

Also, consider the -x option on du -- "one file system" if your doing

du -xh / --max-depth=1 

You'll only see the disk usage summary on your root partition, and it won't try to add up /proc, /sys, /dev, and so on...

2

if you only want disk usage for root file system, then

df -h /

1

No. Could you periodically do a du piped to a text file, in a cron job set to run overnight, so you've got not quite current data immediately available?

Note that measuring the disk space used by a folder containing a large no of files under Windows similarly takes some time.

0

df -h will do the trick

1
  • What I want to know is the total size under a specific directory.
    – apache
    Apr 11, 2010 at 16:17
0

df -kh /

this will show you the amout of disk space used and available.

0

Perhaps

df -h

extra text since a short, concise answer isn't enough for serverfault to think I am human.

6
  • But I need to know the total size under a specific directory
    – apache
    Apr 11, 2010 at 16:12
  • then use will need to use du Apr 11, 2010 at 16:15
  • But in windows,I can see the total used space with no overhead,why can't in linux?
    – apache
    Apr 11, 2010 at 16:17
  • are you serious? ntfs is not ext3 Apr 11, 2010 at 16:20
  • 1
    @apache, oh there is overhead, try seeing the size of a 20GB folder with lots of 10-20 MB files. There is def overhead.
    – Zypher
    Apr 11, 2010 at 19:17
0

To find the space under just that directory, list it instead of /. To find the space under directories foo and bar, list both:

du -csh foo bar
0

You should use dnotify to calculate the disk usage if the filesystem is not changed often.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .