13 Apr 2015 Organize Files

Last year, I dumped my photos and videos from digital camera and few SD Cards in my linux box as backup. Now,I want to organize those files based on the year and the month without loosing files with less effort.I am not remembered whether the files are duplicated.

To my meet goal, I created Python script as ~/organize.py

import os,sys,os.path, time
file=sys.argv[1]
targetBase=sys.argv[2]
if(not file or not os.path.exists(file)):
    print file+" is not exits"
    exit(-1)
if(not targetBase):
    print "no target specified"
    exit(-2)
location=time.strftime("%Y/%m",time.localtime(os.path.getctime(file)))
location=location.replace("/",os.path.sep)
target=targetBase+os.path.sep+location
if not os.path.exists(target):
    os.makedirs(target)
target = target+os.path.sep+os.path.basename(file)
if not os.path.exists(target):
    os.rename(file,target)
    print file +" -> "+ target
else:
    print target + " is already exists"

Execute The following scripts to move files based on created date of file

cd ~/archive/oldphotos
find . -type f -exec python ~/organize.py {} ~/archive/photos
find . -empty -type d -delete # Remove Empty Folders

Now ~/archive/photos are organized like

+ photos
  + 2015
     + 01
        - photo1.jpg
  + 2013
     + 07
        - photo9.jpg

This script can with different combintation with find

find . -name '*.jpg' -type f -exec python ~/organize.py {} ~/archive/photos

If files are duplicated and conflicting with name then those files will be stayed in old folder for manual verification.

comments powered by Disqus

Posted with : Scripts