« March 2005 | Main | June 2005 »

April 25, 2005

Shell tricks and tips

Recently, I started developing exclusively on a remote Linux box. Thanks to Andy I've picked up quite a few shell tricks that have been exceedingly useful. I use zsh at work, but I'm pretty sure they all work on bash as well. Recently, when talking with some friends who are Linux veterans, I was surprised to learn that some of these commands were new to them. Of course, I also learned a new trick or two from them. Here's my list, am I missing any gems?

  1. Tab completion of directory/file names/environment variables - I'm embarrased to say that when I first started to teach myself Linux, it took me about 6 months to learn about tab completion. It was even longer until I realized that tab also completed environment variables. I mention it here because I'm sure I'm not the only one. ;-)
  2. CTRL-W delete word
  3. CTRL-E go to end of line
  4. CRTL-A go to beginning of line
  5. CTRL-R recent command completion - This is one command that wasn't known by too many people. It allows you to search quickly through your command history for previously typed commands. It helps to have a large command history: export HISTSIZE=1000.
  6. CTRL-U Discard line, this works well when you think you've mistyped a password and need to start over.
  7. CTRL-K cut line from cursor
  8. CTRL-Y paste line in buffer
  9. !! (the last command) - I mostly use this when I forget to sudo a command For example:
    rm foo
    sudo !!
  10. !$ (the last argument of the last command)
    Let's say I'm tailing a log, but I want to see more than just the last few lines.
    tail -f /var/log/cron (quit tail)
    less !$

    or you just edited your .bashrc with vim and now want to source it
    vim ~/.bashrc (quit vim)
    source !$
  11. search for any files/directories recursively starting with the current directory and look for any occurance of foo in the name.
    find . -type f | grep foo
    OR
    find . -name "*foo*"
  12. xargs - Takes the output of the preceding command and appends them to the end of the following command. I think it's actually a bit more complicated than this, but the examples that follow do work.
    Here's a use of xargs with find to search within any files/directories recursively starting with the current directory and look for any occurance of foo *within* any of the files
    find . -type f | xargs grep foo
  13. Have you ever accidentally un-zipped a file into the wrong directory and wanted an easy way to undo. You can use xargs to remove the files by first listing the files in the archive, passing that list to xargs with rm (you have to be careful with this one):
    tar xzf foo.tgz
    tar tzf foo.tgz | xargs rm -rf
  14. How about wanting to get all files with foo in them, but you don't want the vim turd files. Note the escaped ~, this is done so that the shell doesn't first convert the ~ to /home/foo. Just remember that the shell will first parse the line, and *then* pass it to the binary. This type of issue still trips me up from time to time.
    find . -type f | xargs grep foo | grep -v \~
  15. pushd/popd - pushd will store the current directory in a stack. When you're ready to return to that directory at some later time, just type popd.
  16. cd - will take you to the previous directory (Thanks to Brain Pikkaart for this one).
  17. screen - (Thanks to Joel for first introducing me to screen ages ago.) I've got a linux development server at work that I connect to from my laptop. From man: "Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)." I've got screen running with 7 shells that I can toggle between. Following Andy's lead, I've setup screen with the following:

    0 - live server
    1 - backup server
    2 - vim
    3 - shell work on shared libraries
    4 - shell work on services
    5 - logs
    6 - mysql
    7 - scratch

    When I'm ready to leave, I just detach my screen session and go home. When I'm ready to start working again, I ssh into my development box, and (screen -r) will reattach my session and I'm back exactly where I left off.


Posted by mark at 3:32 PM | Comments (9)

April 12, 2005

RFID chips for Ellie and Hop Hop

Both of my kids have stuffed animals that they are quite fond of. That fondness borders on obsession that often causes tons of grief when we have to find Ellie (James' elephant), and Hop Hop (Emma's bunny) prior to putting our kids to bed at night. Every parent out there knows what I'm talking about. EVERY NIGHT I walk upstairs to start the bedtime ritual, which is hard enough to begin with, only to realize that Ellie or Hop Hop is missing. I then have to go BACK downstairs, often all the way to the basement, to find one or both of those infernal creatures.

I want to purchase a handful of RFID chips for use in my home. There are a ton of objects that might benefit from this technolgy:

I don't know much about RFID, and I've only heard about companies using it to track inventory (Cisco, Wal-Mart). I haven't heard anything about DIY home RFID kits. I'm betting that the RFID chips are fairly inexpensive, and that the technology to locate and read them is probably the financial bottleneck.

Regardless, even with this system in place, the next time I realize that Ellie is missing, I'll probably be wondering, "Now where did I put that damn RFID receiver."

Portable phones are pretty cheap. I suppose I could just duct tape a portable phone around Ellie's head. Next time she's lost, I can use the phone's buit in paging feature to find it. James could then make phone calls from bed. ;-)

As you get older you could increase the frequency of your RFID receiver, and size of the items you track.

Posted by mark at 1:46 PM | Comments (3)

April 8, 2005

Linux is cool

I just ran uptime on my main intranet server at work. 465 days without a reboot. That's remarkable. Back when I administered Windows servers, I would be lucky if I went 465 hours without a reboot.
7:54am up 465 days, 17:08, 2 users, load average: 0.11, 0.25, 0.25

Posted by mark at 10:52 AM | Comments (1)

April 6, 2005

Almost Famous

It just so happens that I went to high school with Jason Varitek, the catcher for Boston Red Sox. It also just so happens that I'm right next to him in my senior yearbook.

I didn't know Jason, although we did speak for a few minutes as they lined us up for graduation -- too bad I didn't ask for his autograph. I really enjoyed baseball, and I had played baseball my entire life up until transferring to Lake Brantley half-way through my junior year. I didn't even try out, although I often wonder if I could have made the team. Since we'll never know, I like to assume that I would have had a decent shot. ;-) It most likely was a false assumption since we had something like 3500 students, and I'm fairly certain that Lake Brantley High School in Altamonte Springs, FL was voted the #1 baseball team in the country by USA Today my senior year.

Back in 1997 I got a random call from a private investigator asking me questions about Jason and whether or not I knew him, and what I thought of him. I wasn't too helpful, and he wouldn't tell me why he cared, but I can only assume that this was the Boston Red Sox doing a thorough background check on him before signing. I pulled this from the 1997 career highlights.

"Acq. by Red Sox along w/RHP Derek Lowe from Sea. for RHP Heathcliff Slocumb July 31...Reported to AAA Pawt. Aug. 1"

Who knows? But the timing's about right.

I figure my greatest opportunity for national exposure is 20 years from now when some TV series does a "Where are they now?" or "Before the fame and fortune" expose on Jason Varitek. At some point, they're bound to zoom in on his high school yearbook picture, and for a fraction of a second I'll be able to say, "Hey, that's me!"

Posted by mark at 2:07 PM | Comments (2)