Tuesday, 11 October 2011

Find & Replace across multiple files in linux


I was trying to find a solution todo a find & replace across multiple files which was purely command line based. There are plenty of scripts out there which will accomplish this but I needed a single line command. After some google searches and some experimentation I came up with this snippet.

    find . -name "*.php" -print | xargs sed -i 's/foo/bar/g'

It looks a bit complicated but its quite simple. There are three components to the command:

    find . -name "*.php" -print – Find all files (recursively) which has “.php” in the file and print them out. This will give you output like this:

    ./file.php
    ./includes/test.php
    ./classes/class.php

    xargs- This command is used when you want to pass a lot of arguments to one command. xargs will combine the single line output of find and run commands with multiple
    arguments, multiple times if necessary to avoid the max chars per line limit. In this case we combine xargs with sed
    sed -i 's/foo/bar/g' – aka Stream Editor is a tool which should be in every sys admin’s toolkit.  In this case every occurence of “foor” is replaced by “bar” in all the files found using the “find” command. Sed simply parses input and applies certain text transformations to it. There’s a lot to say about sed, you can find more at this tutorial.

This pretty much covers the core of the find & replace command. You could also open up a particular folder in an IDE and use it’s find and replace feature. But find + sed is quite fast and powerful.

Resources:

    find – http://dmiessler.com/study/find/
    sed – http://www.grymoire.com/Unix/Sed.html

Tuesday, 4 October 2011

drbd split brain recovery




I was trying to reboot redhat cluster for backups and gfs performance testing but while restarting I got spilt brain drbd messages in /var/log/messages and the following is the output of /proc/drbd: { we have primary/primary drbd config}




Primary:
version: 8.3.0 (api:88/proto:86-89)
GIT-hash: 9ba8b93e24d842f0dd3fb1f9b90e8348ddb95829 build by
argus@docidtxt03, 2009-03-09 18:04:20
 0: cs:StandAlone ro:Primary/Unknown ds:UpToDate/DUnknown   r---
    ns:0 nr:0 dw:94815205 dr:14861358 al:19160 bm:18136 lo:0 pe:0 ua:0
ap:0 ep:1 wo:b oos:18911216


Secondary:
version: 8.3.0 (api:88/proto:86-89)
GIT-hash: 9ba8b93e24d842f0dd3fb1f9b90e8348ddb95829 build by
argus@docidtxt04, 2009-03-04 16:23:58
 0: cs:WFConnection ro:Secondary/Unknown ds:UpToDate/DUnknown A r---
    ns:0 nr:0 dw:0 dr:0 al:0 bm:14 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:55544 




Following commands are used to recover from the situation:::


drbdadm -- --discard-my-data connect all (on node with "bad" data)
drbdadm connect all (on node with "good" data)




And now the resources are connected. 


Then  verify the /proc/drbd status and if it isn't connecting check iptables and stop or allow the ports which are being used by drbd and then follow the commands again. or otherwise demote one of the node to secondary as 


drbdadm secondary r0 (on bad data node), after the successfull sync do




drbdadm primary r0 (on both nodes)

Copy/paste text with mouse in Linux

Q. How do I copy and paste text or output from a shell prompt? How do I copy text from one terminal window and paste it on diffrent server window using mouse?

A. You can easily copy and paste between multiple shell prompts or the terminals using nothing but your mouse only. This is useful to transfer configuration lines from one terminal to another all opened over ssh session.
Most Linux distros are configured the click of a middle mouse button as paste operation. All you have to do is select text, move your most to another window and hit the middle mouse button. If you have a scroll wheel, just press on it to past the text. If you see only two buttons, just hit both button simultaneously i.e. you can emulate a third, "middle" button by pressing both mouse buttons simultaneously.

Optional: Linux Mouse configuration

Usually you can reconfigure mouse by editing /etc/X11/xorg.conf file or your distributions mouse configuration tool. You can also use xmodmap utility for modifying keymaps and mouse pointer button mappings in X. Here is my sample PS2 wheel Mouse entry:
Section "InputDevice"
         Identifier      "Configured Mouse"
         Driver          "mouse"
         Option          "CorePointer"
         Option          "Device"        "/dev/input/mice"
         Option          "Protocol"      "ImPS/2"
         Option          "ZAxisMapping"  "4 5"
EndSectio

Further readings:

  • man pages xorg, xmodmap, xsetpointer etc

Reference: