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 VP of engineering operations at Final, Inc., the author of a number of books including DevOps Troubleshooting and The Official Ubuntu Server Book, and is a columnist for Linux Journal. Follow him @kylerankin.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- SUSE LLC's SUSE Manager
- My +1 Sword of Productivity
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Managing Linux Using Puppet
- Non-Linux FOSS: Caffeine!
- Tech Tip: Really Simple HTTP Server with Python
- Doing for User Space What We Did for Kernel Space
- Parsing an RSS News Feed with a Bash Script
- Rogue Wave Software's Zend Server
- SuperTuxKart 0.9.2 Released
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide