Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A ruby script to clean a folder based on usage dates UNIX
I wrote this short script when I needed to clean up my Downloads folder (but it can be used on any folder). It looks at when you last accessed each file in the folder, and if it is more than a set number of days old (14 by default), it either prints out the name of the file, moves it to the trash, or deletes it.

Save the file somewhere as clean.rb, and in the Terminal (Applications > Utilities), cd to the directory (e.g., cd Desktop if it's on your Desktop), and type chmod +x clean.rb. If you want, you can edit the top of the file. There are two settings: what you want to do with the file (list, move to trash, or delete), and how long it must be since you accessed it. You can then run the file by typing ./clean.rb Downloads, for example. It will clean out or list the files according to your preference. It will only remove whole directories. Hope this is useful to everyone here!
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[9,217 views]  

A ruby script to clean a folder based on usage dates | 7 comments | Create New Account
Click here to return to the 'A ruby script to clean a folder based on usage dates' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Or use a more appropriate tool for the job.
Authored by: Sciandu on Sep 02, '06 10:35:32PM

Or you could just use a one-liner from the commandline.

To delete files:


find Downloads -mtime +14 | xargs -n 200 --replace={} rm {}

To list files:


find Downloads -mtime +14 -print

To move to trash:


find Downloads -mtime +14 | xargs -n 200 --replace={} mv {} ~/.Trash

To look for files older than 30 days, change the 14 to a 30. To look for files that have been modified in LESS THAN 30 days, change the + to a -. To find out more about what you can do with find, run "man find".

To make a shell script out of any of those, make a file that contains this line:


#!/bin/bash
Followed by one of the above lines, and where you see "Downloads", substitute in "$1". Save the file, do chmod +x filename, and you're off. The script will take the directory to operate on as a parameter.

No offense intended, but Ruby simply isn't the right tool for this particular job. It's an excellent language, and it excels in many areas but shell environments and the dozens of little utilities that support them were created precisely for handling these sorts of tasks and they do so very well with far less effort.

-JF

[ Reply to This | # ]
Or use a more appropriate tool for the job.
Authored by: spicyj on Sep 03, '06 10:36:25AM

Yes, but find is not restricted to only removing whole root level directories. That was my first solution, though.



[ Reply to This | # ]
Or use a more appropriate tool for the job.
Authored by: regeya on Sep 04, '06 01:38:44PM

Not sure *entirely* what you mean, but look in your find manpage for -maxdepth and -type. It may not be called -maxdepth in Tiger's 'find' (one of the bad things about 'standard' command-line utils is that they differ across platforms.) It may be necessary to do this in a couple of lines, which would mean a shellscript, but that's certainly OK.

I have a positively ancient OS 9.x/ASIP installation on which I boot an OS X boot CD once a month and run a shellscript that finds all files modified in the last month that are under 20MB in size, and then I ditto the files, delete the originals, and move the dittoed file into place. It takes very little time and really keeps file fragmentation down. It also made for a ridiculously small shellscript.

BTW, am I thinking right that the original Ruby script could be shortened extensively by using routines from rake?



[ Reply to This | # ]
Or use a more appropriate tool for the job.
Authored by: pinguru on Sep 06, '06 07:05:47AM

Any chance you could post the script? I have been tinkering with something like this for a while but to no avail (no shell scripting experience).

Cheers



[ Reply to This | # ]
Or use a more appropriate tool for the job.
Authored by: darsan on Sep 21, '06 10:56:34AM

I use the following command to clean out old home folders on multi-user workstations:

find /Users -maxdepth 1 -mindepth 1 -mtime +30 -type d ! -name Shared ! -name admin | xargs -n 100 rm -Rd

Change the -mtime option to + however many days you need, adjust the user directories you want to exclude at the ! -name options, for xargs it can be changed as needed. This makes a nice periodic launchd/cron script.



[ Reply to This | # ]
A slight bug-fix for you.
Authored by: Sciandu on Sep 02, '06 10:37:23PM
Unless Ruby does something strange with Unix paths that I'm not aware of, the line that reads:

        FileUtils.mv(entry, File.expand_path('~/Trash/'))
Should actually read:

        FileUtils.mv(entry, File.expand_path('~/.Trash/'))
-JF

[ Reply to This | # ]
A slight bug-fix for you.
Authored by: spicyj on Sep 03, '06 10:39:53AM

Yeah. That's a typo. Thanks.

robg, can you fix that?



[ Reply to This | # ]