Author: Joe Barr
Now stop whimpering, you nattering noobies! This story is for you. Just remember, your mama’s not here to sysadmin for you. Today you’re going to learn how to do a few things at the notorious and fabled command line, things that can make your transition from Windows-weenie to street-elite Linux geek a little easier.
Feel the force, Luke
First, open a terminal window. For you recent converts from the Land of Monopoly Lock-in, a terminal window is similar to the infamous “C:>” prompt — you know, the interface Microsoft tried to hide all those years, so you wouldn’t think Windows was just a GUI running atop DOS. (Which it was, of course.) Now stand back and smell the octane. You’ve just opened the hood.
But before we venture any further into the heartland of Linux, I want to make sure you’re familiar with two similar tools: man
and info
.
They tell you what various programs and features do, and usually provide examples of their use. For example, if you wanted to learn what grep
does, you would enter:
man grep
Who was that masked command?
Another useful tool for survival on the command line is called alias
. It allows you to rename (sometimes long and intricate) commands to something a little easier to remember and type. How cool is that? You get to name commands whatever you like.
If it’s hard for you to remember all the parameters to use to get a directory listing in a directory that shows all files and their permissions, ownership, and size, just use alias to call it, um, something familiar. How about “dir”?
In Red Hat, the place to keep your aliases is a file called .bashrc. It lives in your home directory. Use whatever editor you like (gedit or KEdit will work just fine, as will Vi and Emacs and a whole bunch of others) to open the file and enter the following line:
alias dir='ls -al';
Then save the file.
Before you can use your new command, you have to log out and log back in. Or if you want to be really sneaky, you can just enter . .bashrc
at the command line in your home directory and use it immediately.
Number, please
When I first started using Linux, before Evolution and other apps I could use for keeping a list of phone numbers appeared, I started a simple text file with a name, address, and phone number all on one line. Actually I still use it. It’s a lot quicker to find a phone number I need with that text file and an alias I’ve created using two simple commands: cat
and grep
.
Let’s start by entering man cat
in the terminal window, or what we Linux long-timers refer to as a console. As man
explains, cat
is a tool that allows you to do neat things, like con_cat_enate two files into one. It’s also useful for pumping the contents of one file into one end of a pipe for input to another program, such as grep
. That’s how I use it when I want a phone number.
If you enter cat filename
at the console, the contents of filename
will scroll down the display. So if the name of my text file is phones.txt
, the command cat phone.txt
will display the file in the console.
To pass that data to grep
instead of dumping it on the console, I use the pipe operator. It’s that vertical bar above the on most keyboards. It looks like this: “|”. To pipe the phone numbers to grep, I enter:
cat phones.txt | grep
As shown, it wouldn’t do anything except give me an error explaining that grep
was expecting an argument or two to tell it what to do, but you get the idea.
Of all the CLI tools you’ll ever use, grep
is one of the most powerful. When someone says to grep
for something, they are saying to look every way imaginable to find the desired object. And that’s about how many ways you can tell grep
how to search or what to do based on the results of the search: every way imaginable.
When I want to find the phone number for my plumber, I want his number right away. I want it even if I search for “steve,” but I entered it in the text file as “Steve.” So I need to tell grep
not to bother matching the case; make it case-insensitive. The argument to do that is -i
, so the entire command I would use would be:
cat phones.txt | grep -i steve
That’s a lot to type when you’re in a hurry, so I created an alias
called phone to type it for me — all of it but the name, that is. The line defining the alias in my .bashrc file looks like this:
alias phone='cat /home/warthawg/phones.txt | grep -i'
As you have probably guessed, the shell gets the name “steve” from phone
command line and adds it to the end of the aliased command.
So this:
phone steve
becomes this:
cat /home/warthawg/phones.txt | grep -i steve
If there are multiple Steves (or steves) in the file, they will all show up, but that’s no problem. Using grep
with cat
makes searches easier and quicker than with a GUI. Even under an alias, a cat
is faster than a mouse.
If you’re not a noobie to grep
and the CLI, you’ve probably noticed that my solution is not optimal. The same results can be obtained by using grep
alone. The command grep -i steve phones.txt
does the same thing. But the alias is still less to type and therefore worth having.
Ok, that’s it for today. I hope it’s enough to get you newbies out of your overstuffed GUIs for a bit so that you can continue to learn about Linux and what makes it such a great choice for your daily desktop computing chores.
Joe Barr has been writing about personal computing for 10 years, and about Linux for five. His work has appeared in IBM Personal Systems Journal, LinuxGazette, LinuxWorld, Newsforge, phrack, SecurityFocus, LinuxJournal.com, and VARLinux.org. He is the founder of The Dweebspeak Primer, home of the official newsletter of the Linux Liberation Army, an organization in which he holds the honorary rank of Corporal-for-life. No IBM money-trees were killed in the preparation of this bio.