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 would like to run a cronjob that ran every morning to move the files that were created on the previous day in a specific directory to be moved to a folder that has been created with the previous dates title.

For example, Motion creates a series of jpg files in a directory called snapshots. I would like to have the script run and find all the files that were created yesterday in the snapshots directory (including avi files it has created), and move them into a folder with yesterday's date as the title on it.

Has anyone tried this before? Does motion already have this functionality built in and I am just not seeing it?

The next step will be to have it auto purge itself after 7-14 days, but that's a whole other post.

share|improve this question
"Has anyone tried this before?" tried?! it is rather custom to do this. Have a look for rsync examples ;) Have a look at – Rinzwind Jun 18 '14 at 18:24

This is very simply done through find. The relevant options are:

   -mtime n
          File's data was last modified n*24 hours ago.  See the  comments
          for -atime to understand how rounding affects the interpretation
          of file modification times.
   -atime n
          File was last accessed n*24 hours ago.  When  find  figures  out
          how  many  24-hour  periods  ago the file was last accessed, any
          fractional part is ignored, so to match -atime +1, a file has to
          have been accessed at least two days ago.
   -exec command ;
          Execute  command;  true  if 0 status is returned.  All following
          arguments to find are taken to be arguments to the command until
          an  argument  consisting of `;' is encountered.  The string `{}'
          is replaced by the current file name being processed  everywhere
          it occurs in the arguments to the command, not just in arguments
          where it is alone, as in some versions of find.  Both  of  these
          constructions might need to be escaped (with a `\') or quoted to
          protect them from expansion by the shell.  

So, to move all jpg files created in the snapshots directory in the last 24 hours, you would run

find /home/yourusername/snapshots/ -name '*jpg' -mtime +0 -exec mv {} /path/to/dest

The {} is replaced by each file name found. QUotes are not necessary since find deals with strange file names gracefully before executing the command. The +0 means "the file was last modified at least 1*24h ago as explained in the atime section quoted above.

If you want to move these to a directory whose name is yesterday's date, you'd have to create it (using the -p option so mkdir does not complain if the directory exists):

mkdir -p $(date -d yesterday +%F)

The date command will print yesterday's date in YYYY-MM-DD format. For example, 2014-06-18. You can combine the two commands into the same find -exec call (the \ after -mtime is only there for readability, it allows you to break a command into multiple lines):

find /home/yourusername/snapshots/ -name '*jpg' -mtime +0 \
 -exec bash -c "mkdir -p $(date -d yesterday +%F) && mv {} $(date -d yesterday +%F)" \;

So, to run this with cron, you could add a line like this to your crontab (run crontab -e):

0 9 * * * find /home/yourusername/snapshots/ -name '*jpg' -mtime +0 -exec bash -c "mkdir -p $(date -d yesterday +%F) && mv {} $(date -d yesterday +%F)" \;

The above would run the find command every day at 9 AM.

share|improve this answer
find /path/to/my/folder/ -type f -mtime -1 -exec ls -l "{}" \;

If you want to move them, you may change the exec command:

find /path/to/my/folder/ -type f -mtime -1 -exec mv "{}" /path/to/my/newfolder/ \;

"{}" is the full path to each file name that matched the search.

The \; is required at the end of the command line.

You can delete them using -delete:

find /path/to/my/folder/ -type f -mtime -1 -delete
share|improve this answer
Wouldn't move essentially "delete" the files? Or is this to do something else? – No Time Jun 18 '14 at 19:39
It would move the files from one folder to the other. I just added the -delete example in case nosaj wanted some information about an auto-purge function. :) – medigeek Jun 20 '14 at 20:58

And here is the "verbose" option as a python script.

What it does is as you describe:

  • It creates a folder, named after yesterday's date, in the destination directory
  • It finds all files of yesterday's date (recursively) in the source directory
  • Moves the files into the newly created folder

    #!/usr/bin/env python3
    import os
    import datetime
    import shutil
    import calendar
    source = "/path/to/source"; destination = "/path/to/destination"
    today = int('%j'))
    daily_folder = destination+"/"+str( = 1))[:10]
    if not os.path.exists(daily_folder):
    if today > 1:
        yesterday = today-1
        yesterday = 366 if calendar.isleap(int(str([:4])-1) else 365
    for root, dirs, files in os.walk(source):   
        for name in files:
            file = root+"/"+name
            filedate = int(datetime.datetime.fromtimestamp(os.path.getmtime(file)).strftime('%j'))
            if filedate == yesterday:
                shutil.move(file, daily_folder+"/"+name)

How to use

Copy the script into an empty file, save it as In the lines, starting with source = and destination =, enter the appropriate paths. Add the following line to the crontab file (crontab -e):

0 9 * * *  python3 /path/to/

Alternatively, You could make it run on startup (login) by adding it to your startup applications (Dash > Startup Appliocations > New). Add the following line: python3 /path/to/

share|improve this answer

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.