Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


Anyone knows something or similar tool?

share|improve this question
What year? Creation date year? Last modified date year? A year in the filename? – Rinzwind Sep 5 '11 at 6:54
@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

Save this text into a file ( and run it:

./ 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]
    print "Arguments: ./ [src] [dst]"

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

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

# 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):

    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



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

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


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.