Welcome, guest | Sign In | My Account | Store | Cart

List the summed size of a directory (or set of directories) without the GUI. This recipe makes it quick and easy to see where all your disk space has gone! It has been tested under Windows and Unix, however it will probably be most useful under Windows considering the fact that Unix ships with a similar command, du.

Have fun!

Python, 79 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import string, sys, os, getopt
from os.path import *

units = 'b'

def print_path (path, bytes):
	if units == 'k':
		print '%-8ld%s' % (bytes / 1024, path)
	elif units == 'm':
		print '%-5ld%s' % (bytes / 1024 / 1024, path)
	else:
		print '%-11ld%s' % (bytes, path)

def dir_size (start, follow_links, my_depth, max_depth):
	total = 0L
	try:
		dir_list = os.listdir (start)
	except:
		if isdir (start):
			print 'Cannot list directory %s' % start
		return 0
	for item in dir_list:
		path = '%s/%s' % (start, item)
		try:
			stats = os.stat (path)
		except:
			print 'Cannot stat %s' % path
			continue
		total += stats[6]
		if isdir (path) and (follow_links or \
			(not follow_links and not islink (path))):
			bytes = dir_size (path, follow_links, my_depth + 1, max_depth)
			total += bytes
			if (my_depth < max_depth):
				print_path (path, bytes)
	return total

def usage (name):
	print "usage: %s [-bkLm] [-d depth] directory [diretory...]" % name
	print '\t-b\t\tDisplay in Bytes (default)'
	print '\t-k\t\tDisplay in Kilobytes'
	print '\t-m\t\tDisplay in Megabytes'
	print '\t-L\t\tFollow symbolic links (Unix only)'
	print '\t-d, --depth\t# of directories down to print (default = 0)'

# main area
follow_links = 0
depth = 0

try:
	opts, args = getopt.getopt (sys.argv[1:], "bkLmd:", ["depth="])
except getopt.GetoptError:
	usage (sys.argv[0])
	sys.exit (1)

for o, a in opts:
	if o == '-b':
		units = 'b'
	elif o == '-k':
		units = 'k'
	elif o == '-L':
		follow_links = 1
	elif o == '-m':
		units = 'm'
	elif o in ('-d', '--depth'):
		try:
			depth = string.atoi (a)
		except:
			pass

if len (args) < 1:
	usage (sys.argv[0])
	sys.exit (1)
else:
	paths = args

for path in paths:
	bytes = dir_size (path, follow_links, 0, depth)
	print_path (path, bytes)

This recipe could be useful for Windows users who need to determine the summed size of a directory (or directories) without being forced to use the GUI to do so. For example, a common task is to find out where all the disk space has gone. The following command will look to see how large the c:\program files folder is:

python dir_size.py "c:\Program Files"

To narrow it down further, the next command will display the size of each directory inside c:\program files:

python dir_size.py --depth=1 "c:\Program Files"

2 comments

s g 17 years, 7 months ago  # | flag

Pydirstat. pydirstat is a really handy cross-platform, command-line way to generate information about disk usage.

http://developer.berlios.de/projects/pydirstat/

ON WINDOWS

Google for "pydirstat exe" to find the download page of the windows executable or go to http://developer.berlios.de/projects/pydirstat/ and look for the link. Install by clicking the the .exe file. Then go into Windows explorer and right-click the folder or drive letter you want to inspect, choosing then pydirstat option. When the black DOS window finally closes you will find a "dirstat.html" file in the directory of the folder you were inspecting. Double-click that. If you have firefox installed and that's the default then you should be able to move your mouse over the various files to find out their sizes. (Not sure about IE.)

ON LINUX

cd ; wget http://download.berlios.de/pydirstat/pydirstat-0.9.10.tar.gz ; tar xvfz pydirstat-0.9.10.tar.gz; cd pydirstat-0.9.10/; \ cd /; sudo python ~/pydirstat-0.9.10/src/pdshtml.py

Use Firefox to view the resultant "dirstat.html"

ON MACOSX:

cd ; curl http://download.berlios.de/pydirstat/pydirstat-0.9.10.tar.gz > pydirstat-0.9.10.tar.gz ; \ tar xvfz pydirstat-0.9.10.tar.gz; cd /; sudo python ~/pydirstat-0.9.10/src/pdshtml.py; open dirstat.html

(Again, use Firefox to open it.)

a 13 years, 12 months ago  # | flag

atoi() is deprecated. you should use int() instead