Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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'm running an unnecessarily complex script which looks similar to this command:

find /home/user/videos/*.avi -mtime +0 -exec mv -f {} "/home/nb/videos/1 day old" \;

And then I run the same command, but this time, from the "1 day old" folder into a "2 days old" folder. And some more commands keep repeating all the way up to 7 days old! So each day, files from the previous "X days old" folder move into the next day up's folder.

I'm basically trying to organise files automatically, running in cron, to move files older then X days into the correct folder. It's inefficient, but I do need this to happen. Is there any easier, more efficient way?

Once it gets to the 7 days old folder, I have the final command which removes files older than 7 days from that last folder.

share|improve this question

I use (for a similar situation) a non-standard way: logrotate.

This will back up all videos in /home/user/videos to the /home/nb/videos/backup/videos.tar file (daily), and then automatically move the old tar files to ...1.tar, ...2.tar etc. Backups older than 7 days will be deleted.

I included mv /home/user/videos/*.avi /var/tmp to keep a copy of videos in case anything goes wrong.

To activate this logrotate configuration put a similar file to this to the /etc/logrotate.d directory:

/home/nb/videos/backup/videos.tar {
  rotate 7

    if ls /home/user/videos/*.avi > /dev/null 2>&1; then
      tar cf /home/nb/videos/backup/videos.tar /home/user/videos/*.avi
      mv /home/user/videos/*.avi /var/tmp

For more information see man logrotate.

share|improve this answer
Thanks. Your idea has got me thinking. Instead of tar-ing them, is there a command I can use, to put all files created on its creation date, move into a folder with the same date? And this command can be run as a cron job daily for example. Any way to do this? And of course, any files created after the command has run, should still go into the same correct folder if the command is soon run again. – nLinked Dec 10 '11 at 17:59
It's definitely possible, but not via logrotate, as it can only rotate files, not directories. Maybe someone will post a solution for this case. – arrange Dec 10 '11 at 18:19

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.