2

I want to make room in my OS X hard drive by identifying and removing "dead wood", i.e. files that have not been used in a long time (say, longer than 2 years).

Is there a general tool for identifying old, unused files?

(Note 1: I know of a tool for removing localization files, but I am looking for something far more general than that. Likewise, I know of tools of to identify large files, but this is not what I am looking for at the moment.)

(Note 2: I'm not interested in a tool thst automatically deletes the old, unused files it finds; I want to be able scan the list of candidate files, and remove any that I want to keep despite their being old and inactive.)

Thanks!

Michael S.
  • 3,987
  • 8
  • 42
  • 64
kjo
  • 1,201
  • 4
  • 20
  • 39

3 Answers3

5

Using mdfind (Spotlight)

As OS X indexes everything using Spotlight, you have a very powerful command line tool to find what you need:

mdfind -onlyin <directory> 'kMDItemLastUsedDate <= $time.today(-<days>)'

would for example list everything in <directory> that you haven't opened for <days>. You can of course leave out the onlyin if you want to search system-wide. I'd recommend however to look in specific directories.

You can modify this query with:

  • $time.this_week
  • $time.this_month
  • $time.this_year

and of course also add $time.this_week(-5), for example, if you want something that hasn't been opened for 5 weeks. Or, in your case, using $time.this_year(-2) for something that wasn't used in the last two years.

Also, you can access other attributes:

  • kMDItemContentModificationDate (when it was modified)
  • kMDItemContentCreationDate (when it was created)

There are a number of other queries you can make. See here for a list of other attributes.


Using find

A simple listing of files in <directory> that have been modified more than <d> days ago:

find <directory> -type f -mtime +<d>

So, for example

find ~/Documents -type f -mtime +150

lists all your files in ~/Documents that you haven't modified for at least 150 days.

mtime is the Unix modification time of a file which is not changed when you (or the system) reads it. Conversely, if you want to list files that have been modified within the last 150 days, you would use -150 instead of +150.

Just like mtime, there is also atime, which designates the last access time. Same syntax:

find ~/Documents -type f -atime +150

But I'm not too sure it's too precise. At least on OS X, Spotlight seems to be the better option here.

slhck
  • 223,558
  • 70
  • 607
  • 592
  • This is likely going to select hundreds of system-files that are read, not modified. – pberlijn May 17 '11 at 15:38
  • No, that's what the `mtime` is for ("modified" time). Reading a file doesn't change its `mtime`. – slhck May 17 '11 at 15:40
  • Yes, that was my point. It seems that he is asking to delete files that have been 'unused'. Reading a file is still using it. Ergo, mtime does not find 'unused' files. – pberlijn May 17 '11 at 15:44
  • @pberlijn Oh, right. Updated my answer. – slhck May 17 '11 at 16:02
3

To do this in Finder, select the folder you want to search (e.g. your home directory) and press Cmd-F.

Hold down Option and click the ... button (which is a + button when you don't press Option) to the right of the predicate editor (the bar with the search criteria).

Select none where it says any for the new criteria block, and select either Last Opened Date (probably the default) or Last Modified Date in the next line. Then, enter your desired time range. It should look somewhat like this:

enter image description here

You don't need to actually enter a search expression, it'll just list the files that haven't been modified in some time.

Doing this from Finder has the advantage that it's very easy to add additional criteria, and to sort and view the resulting files.

See this answer on how to add the size column to the Spotlight results list to selectively delete old and large files.

Daniel Beck
  • 109,300
  • 14
  • 287
  • 334
  • Oh, +1. I'm so hooked on the Terminal that I forgot it has this UI feature. D'oh. – slhck May 17 '11 at 17:29
  • @slhck Thanks. It's easy to forget that though ;) —— Could you check that *Last Opened Date* is broken (at least with this method)? I find several more recent files, that were written but never opened again. The *Last Opened* column says *today* though, but it's a result anyway. – Daniel Beck May 17 '11 at 17:37
  • @Daniel I'm not 100% sure but judging from all I've tried the results seem reasonable. At least I'd consider them useful :) – slhck May 17 '11 at 18:14
  • @slhck So you don't have [this problem](http://i.stack.imgur.com/eHzG5.png)? – Daniel Beck May 17 '11 at 18:39
  • @Daniel Oh now I see what you mean exactly. Seems as if the attribute is set even if *you* don't open the file. [At least here it's BibDesk.app](http://i.imgur.com/vERxv.png) and a few Adobe folders. All other files seem to be C headers and `/Developer` apps. Strange. March 20 to 25th I set up that machine, installing CS4 and XCode. – slhck May 17 '11 at 18:51
  • 1
    @slhck Well I'm not too concerned about the value in the list, more that they're in the results list. Also, you'd expect them to disappear once you add the *Last Modified* criterium, but they still show up. Remove *Last Opened* (leaving only *Last Modified*) and they disappear. It appears that *Last opened date* is ridiculously useless. – Daniel Beck May 17 '11 at 18:56
  • @Daniel Duh, that's weird. Well, nice to know that at least it *doesn't really work* ;) – slhck May 17 '11 at 19:14
  • Thanks++ !!! I wish I could accept both slhck's and your reply, they're both great. I went with slhck's only because of my fanatical bias in favor of the CLI. – kjo May 21 '11 at 21:37
  • @Daniel Do you have any objections to me combining our answers and writing a post for the SU blog? – slhck May 25 '11 at 10:30
  • @slhck Not at all. Go ahead. (if you see a post of mine that seems like good blog material, please let me know -- I'm still struggling with that) – Daniel Beck May 25 '11 at 10:35
  • @Daniel Fine, I'll cc you when I have a draft! – slhck May 25 '11 at 10:42
0

Spotlight.

Enter some wildcard for the type of files you want to delete (or a general wildcard), and add the attribute 'Last opened date'. Set this to your liking.

Be careful what you delete.

pberlijn
  • 9,600
  • 1
  • 15
  • 8