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?

Larry
  • 121
  • 1
    Are you specifically looking for a terminal command, or would something like an automator action suffice? – Jerry Apr 24 '16 at 20:33
  • 1
    What do you mean by "whose dates are added?" Added to what and how? – Allan Apr 24 '16 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 '16 at 00: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 '16 at 11:47

3 Answers3

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:

Pang
  • 437
Allan
  • 101,432
  • 6
    Instead of using rm, you could move the files to ~/.Trash to prevent them for being immediately deleted. – lucasoldaini Apr 24 '16 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 '16 at 00:01
  • 2
    If you have homebrew: brew install trash and instead rm use trash command from now on. – Kyslik Apr 25 '16 at 00: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 '16 at 12:23
  • 1
    I like your solution more than mine. – Jerry Apr 25 '16 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

Jerry
  • 444
  • 3
  • 10
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

reboot
  • 429
  • 3
  • 4
  • 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 '16 at 12:30