I love find

There is no other way to put it. It’s such a fantastic tool and once you get used to it, you almost never use the sub par “locate” utility to find files.

Some of my favorite find switches and one liners are listed below. Explanations too.

Find all directories in the current directory only without going any deeper that start with “20″. Note that I have included ‘-mindepth 1′ and there is a reason for that. Try excluding it and you’ll see exactly what I mean.

find ./ -maxdepth 1 -mindepth 1 -type d -name "20*"

Find all files named config.php in this directory and all sub directories. Print the name of the file and run ‘ls -al’ on the file. run a grep command for “string1″ (case insensitive) against the files. I find this command very useful in doing a single replacement across many files. It helps me determine what will change if I run a mass replacement. The “{}” at the end is the name of the file that is replaced in the command. Without that, you will get an error.

find ./ -name "config.php" -ls -print -exec grep -i string1 {} \;

Now I can find those same files and then run a sed command that will replace all occurrences of string1 with string2. The “-i” in this case tells sed to make the change to the file in place without creating a new file. This way you have less to clean up but be careful because there is no backup. If you want to backup the file, use “-i” where is the extension you want the backup file to have. E.g. -i.bak

find ./ -name "config.php" -exec sed -i "s/string1/string2/g" {} \;

Another use of exec where you sub {} for the file name allows you to do cool things like back up many different files with the same condition. In this case, anything that is “config.*” will be “config.*.bak”.

find ./-name "config.*" - exec cp {} {}.bak \;

Find any directories that have a no permissions for group and others. This is important if you want to find directories that don’t have permissions that are needed for users to enter a directory. After this, you can use -exec chmod 755 {} \; to change the permissions appropriately.

find ./ -type d -perm 0700

Find any files that are owned by a particular user. You can also specify “-group” to find files by user and group names. I use this to find files owned by “root” in home directories and then chown them to the appropriate user.

find ./ -user "username"

The -exec feature in find is perhaps the most valuable aspect of “find”. I use it several times a day in combination with sed and awk to do all sorts of one liners that would normally need a script.

Don’t forget that you can do all sorts of really neat things with find, sed, awk if you use regular expressions.

A while back, I was asked to come up with a logrotation script.

This script would rotate all logs into a tarball and move it somewhere for archival purposes.

The problem was that I couldn’t just rotate today’s logs because it would be incomplete. I had to intelligently find a way to rotate the previous days logs (which would be complete) and then tar them up.

It turns out that you can do this with the “date” command in linux.


YESTERDAY=$(date -d "yesterday" '+%Y-%m-%d')
echo $YESTERDAY

This little snippet came in very handy and so I thought would share it.

The whole script is very small actually. Here it is. All you want to do is direct the output to a log file so you can review it later if need be. It’s chatty but you want chatty when you’re backing up and deleting log files.


#!/bin/bash
if [ $# -lt 3 ]; then
echo "USAGE: backup_logs.sh ORIGINAL FILE-PREFIX- DESTINATION"
exit
fi
# get command line variables
FILE=$2
ORIG=$1
DEST=$3
# get current time for log files.
NOW=$(date)
# get yesterdays date
YESTERDAY=$(date -d "yesterday" '+%Y-%m-%d')
# beginning entry in the log file
echo "$NOW Backing up $FILE from $ORIG for $YESTERDAY to $DEST/$FILE-$YESTERDAY.tgz"
echo "$NOW Command is /bin/tar -zcvf $DEST/$FILE$YESTERDAY.tgz $ORIG/$FILE$YESTERDAY*"
/bin/tar -zcvf $DEST/$FILE$YESTERDAY.tgz $ORIG/$FILE$YESTERDAY*
echo $NOW Done backing up logfiles to $DEST/$FILE-$YESTERDAY.tgz
echo "$NOW removing files that were backed up."
/bin/rm -vf $ORIG/$FILE$YESTERDAY*
echo "$NOW done removing backuped up files."


© 2007 wp | anoopdotnet | iKon Wordpress Theme by Windows Vista Administration | Powered by Wordpress