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”…