Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I got a lot of data over the years, and I'm looking for a tool that can get a folder name and then move all the files to subfolders by year, like:

2005
2006
2007
etc..

Anyone knows something or similar tool?

share|improve this question
1  
What year? Creation date year? Last modified date year? A year in the filename? –  Rinzwind Sep 5 '11 at 6:54
2  
@Rinzwind: you can't determine a file's creation date in Linux, can you? –  arrange Sep 5 '11 at 7:04
    
Probably not. I was in Unix mode ;) –  Rinzwind Sep 5 '11 at 7:40

2 Answers 2

Save this text into a file (sort.py) and run it:

./sort.py src-dir/ dst-dir/

It loops through the source directory, gets the mtime of all files and folders, and moves them to the destination directory. Note: It's not recursive! Use it at your own risk!

#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-


import os
import sys
import shutil
from datetime import date

# Check arguments
if len(sys.argv) > 2:
    src = sys.argv[1]
    dst = sys.argv[2]
else:
    print "Arguments: ./sort.py [src] [dst]"
    sys.exit(1)

# Check paths
if not os.access(src, os.R_OK):
    print "Source path not found"
    sys.exit(2)

if not os.access(dst, os.W_OK):
    print "Destination path not found"
    sys.exit(3)

# Start sorting from root
for f in os.listdir(src):
    fpath = os.path.join(src, f)
    mtime = os.stat(fpath).st_mtime
    year = str(date.fromtimestamp(mtime).year)
    ypath = os.path.join(dst, year)
    if not os.access(ypath, os.W_OK):
        os.mkdir(ypath)

    print "Moving %s to %s" % (fpath, ypath)
    shutil.move(fpath, ypath)
share|improve this answer

A solution using bash.

Save the following script to ~/my-script

#!/bin/bash

dst="$1"
shift

for f; do
  year=$(stat -c '%y' "$f" | sed 's/-.*//')
  ext=$(sed 's/.\+\.//' <<<"$f")
  ext=
  [[ $ext == $f ]] && ext=
  dir="$dst/$year/$ext"
  mkdir -p "$dir"
  cp --backup=numbered "$f" "$dir"
done

make it executable, then run

find /path/from -type f -exec ./my-script /path/to {} +

where /path/from and /path/to should be replaced with source and destination directories. For example, supposing all you data are in you home, you can execute

find $HOME/* -type f -exec ./my-script $HOME/DocumentsByYear {} +

where ~/DocumentsByYear and all subdirectories will be create as needed. I put as source path $HOME/* to avoid hidden files.

If you comment the line with ext=, you will obtain, in each year subdirectory a further classification by file extension.

The script take care of name clashes with the backup feature of cp command.

share|improve this answer
    
great can you give me just a real syntax usage, i did not undersatand the /path/to {} part... –  Amir Ashkenazi Sep 6 '11 at 3:42
    
@Amir Ashkenazi: see the modified answer with an added example. –  enzotib Sep 6 '11 at 12:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.