HOW TO : Find files, search for content in them, replace the content

The title pretty much says it all :). Here is a quick  one liner, using multiple tools, to look for files in a directory, search for certain content in them and replace them with other content

[code]find -type f | xargs grep -l ORIGINAL_CONTENT | xargs perl -p -i -e ‘s/ORIGINAL_CONTENT/NEW_CONTENT/g’ [/code]

You can theoretically take out the grep (second command) and directly pipe the find output to perl and get the same outcome.

Going over list of the options used

find

  • “-type f” lists all objects of type file in the directory (and sub directories)

grep

  • “-l” lists the names of the files (with relative path) which have the text ORIGINAL_CONTENT in them

perl

  • “-p” forces perl to loop through requests. In this case files
  • “-e” tells perl that the next argument is a perl statement
  • “-i” tells perls to edit the file in place (i.e. no need for an output file)

HOW TO : Use word boundaries in Regular Expressions

If you are every looking to match strings as a whole word instead of literal strings using a regular express, Word Boundaries are your friend

For example if you are looking to match a string of 10 numbers in a log file, you can use

[code]grep -i ‘\b[0-9]\{10,\}\b’ –color -H -n FILE_NAME_TO_SEARCH [/code]

The \b is the option that tells grep to look for a word character.

More information about the option is available at http://www.regular-expressions.info/wordboundaries.html

Overheard : Comment about politics

Milwaukee mayor, Tom Barrett commenting about his political goal while speaking with Robert Siegel on all things considered

I will never be the rock star of the far right and I’ll never be the rock star of the far left. What I want to be is rock solid and create jobs in the state.

Entire interview can be found here http://www.npr.org/2012/05/31/154093267/political-battle-heats-up-as-wis-recall-election-nears

HOW TO : Use screen to multitask

Want your terminal to look like this? 

In addition to the coolness, factor it also helps you do (monitor) multiple things at  a time. In this screenshot

  • I am checking the resource utilization on my web server
  • Tailing the web server logs to look for errors
  • have a small console for me to run any commands

You can achieve this by using the nifty screen utility. Screen allows you to multiplex between multiple consoles.  So you can open one terminal and have multiple consoles on it. The commands for using screen are a bit hard to get used to. Here are the shortcuts I used to achieve the screen above

  1. Install the screen package
  2. Create a new screen session by running[code] screen [/code]
  3. Add a new screen console by executing[code] ctrl + a [/code]

    [code]c[/code]

  4. Split the screen by executing[code]ctrl + a [/code]

    [code] Shift + s [/code]

  5. Name the different consoled by executing[code]ctrl + a[/code]

    [code]Shift + a[/code]

As you might have figured out by now, “ctrl + a” puts you into screen command mode. You can get a list of all available options by executing

[code]ctrl +a[/code]

 

[code]?[/code]

Here’s a quick reference guide that has more details http://aperiodic.net/screen/quick_reference 

Have fun multitasking 🙂

Brilliant use of DNS

I was listening to this week’s edition of Steve Gibson’s Security Now podcast and Steve talked about a unique way of using DNS. His spintrite application uses DNS to check for the latest version of the application. Most applications use http to check version information. This might pose a problem in environments with proxy servers. DNS traffic on the other hand is generally allowed in most environments. He says his application does a DNS lookup for something like application.version.grc.com and the “IP” address that is returned denotes the major and minor versions of the code. And depending on the response, the application will prompt with a “need to update” message.

Brilliant!!!

Here’s a more technical post way back from 2006 by Jan-Piet Mens on the same subject

http://jpmens.net/pages/checking-current-application-or-data-versions-using-dns/