Archive for the ‘Unix’ Category

The power of less

Monday, November 30th, 2009

Goal: I want to find some text called mytext within all php files in a directory. I also want to display all the matches and highlight them.
If this is what you want try this:

grep -Hin "mytext" *.php | less +/mytext

find php syntax errors

Monday, November 30th, 2009

To find syntax errors in all php pages within a directory called /var/www/html, use the following command:

find /var/www/html -name '*.php' -type f -exec php -l {} \;

Sphinx search

Sunday, November 15th, 2009

Have a big website with millions of records? Is MySQL full-text search letting you down? Sphinx search http://sphinxsearch.com by Andrew Aksyonoff is probably gonna solve your problems. It is an awesome product (and yes it’s open source) with less than a second search results on a data set with millions of records.
I had the opportunity to author a sphinx based search engine for vBulletin. We use it for our huge forums at Internet Brands. A demo can be viewed here:

use watch command to WATCH :)

Sunday, November 15th, 2009

From the man watch page: “The -d or --differences flag will highlight the differences between successive updates.”
Try

watch -d -n 3 "free -m"

The above command would highlight the differences in memory consumption every 3 seconds.

slicehost vs rackspacecloud

Thursday, November 5th, 2009

I tried a 256 MB slice (that’s what they call a cloud) with Slicehost, a rackspace company, and the guys are AWESOME! Shortly after that, rackspace came up with rackspacecloud.com (they had slicehost, why would they? who cares :) ), and I have been a big big fan of rackspace. So, I decided to give rackspacecloud.com a shot. Same memory, same disk space, same everything (well I haven’t really needed rackspacecloud.com support that often) and close to half the price.

256 MB on slicehost = $20 vs 256 MB on rackspacecloud.com = $11 approximately (for me atleast)

Doesn’t quite make sense to me :)

How to duplicate a file with the current date and time

Tuesday, September 29th, 2009

To duplicate (backup) a file as filename.unixtimestamp use

cp myfilename{,.`date +%s`}

You can change the date format as per your preference. Below would duplicate it as filename.yyyymmdd

cp myfilename{,.`date +%Y%m%d`}

To find all files modified after a certain date and time

Tuesday, September 29th, 2009

To find all files (regular files) modified after 12:00 AM today use

touch -t `date +%Y%m%d0000` /tmp/mintimestamp
find /path/to/search/for -type f -newer /tmp/mintimestamp
rm /tmp/mintimestamp

The trick is to timestamp /tmp/mintimestamp with the timestamp that we want to compare against.

So you would use the touch command below in the above set of commands to find all files modified after 12:00 AM on Jan 1, 2008:

touch -t `date 200801010000` /tmp/mintimestamp

Migrating website content is quick and easy

Tuesday, September 29th, 2009

Check out my post on migrating website content here: http://priteshjshah.com/?page_id=42

find everything except something using grep

Monday, September 28th, 2009

the -v switch is used to return negative results. So, to find everything which does not contain “findme” use

cat filename | grep -v "findme"

Find and replace text in all html files using PERL

Monday, September 28th, 2009

To find and replace “findme” with “replacewithme” in all HTML files in a directory /var/www/html use

find /var/www/html -name '*.html' -print0 |xargs -0 perl -p -i -e 's/findme/replacewithme/g'

To find and replace “findme” with “replacewithme” in all HTML files in a directory /var/www/html and to backup the original files as let’s say filename.unixtimestamp use

find /var/www/html -name '*.html' -print0 |xargs -0 perl -p -i.`date +%s` -e 's/findme/replacewithme/g'