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?
-
1Are you specifically looking for a terminal command, or would something like an automator action suffice?– JerryApr 24, 2016 at 20:33
-
1What do you mean by "whose dates are added?" Added to what and how?– AllanApr 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.– MilesApr 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.– AllanApr 25, 2016 at 11:47
3 Answers
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 commandrm
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:
-
6Instead of using
rm
, you could move the files to~/.Trash
to prevent them for being immediately deleted. Apr 24, 2016 at 23:13 -
2Note 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.– MilesApr 25, 2016 at 0:01
-
2If you have homebrew: brew install trash and instead rm use trash command from now on.– KyslikApr 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.– AllanApr 25, 2016 at 12:23
-
1
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
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.
-
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
inmdls
but I highly recommend the Hazel solution instead.– TJ LuomaMay 17, 2016 at 12:30