Hack and / - Lightning Hacks—the Command Next Door
Every year or so, I like to write a column I title “Lightning Hacks”. This column is inspired by the lightning talks you encounter at most conferences. In a lightning talk, instead of having one speaker give a 60-minute presentation, multiple speakers give short 5–10-minute presentations. By the end of a lightning talk, you end up hearing about all sorts of cool topics that wouldn't have gotten their own time slot. In this column, I get a chance to talk about a few cool “hacks” I've run across that by themselves wouldn't fill an entire column.
In past Lightning Hacks columns, I've covered a wide variety of different topics, and in the previous edition, I focused strictly on hacks with the ssh command. In this column, however, I showcase a few interesting tricks with commands you might use every day. It's easy to take your favorite programs for granted, but I've found that no matter how long I use Linux, it seems I'm always picking up new time-saving tips, and a Lightning Hacks column is as good as place as any to list a few of my favorites.
It isn't news to frequent readers of this column that I edit all of my text files with vim. I've been using vim for so long, my muscle memory for the Esc key is second nature. When you press the Esc key in vim, you return back to the default navigation mode from whatever mode you were in. When I'm at all unsure where I am or what mode I'm in, my brain automatically has my hand bang on that Esc key like I'm a champion Hungry Hungry Hippos player. Of course, on a modern keyboard, the Esc key is quite a way from home row, and what's worse, on some keyboards (like on ThinkPads), the Esc key is in a different place, so someone like me hits F1 half the time. It turns out, however, that Ctrl-[ functions just like Esc in vim (and other programs that accept vi-style syntax, like vimperator). Both of those keys are just a pinkie-reach away and keep most of your fingers on home row. I have to say though, old habits die hard. I had to put a post-it note on my monitor that read “Ctrl-[” for a few weeks before my fingers started to catch the hint. It takes only a few weeks for the habit to form though, and before you know it, you'll be shaving that extra millisecond off your typing time.
Now that I've removed yet another function from the poor Esc key, let's give it something else to do. When I first set up my N900 with terminal sessions, I ran into a problem a lot of portable computer users face: half the special keys aren't on the tiny keyboard. In my case, this presented quite a problem, because I am a heavy Irssi user and my keyboard had no way to press Alt even in the special symbols menu. Without the Alt key, it was a pain to switch between different Irssi windows. I wasn't sure what to do until I discovered that in most terminal sessions, the Esc key can substitute for Alt. It turned out my terminal emulator had an Esc touchscreen key-mapped, so from that point on, I just pressed Esc whenever I needed Alt. It also turns out this works the same way on all of my laptop terminal sessions.
This is a quick-and-simple tip that I can't believe took me so long to discover. Quite often, you will be in a situation where you want to open a file to a specific line number. A few good examples include when you re-kickstart a server, go to ssh in to it and realize the host keys have changed. The error message lists the line number with the conflicting key, so it is easy to go in and erase it. Another common example occurs when you are working on a script or configuration file and see a reference to a syntax error on a specific line.
Back in the day, when I wanted to go to a specific line in vim, I would open the file and press :<number><Enter> to go to the line, but if you want to save a step, just add +<number> to the command line as an argument when you execute vim. For instance, if I want to jump ahead to line 27 in my ~/.ssh/known_hosts file, I would type:
vim ~/.ssh/known_hosts +27
Grep is one of the crucial tools you should master if you want to spend any time on the command line. One of the most common ways I use grep is to check whether a certain process is running and see its process ID. For instance, if I want to check for the process ID of my cron dæmon, I might type:
$ ps -ef | grep cron root 1215 1 0 Feb23 ? 00:00:00 cron greenfly 1252 1976 0 20:57 pts/0 00:00:00 grep cron
In this example, cron has a process ID of 1215. The annoying thing though is that because I had cron in my grep command, it also showed up in the output. This means every time I run this command, I have to be sure to ignore that line in the output. Of course, I also could use grep to filter itself:
$ ps -ef | grep cron | grep -v grep root 1215 1 0 Feb23 ? 00:00:00 cron
That works; however, it's unnecessary. If I enclose one of the characters in my first grep filter in brackets, it does the filtering for me:
$ ps -ef | grep [c]ron root 1215 1 0 Feb23 ? 00:00:00 cron
This works because when I enclose c in brackets, it turns it into a character class that contains only one character, c. However, the grep command itself shows up in the process list with those brackets, so it doesn't match my [c]ron pattern anymore.
Kyle Rankin is a systems architect; and the author of DevOps Troubleshooting, The Official Ubuntu Server Book, Knoppix Hacks, Knoppix Pocket Reference, Linux Multimedia Hacks, and Ubuntu Hacks.
|Speed Up Your Web Site with Varnish||Jun 19, 2013|
|Non-Linux FOSS: libnotify, OS X Style||Jun 18, 2013|
|Containers—Not Virtual Machines—Are the Future Cloud||Jun 17, 2013|
|Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer||Jun 12, 2013|
|Weechat, Irssi's Little Brother||Jun 11, 2013|
|One Tail Just Isn't Enough||Jun 07, 2013|
- Speed Up Your Web Site with Varnish
- Containers—Not Virtual Machines—Are the Future Cloud
- Linux Systems Administrator
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Senior Perl Developer
- Technical Support Rep
- Non-Linux FOSS: libnotify, OS X Style
- UX Designer
- RSS Feeds
- It is quiet helping
32 min ago
49 min 4 sec ago
- Reachli - Amplifying your
2 hours 5 min ago
2 hours 54 min ago
- good point!
2 hours 57 min ago
- Varnish works!
3 hours 6 min ago
- Reply to comment | Linux Journal
3 hours 35 min ago
- Reply to comment | Linux Journal
6 hours 1 min ago
- Reply to comment | Linux Journal
10 hours 1 min ago
- Yeah, user namespaces are
11 hours 17 min ago
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?