ADAM'S WEB PRESENCE

9 December 2008

Linux is amazingly useful

Filed under: General — adam @ 4:04 pm

It’s incredible the things you can do in Linux. Here’s two amazing things I’ve done today:

Amazing thing #1

I had a corrupted disk and I wanted to run some recovery tools on it but to be safe, I wanted work with a copy of the corrupted disk. So I created a raw copy of the entire disk and piped it to a different machine which had enough storage:

cat /dev/hda1 | netcat -q 1 192.168.0.15 9998

That created a file of 1128GB in size – probably the single biggest file I’ve ever created. It took more than 12 hours to copy to the other machine!

Then I created a loopback device so the system could pretend the file was a real disk:

losetup /dev/loop0 /home/adam/recovery.raw

Then I ran xfs_repair on it and recovered all the files. In the old days I would have to physically install a second hard drive in the corrupted box – did I mention that I did all this remotely from off-site through an ssh session.

Amazing thing #2

I needed to find all the Makefiles for an older version of my projects and update them. This command searches through a bunch of folders for files which contain the string “1.6″ and loads them all up in a text editor for me. It sure beats manually going through dozens of folders and eyeballing every file.

find /usr/src -name Makefile | xargs grep -l "1.6" | xargs kate

3 Comments »

  1. Comment by Ben — 10 December 2008 @ 1:24 am

    At first I was wondering what you used to receive the disk’s contents…but then I realized…netcat! Linux is amazing.

    With some more code, you could use KDE’s DCOP to further automate amazing thing #2. I’ve been using it recently to auto reload a PDF I’m working on that KDPF for some reason doesn’t want to reload automatically when the file changes. Once kate is running, you could invoke DCOP to script kate to do certain things, if the interfaces are exposed. I just spent two minutes tab-completing $dcop kate… to see what’s available, and I could get kate to search, but not to seek to the match. hmm.

  2. Comment by Ben — 10 December 2008 @ 1:05 pm

    Apparently socat is the new king in town, surpassing netcat. I have still to grok socat in fullness, but it seems sweet.

    http://www.dest-unreach.org/socat/

  3. Comment by Tom — 24 December 2008 @ 6:39 am

    Aaaargh – pedant alert.

    All of those things can be done with almost any UNIX-like OS, including the amazing FreeBSD. Personally, I’d say FreeBSD is better, because it actually comes with netcat as part of the base OS :)

    As for xargs/find, it is truly awesome. I think spending a day learning and practising find/xargs combos is essential for the noobies. Hmm, I’ll just go add that to our onboarding plan…
    Expanding that find to work with files that have spaces in their paths/filenames is a little bit trickier, but a cool trick (made necessary due to grep -l not having a null separated output :)

    find /usr/src -type f -name ‘Makefile’ -print0 | xargs -0 grep -Fl ‘1.6′ | tr ‘\n’ ” | xargs -0 kate

    The only thing really wrong with that line now in my mind, is that ‘vim’ has been incorrectly spelled ‘kate’ :)

RSS feed for comments on this post. TrackBack URI

Leave a comment


Powered by WordPress