How to move only the oldest files out of a directory leaving the new ones

I had a log directory with over 15000 entries from a period of over two years. My task was to move all but the most recent logs into a subfolder. I achieved this using the find command like so:

# cd /path/to/logs
# find . -maxdepth 1 -type f -mtime +90 -exec mv {} old_unprocessed/ \;

Reading from left to right this command searches through the current directory without descending into any subdirectories, looks for regular files with a ‘date last modified’ attribute of 90 days or more ago, and for each file found moves it into the subfolder called ‘old_unprocessed’.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>