Monthly Archives

June 2011

Recursive Global Search and Replace in Linux

I was working on a very old project of mine, a MUD called DooMMUD (based off of the popular id Software series of games) and just for shits n giggles, I thought I would move the codebase over to something newer and more stable like the Circle-based tbaMUD codebase just to see if I could get it up and running again.

At some point I needed to change some Variables and Classes (structs really) over the entire codebase (e.g. coins to credits)

No way was I going to bother editing all the files (if I could even find them all), I would almost definitely miss something.

So I used a combination of grep and xargs & sed to recursively go through every directory in the tbaMUD structure and replace every string or replace every phrase that I needed changed.

Behold the majesty of my one line command:

grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'

We pipe the recursive results from grep into xargs & sed which takes those results and run a global search and replace within each instance of string1 with string2.


I remember when I was 8 years old, my father had made me an account on his Sun Microsystem Workstation 3/80 running some later version of SunOS. I used KornShell over a matter of person preference (I don’t think BASH was even around back then) and every day I would look into /usr/bin or /bin to look for new command, run man page on it and tinker around with it.

My father said it best when he said “Its like having a Christmas Present every single day”. The man was right.

Bad Cable Woes Force me to Force 10Mbps Full Duplex

This seems to be a rare problem for most people, and in fact should be, however, if you’ve ever had bad CAT (5/5e/6/whatever) cables [because you made them yourself maybe], then theres a possibility you’ve come across a strange scenario where your NIC cannot access the network (or establish a good connection with your Switch/Hub or Router) and forcibly setting it down to 10Mbps is the only way to get it to work.

So, if you have the same problem, there’s a ridiculously easy solution in Linux. Years ago, we had to use mii-tool. mii-tool, however, is now deprecated and could only handle 100baseT4, 100baseTx-FD, 100baseTx-HD, 10baseT-FD or 10baseT-HD.

Here comes in our hero of the day namely ethtool.

Now, I was at my ranch where I did all of the wiring for the network cables. That included the RJ-45 wall jackets, as well as the plastic connectors for the basement where all of the cables eventually lead down to multiple / separate switches and routers (yes, multiple routers, mini-networks, pain in the ass – i know)

Well there’s one room thats always caused problems from the get-go. At first I thought it was the socket, then I thought it was the plastic connector. I’ve changed both many times, different computers would work, some would not, sometimes it’d work when I would boot with one OS, often it wouldn’t with another.

Finally, I figured out that the pattern had to do with whether or not the NIC was set to 10Mbps, as opposed to 100Mbps or 1000Mbps/1Gbps. Then it hit me, the cable must have be damaged IN SIDE the wall somewhere or in the floor.. either way way I have to make sure that the NIC is downgraded to 10Mbps and everyone works fairly well afterwards.

So, here’s the magical command everyone insisted on me using:

ethtool -s eth0 speed 10 duplex full autoneg on

Sadly, this command would not work!

First things first, I am using the RTL6169 driver for my RTL6168/6111b NIC – it doesn’t support being told to renegotiate, I realized, so then I thought I would follow up with these commands:

ethtool -s eth0 speed 10 duplex full
ethtool -r eth0

…to manually re-negotiate after setting the speed. However, I found that the first step would not even work. Basically, in no way could I get ethtool to downgrade or change the speed setting. It just refuses to do make it change at all.

This means I had to go back and use the so-called “deprecated” mii-tools. On Ubuntu 11.04 I was able to install it using the net-tools package. Alternatively you can download the source to mii-tools (its a very harmless easy compile / install with the make command).

So I fired up the good ‘ol tool using this exact command:

mii-tool -F 10baseT-FD eth0

Voila! Presto! Banzai! It works! It.. just.. works.. ethtool could not work or ethtool would not work, but mii-tool worked just fine.

Obviously, I did not want to run the command every time I rebooted, so I edited the file /etc/network/interfaces and added this line at the bottom:

pre-up /sbin/mii-tool -F 10baseT-FD eth0

Some people recommend making a script or running it from rc.d or init.d, etc. That’s just bad system administration, IMHO. There is no better place than the interfaces file, its there (and the use of the pre-up command) for a reason just like this one for startup NIC operations.

So the moral of the story? Just because a tool is old does not mean its usefulness has waned!