UNIX How-to: house-keeping script

In an earlier post, I wrote about not using Java for performing house-keeping tasks such as moving, copying or deleting files on the OS (be it UNIX or Windows). In this post, I’m going to reproduce what I did a couple of years ago. The UNIX script/commands for performing these tasks.

The first step in solving these problems would be to find all the target files. Once we find them, we could then perform the necessary remedial actions. We could find the files by their last access (read) or last modified (contents) changed date using the find command. The find command is recursive, so it looks into subdirectory too.

Search for the list of *.log files that were last accessed more than 90 (3 months) days ago:

find /<directory> -iname “*.log”  -atime +90 >> lastaccessedlogsolderthan90days.txt

Noticed that I actually created a text file to hold the list of such files. I find this useful for a couple of reasons:

  • I could iterate through the file contents and perform the necessary actions.
  • It tells me what are the files that I’m going to act on, so I can verify before doing anything irreversible.

Search for the list of *.log files that were last modified more than 90 (3 months) days ago:

find /<directory> -iname “*.log”  -mtime +90 >> lastmodifiedlogsolderthan90days.txt

Note that you should do a man find to check out your options as we’re probably using different versions of UNIX. These commands are tested on MAC OS Tiger.

If you’re searching for files that are last accessed or modified within n days, you should be using these:

-atime -90 or -mtime -90

If it’s meant to be an exact search, then:

-atime 90 or -mtime 90

You could easily modify the command to include the move/copy/delete action. If you’re sure that it is perfectly safe to do so, you could simply do this:

find /<directory> -iname “*.log” -mtime -90 -exec rm {} \;

You could do a copy like this, but your original files are still intact.

find /<directory> -iname “*.log” -mtime -90 -exec cp {} /<target directory> \;

Sometimes you want to move the files to an archive folder for a further period of time before purging, or so that a tape backup can run and it picks up these files at a specific directory.

find /<directory> -iname “*.log” -mtime -90 -exec mv {} /<target directory> \;

If you wish to have visibility of what you copy/move/delete, it is better to find and pipe the results to a text file, then parse the contents, for each row, perform the action and pipe the file name to another results file, then you would know what’s being moved/copied/deleted.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: