Posted on

Linux : Find All Files Older Than…

I recently needed to clean up a directory on my Linux box that included hundreds of files. I wanted to get rid of all the files that hadn’t been updated in over a year. At first I decided just to list the files by date:

ls -lt

This will list the files in long format by time (newest files list before old file). This shows me all the details with the oldest files scrolling to the bottom of the window so the last few files above my command prompt are the oldest.

There are hundreds of files more than a year old.

Employing Find

Find is one of the tools I keep in my Linux tool belt. I don’t need it often, but when I do it saves me quite a bit of time. Find is the Swiss Army Knife of Linux search tools. It is complete, thorough, and comes with just about every “doo-dad” (a technical term) for finding files. It does real-time system searches, so unlike locate it does not rely on a secondary database which may become outdated and not give complete results.

The downside of find is that there are so many options. It is easy to choose the wrong option or, more likely, to string together the options in a manner that the search takes forever and you get no results.

The upside, thanks to how the command shells work, is that you can use the output of find to drive other applications. Like ls or rm. The later two are how we’ll employ find.

Find Files Not Touched In A Year

First we can find all the files in our current directory that are ‘stale’ like this:

find ./ ctime +365

In English “find stuff in this directory (./) where the creation time (ctime) is at least 365 days ago”.

The sister option is mtime, which is “modification time”, and may be more appropriate depending on whether you are truly looking for “modified since” (touched at all) or “created since” (date it was first brought into existence).

Now we can combine this with ls to list the results. It may seem redundant, but I like to test the parameter passing of find to another shell command using something innocuous such as ls. So we test like this:

ls -l `find ./ ctime +365`

The back-ticks take the output of find, which is a simple relative-path based list of the files it located, and uses that as the second parameter to ls.

If all looks good we can now force a remove of those files. Be careful with rm -f. You can do irreparable harm with this. There are other options and if you are not comfortable with power tools that can take a limb off with one keystroke, then drop the -f or us one of the myriad of linux admin tools to help you out. I’ll roll the dice and hope all my limbs remain intact:

rm -f  `find ./ ctime +365`

Other Find Options

There are a lot of ways to find files by other attributes such as “delete all files larger than ? MB” or “delete all files older than <this file>”. This is a good resource that explains some of the options and how to perform different types of find operations:

http://www.linuxquestions.org/linux/answers/Applications_GUI_Multimedia/Find_command_0

Good luck & keep your limbs on!

Posted on

Using Find To Help Manage Files On Linux

We found a system administration problem on a  server today that was being caused by incorrect directory permissions.  Any email that passes through the server-wide spam filter was not going through because of permissions on the /home/<domaindir-here>/etc directory.  That directory needs to be owned by mail.

Here is a quick way to update those directories:

 [root@host:home]# cd /home

The find command only lists directories (much, much faster if you know you only need a certain file type like ‘d’), up to 2 levels deep (.  = current directory = level 1), and matching the name etc…

 [root@host:home]# chgrp mail `find /home -maxdepth 2 -type d -name etc`

Now we pass find as a variable list to the ls command to see what we touched.  The ‘d’ on ls also restricts it to directory level output only, so we don’t descend into those directories and list the contents.

 [root@host:home]# ls -ld `find /home -maxdepth 2 -type d -name etc`
drwxr-x---  3 aaron    mail 4096 Feb 10  2008 /home/aaron/etc
drwxr-x---  2 abundatr mail 4096 Oct 20  2009 /home/abundatr/etc
drwxr-x---  3 alutask  mail 4096 Feb 10  2008 /home/alutask/etc
drwxr-x---  3 banks    mail 4096 Feb 21  2008 /home/banks/etc
drwxr-x---  4 chasvol  mail 4096 Feb 10  2008 /home/chasvol/etc
drwxr-xr-x  3 cyberspr mail 4096 May  7 11:24 /home/cyberspr/etc
drwxr-x---  2 daedalus mail 4096 Mar 27  2008 /home/daedalus/etc
drwxr-x---  7 dolphin  mail 4096 Jul 30  2008 /home/dolphin/etc
drwxr-x---  3 dutchbul mail 4096 Feb 10  2008 /home/dutchbul/etc
drwxr-xr-x  2 eatchas  mail 4096 May 10 21:59 /home/eatchas/etc
drwxr-xr-x  2 fireant  mail 4096 May 25 21:16 /home/fireant/etc
drwxr-xr-x  4 jrsint   mail 4096 Jan 11  2008 /home/jrsint/etc
drwxr-x---  3 lance    mail 4096 Jul  9  2007 /home/lance/etc
drwxr-xr-x  2 memoryve mail 4096 Feb 16 10:29 /home/memoryve/etc
drwxr-x---  2 michaelc mail 4096 May 13  2008 /home/michaelc/etc
drwxr-x---  3 modelloc mail 4096 Dec 18 19:22 /home/modelloc/etc
drwxr-x---  3 monstrss mail 4096 Feb 10  2008 /home/monstrss/etc
drwxr-x---  3 nicolas  mail 4096 Feb 10  2008 /home/nicolas/etc
drwxr-x---  3 outdoor  mail 4096 Aug 26  2008 /home/outdoor/etc
drwxr-xr-x  2 perks    mail 4096 Jun  6 15:17 /home/perks/etc
drwxr-x---  2 pout     mail 4096 Jun 15 12:08 /home/pout/etc
drwxr-x---  3 ravenel  mail 4096 Aug 12  2007 /home/ravenel/etc
drwxr-x---  4 remodel  mail 4096 Feb 10  2008 /home/remodel/etc
drwxr-x---  2 saveag   mail 4096 Oct  9  2008 /home/saveag/etc
drwxr-xr-x  2 shoppout mail 4096 Jun 15 16:46 /home/shoppout/etc
drwxr-x---  3 southern mail 4096 Feb 10  2008 /home/southern/etc
drwxr-x---  2 tbcustom mail 4096 Jun 20  2008 /home/tbcustom/etc
drwxr-x---  3 thebicyc mail 4096 Jun 16  2008 /home/thebicyc/etc
drwxr-xr-x  3 theenerg mail 4096 Feb  9  2008 /home/theenerg/etc
drwxr-x---  2 unclelue mail 4096 Dec 14  2009 /home/unclelue/etc
drwxr-x---  2 vanjean  mail 4096 Feb 16  2009 /home/vanjean/etc
drwxr-x---  3 wwwbrea  mail 4096 Dec 18 01:22 /home/wwwbrea/etc

This same technique can be used for any number of commands when you need to work on directories.   Just be careful with it, this can wreak as much havoc as it can repair damage done by other command line tools that have been wielded without care.

This Red Rider BB Gun is loaded.  Be careful out there!  “You’ll shoot your eye out kid”…