Wednesday, July 30, 2008

Top 10 Command Line Tools [Lifehacker Top 10]

 

When you need something done quickly, efficiently, and without any software overhead, the command line is where it's at. It was the first way humans told computers what to do, but as graphics became increasingly important, the command line, or terminal, became an insiders' secret weapon. But with the right commands and a little bit of know-how, anyone can get things done from a text-only interface. Let's take a look at 10 commands and tricks that make the terminal more accessible, and more powerful, on any system. Photo by blakepost.

Note: Mac OS X and Linux users have robust command line interfaces baked right into their systems. To get to them, head to Applications->Utilities->Terminal in Finder. It varies in Linux, depending on your distro and interface, but a "terminal" can usually be found in an "Accessories" or "Utilities" menu panel. Windows users are best served by installing and configuring Cygwin, a Unix emulator, which we've detailed in a three part series.

10. Customize your prompt
If you're going to spend any time at the terminal, or want to start doing so, it should be a welcoming place. To go beyond green or white on black, check out this Ask Lifehacker response, in which Gina runs through a few simple ways to change the colors, and the greeting message, on your prompt for Windows, Mac, or Linux systems.
9. Force an action with sudo !! ("bang bang")
You already know that prefixing a command with sudo makes your system execute it with superuser privileges. But when you forget to sudo, the !! or "bang bang" comes to the rescue. When you've perfectly crafted a long command that does exactly what you need, hit Enter, and d'oh—you don't have sufficient access privileges—you can sudo !! to repeat the last command with superuser privileges. It's the ultimate nerd triumph: "Oh, you didn't like that command? Well, then sudo !!"
8. Create whole directory trees with mkdir
When it comes to organizing music, pictures, documents, or other media, nested folders become a necessary annoyance—as in right-clicking, choosing "New Folder" and then naming and clicking through each of "The Beatles->White Album->Disc 1." It's far easier from the terminal, as the Codejacked blog points out:
mkdir The Beatles\White Album\Disc 1
Some terminal users have to add a \ before spaces, but you get the idea. If you're a Vista user who's just not down with Cygwin, you can still pull this off with the md tool in command line.
7. Filter huge lists with grep
Some terminal commands spit back a bit too much information, and that's where grep comes in. Need to manually kill a faltering Thunderbird? Punch in ps aux | grep bird, and you'll get back the specific number to kill. Need to know which files don't have your company name in them? grep -v DataCorp *.doc. Programmer Eric Wendelin explains grep more in-depth.
6. RTFM with man (and more)
Let's say a program, or web site, has just asked you to run a command to unlock or enable something, but you'd like to know just a little more before jumping in. Add man before the command (as in man ssh) and you'll get manual-style pages detailing how to use the command. Bit too much material to process? Try whatis for a brief description, --help after the command for basic usage, or any of these other command-line learning tools.
5. Manage processes with top
Most systems have a tool to view "tasks" or "running programs," but they usually hide the true guts of what your system's doing from you. The Hackszine blog points out that Mac and Linux users can harness the power of the built-in top command to track and kill runaway processes making your system unstable. There's also ps -aux for a single-screen, non-updating look at what's bugging your computer.
4. Master wget for powerful file-grabbing
The wget command has been around since before there was all that much stuff to actually yank from the net, but this extensible, multi-purpose tool has lots of great uses these days. You can mirror entire web sites locally, resume huge downloads on the flakiest of connections, download the same file every hour to keep tabs on a project, and do much, much more with wget. It's one of those elegantly simple tools that's only as powerful as your creativity.
3. Get way beyond system searching with find
Once again, programmer Eric Wendelin offers real-world examples of how powerful a command line tool like find can be in, well, finding files and directories that match the smallest criteria you can imagine. Want a list of every HTML file that references the hexidecimal color #FF0000 (red)? find can totally do that for you. As Wendelin points out, find, by itself, is about as convenient and powerful as a total-system searcher like Google Desktop or Quicksilver, but piped into and out of other tools like grep, it's a powerhouse. For a more pared-down look at some of find's powers, check out this tutorial at Debian/Ubuntu Tips & Tricks.
2. Set up powerful backups with rsync
You can spend a lot of money and time hunting down a perfect backup app that works with all your systems just the way you want. Or you can spend a few minutes learning the basics of rsync, the flexible, powerful command that makes one folder (on your system) look like another (where you back up). To put it simply, rsync is a cross-platform, completely free Time Machine, if you use it right. Luckily, Gina's already shown us how to do that.
1. See your most-used commands with history, make aliases for them
Once you're comfortable with the terminal and getting good use from it, you might notice some of the more useful commands require an astute memory and typo-free typing—unless you make them shorter and easier. Start off by copying and pasting this command (on one line):
history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort -r
It will return a ranked list of your most commonly-entered commands using your command history—and you can start creating aliases to shorten them and make them easy to remember. Or you could search through your recently-used commands with as-you-type results for quick-fire repeats.

While these 10 commands are generic and applicable on all systems with a Unix-like terminal, Mac OS X offers a few Mac-specific tools. Here are useful command line tricks for Mac users.

We're love to have some CLI fun around here, and we know our savvier readers have tons of cool terminal hacks and tricks that are new to us. So, please—share the knowledge and spread the wealth in the comments.

No comments:

Post a Comment

Total Pageviews

Popular Posts