How can I use a command to delete all files in Downloads whose dates added are greater than 1 year?

Solution 1:

It's a relatively simple command to delete files within a directory greater than one year. For instance, the following command executed in Terminal

find ~/Downloads/ -type f -mtime +365 -exec rm {} \;

will delete all files in the ~/Downloads directory older than one year.

  • find is the command that searches for "stuff"

  • -type f tells find what to look for, in this case files

  • -mtime +365 tells find that the modified time should be greater than 365 days

  • -exec rm {} passes (executes) each "found" item to the command rm

What I like to do is test it out by excluding the -exec rm {} \; portion and make sure I am getting the correct results. Once I am certain, I just add it in to complete the remove.

One Caveat!! These files are not added to your "Trash." The command bypasses the Trash and are immediately deleted, so use with caution.


A Safer Way...

(Thanks to user lucasoldaini in the comments below)

If you want to move your files to the "Trash", simply replace the -exec rm {} \; portion to -exec mv {} ~/.Trash \;

They will remain in your Trash until the next time you empty it.

You can get more information on each command by viewing their man pages:

  • man find
  • man exec

Solution 2:

If you want to use Automator you can build an action like so:

Find Finder Items - Search Downloads - All of the following are true: -Date Created is not in the last 365 days.

Get Selected Finder Items

Move Finder Items to Trash

Convenient Screenshot: Automator Actions

Solution 3:

My choice of tool is Hazel, a low cost multipurpose piece of software that does things to files and folders when they match certain rules that you set up. This rule will move files not added the last year from the folder Hämtade filer. In differs from the command line solution above as it runs every day, so its a set and forget solution. Screenshot of Hazel pref pane