Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.
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.

share|improve this question
add comment

13 Answers 13

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.

share|improve this answer
add comment
du -h --max-depth=1

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

share|improve this answer
    
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. –  The Unix Janitor Apr 11 '10 at 18:20
    
Yeah, unless they run an indexing service or something. –  David Rickman Apr 11 '10 at 19:13
add comment

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.

share|improve this answer
add comment

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.
share|improve this answer
    
+1 This is a very useful answer. –  Richard Holloway Apr 13 '10 at 13:46
    
excellent answer! –  fpmurphy1 Apr 19 '10 at 15:03
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

df -h will do the trick

share|improve this answer
    
What I want to know is the total size under a specific directory. –  apache Apr 11 '10 at 16:17
add comment

df -kh /

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

share|improve this answer
add comment

Perhaps

df -h

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

share|improve this answer
    
But I need to know the total size under a specific directory –  apache Apr 11 '10 at 16:12
    
then use will need to use du –  The Unix Janitor Apr 11 '10 at 16:15
    
But in windows,I can see the total used space with no overhead,why can't in linux? –  apache Apr 11 '10 at 16:17
    
are you serious? ntfs is not ext3 –  The Unix Janitor Apr 11 '10 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 '10 at 19:17
show 1 more comment

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
share|improve this answer
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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

df -h /

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.