Archive

Posts Tagged ‘extract’

Extract files from a directory to another by file extensions

May 19, 2013 Leave a comment

Due to the complexity of the folders created for testdisk, I decided to implement a script to get the data I am interest in.
Copy this script and save it as script.py in the directory you would like to scan. Run the script on the terminal by:

python script.py “/home/username/path-to-folder”

The argument is the path where you are going to save your extracted files.

import os
import shutil
import sys, getopt
from os.path import join, getsize

def main(argv):
    path = '.'
    try:
        opts, args = getopt.getopt(argv,"")
    except getopt.GetoptError:
        print 'script.py "path"'
        sys.exit(2)
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith((".jpg",".mov",".mp4")) and getsize(join(root,name))/1024 > 100:
                print name, getsize(join(root,name))/1024
                shutil.copy2(join(root,name), args[0])

if __name__ == "__main__":
    main(sys.argv[1:])

Once finished you will have in the folder you specified all the files greater than 100Kb and with extensions “.jpg”,”.mov” and “.mp4”. Feel free to add the extensions you want.

Categories: python, ubuntu Tags: , , ,