Monday, February 3, 2014

Searching big files over in linux with python script.

hi all,

Welcome to download and put this in your /usr/bin directory for handy big files searching..

import os
import sys

# searching for bigfile
def searchbigfile( search_dir ):
        filesize = 0
        filename = ''
        print "Searching directory: %s " %(search_dir)
        for root, dirs, files in os.walk(search_dir):
                for f in files:
                        myfile = os.path.join (root, f)
                        if os.path.isfile(myfile) and os.path.getsize(myfile) > filesize:
                                filesize = int(os.path.getsize(myfile))
                                filename = os.path.realpath(myfile)
                                print "file: %s (%s) " % (filename, sizeof_fmt(filesize))


# get the size in human readable format
def sizeof_fmt(num):
        for x in ['bytes', 'KB', 'MB', 'GB', 'TB' ]:
                if num < 1024.0:
                        return "%3.1f %s" % (num, x)
                num /= 1024.0


if __name__ == '__main__':
        if len(sys.argv) == 2:
                if str(sys.argv)[1]:
                        search_dir = str((sys.argv)[1])
        else:
                search_dir = "/"
        try:
                searchbigfile( search_dir )
        except KeyboardInterrupt, e:
                print "Caught CRTL-C, script is terminated.", e


The result will be something like this.

[root@nasilemak~]# ./bigfile.py
Searching directory: /
file: /javacerts.tgz ==> size: 242.6 KB
file: /lib/modules/2.6.32-358.18.1.el6.x86_64/kernel/fs/cifs/cifs.ko ==> size: 497.5 KB
file: /lib/modules/2.6.32-358.18.1.el6.x86_64/kernel/fs/btrfs/btrfs.ko ==> size: 1.2 MB
file: /lib/modules/2.6.32-358.18.1.el6.x86_64/kernel/fs/xfs/xfs.ko ==> size: 1.7 MB
file: /boot/System.map-2.6.32-279.el6.x86_64 ==> size: 2.2 MB
file: /boot/vmlinuz-2.6.32-358.18.1.el6.x86_64 ==> size: 3.9 MB
file: /usr/share/dict/linux.words ==> size: 4.7 MB
file: /usr/share/cracklib/pw_dict.pwd ==> size: 8.2 MB
file: /usr/lib/locale/locale-archive ==> size: 94.6 MB



1 comment:

Farhan said...

This is great Hiu!!. Looks simple but it does what it meant to. :)