12

I am using a Mac. How can I use a command to delete all files in ~/Downloads whose dates added are greater than 1 year?

4
  • 1
    Are you specifically looking for a terminal command, or would something like an automator action suffice?
    – Jerry
    Apr 24, 2016 at 20:33
  • 1
    What do you mean by "whose dates are added?" Added to what and how?
    – Allan
    Apr 24, 2016 at 20:43
  • 1
    @Allan Check "View > Arrange By" or the columns available in "View Options" in the Finder; OS X tracks the date an item was added to a folder.
    – Miles
    Apr 25, 2016 at 0:08
  • @Miles - those attributes aren't available in bash unless you install XCode Dev tools and the OP specifically used ~/Downloads which is a bash (or more generically, a shell) convention.
    – Allan
    Apr 25, 2016 at 11:47

3 Answers 3

24

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:

5
  • 6
    Instead of using rm, you could move the files to ~/.Trash to prevent them for being immediately deleted. Apr 24, 2016 at 23:13
  • 2
    Note that date modified (which this answer is operating on) is different than date added. Furthermore, Safari will set the modification date of a downloaded file to the one provided by the server if available, so even a file downloaded yesterday can have a modification date more than a year old.
    – Miles
    Apr 25, 2016 at 0:01
  • 2
    If you have homebrew: brew install trash and instead rm use trash command from now on.
    – Kyslik
    Apr 25, 2016 at 0:10
  • I updated my answer based on the suggestion by @lucasoldaini I prefer to use built in methods rather than use homebrew or MacPorts.
    – Allan
    Apr 25, 2016 at 12:23
  • 1
    I like your solution more than mine.
    – Jerry
    Apr 25, 2016 at 21:40
10

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

7

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

1
  • This is the best solution, as it actually uses Date Added rather than Date Modified, which may not be the same. A command-line solution could look for kMDItemDateAdded in mdls but I highly recommend the Hazel solution instead.
    – TJ Luoma
    May 17, 2016 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .