preload
IronRuby 1.0 Released Textual Table class for Ruby
Apr 21

One of my common Python tools: use this to list out all the directories from the specified root on down, ignoring some common directories that are specified in the program. You might want to modify this code to read in your ignore list. The program will also optionally take a size limit so that it only lists directories where the storage usage is over that limit.

#!/usr/bin/python
#
# listdirtree.py  --  print out the complete directory tree from the specified root directory,
#                          with Ignore list.
#
# usage:
#      >listdirtree.py  [{path to start with}] [{sizelimit to list}]
#      >listdirtree.py / 2000000
#      >listdirtree.py /home
#      >listdirtree.py
#
# John Allen, April 2010
#

import os
import sys

ign = [
  "/sys",
  "/usr/lib",
  "/usr/share",
  "/usr/local/share",
  "/lib/modules",
  "/cdrom",
  "/var/crash",
  "/tmp",
  "/dev",
  "/proc",
  "/devices",
  "/var/run"
]

def showBranch(fcount, fpath, filelist):
  global sizelimit
  for i in ign:
    try:
      fpath.index(i)
      return   # found, don't process
    except:
      t = 1  ## Do nothing

  fs = 0
  for f in filelist:
    fullf = os.path.join(fpath, f)
    if os.path.islink(fullf):  continue
    if os.path.isfile(fullf):
      fsx = os.path.getsize(fullf)
      fs += fsx
      fcount[0] += fsx
      fcount[1] += 1
    if os.path.isdir(fullf):
      fcount[2] += 1
      if (sizelimit == -1) or (fs > sizelimit):
        print fullf + "=>" + str(fs)

def main():
  global sizelimit
  try:
    root = sys.argv[1]
  except:
    root = "."
  try:
    sizelimit = int(sys.argv[2])
  except:
    sizelimit = -1

  sums = [0,0,1]  # 0 bytes, 0 files, 1 dir so far
  os.path.walk(root,showBranch,sums)
  print
  print "There are " + str(sums[2]) + " directories."

if __name__ == "__main__":
  main()
  exit

The program is very handy when you need to see what directories are taking up too much space :)

>./listdirtree.py /var/log 12000000
/var/log/cups=>12263821
/var/log/prelink=>12801421
/var/log/mail=>13008127
/var/log/sa=>13130178

There are 7 directories.
SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon

Leave a Reply