Scared of the Linux Command Line? We Walk You Through it.

Linux's graphical user interface is improving, but there comes a time when we all must descend into that scary and obscure world of the Linux command line. Though communicating with your computer via keyboard rather than mouse can be a bit intimidating at first, most people find that picking up just a few simple commands can go a long way toward making them much more efficient and happy Linuxniks.

Linux’s graphical user interface is improving, but there comes a time when we all must descend into that scary and obscure world of the Linux command line. Though communicating with your computer via keyboard rather than mouse can be a bit intimidating at first, most people find that picking up just a few simple commands can go a long way toward making them much more efficient and happy Linuxniks.

Have you ever asked an older nerd about the first computer they ever used? For me, this was a huge IBM system ensconced in a sacred glass-walled air-conditioned room. To run a program you had to leave a stack of punched cards and come back the next day to pick up the results. The trick to running two or three programs at a time was to stay really late and maybe buy the computer operator a pizza.

Fast forward about 10 years. The latest model of IBM’s mainframe is out, and now when you go to the computer center you get to sit at a terminal and run programs yourself. While the hardware has improved and you can now play with the computer via a keyboard and screen rather than punch cards, the heart of that big mainframe is still pretty much the same as it was a decade earlier.

This is the world into which Unix was born. There were no graphical user interfaces (GUIs). Computers were big and tended to be very expensive to run, and all operations were done with a command line. Unix was a major innovation and changed how people tended to think about operating systems. One thing that didn’t change with the advent of Unix, however, was the command line. At that point in the history of computing, there was simply no other way that a person could talk to a computer. Graphical interfaces simply hadn’t been invented yet.

Everything changed with the advent of the graphical user interface. The GUI allowed users to communicate with machines in a visual environment, using a mouse and keyboard to manipulate visual representations on the screen. The X Window System and Microsoft Windows are two GUI systems that are in common use today, and there are many computer users in the world who have never seen a command line. But both the GUI and the command line are really just different ways for the user to tell the computer to do its job.

Anatomy of a Command

The easiest way to get into a command line is to launch what is called an X-terminal. The icon to make this happen will probably look like a computer screen, and it will most likely be called something like “Terminal” or “Terminal emulation,” depending on your version of Linux. Clicking on this icon will launch the X-terminal; this is the command line. Using it is a lot like running the “MS-DOS prompt” in Windows, which gives you an interface to Windows’ command-line-driven DOS interface.

When you type a command on the command line, what you’re actually doing is telling the computer to execute a particular program. For example, if you type pwd on the command line and hit “Enter,” what you’re doing is executing the program named pwd. While this is a very simple program (all it does is print the name of the directory you’re in), it is still an executable program.

Sometimes you have to feed a command some extra information before it can properly execute. Each part of this extra information is referred to as a command-line argument. So, if you see something like ls -a foo, the command is ls (which lists the contents of a directory) and the arguments are -a (which tells ls to list all types of files in the directory) and foo (which is the name of the directory you want to see listed).

Just like Microsoft’s DOS and Windows, Linux keeps files in directories. If a file is like a sheet of paper in your office, then a directory is like a file folder. When you launch your X-terminal you will be put into a default directory. The directory you’re in is called your “working directory.” You can switch the directory you’re “working” in with the cd command.

Wild, Wild, Wildcards

Now that you know how to work the command line, what do you do when you want to remove a whole group of files — everything ending in .tmp, for example? Or say you want to list only the names of files starting with a in a certain directory.

This is where the command line becomes very powerful, because it lets you use those nifty little creatures called wildcards. They are the secret to becoming a power Linux user. Wildcards are your way of saying, “I don’t really care what goes here.”

When used in the command line, the asterisk (*) wildcard represents any number (including zero) of characters, while the question mark represents one character. So, if you typed ls *, Linux would list all the files in your current working directory, since * means any combination of characters that Linux can find.

[bob@localhost foo]$ ls *
ad ax axe file1.txt file2.abw file3.html

On the other hand, if you typed ls a?, only files with two-letter names starting with the letter “a” would be listed.

[bob@localhost foo]$ ls a?
ad ax

These may look similar to wildcards in DOS, but they are better. First of all, in Linux the period can be represented by a wildcard. This isn’t the case in DOS, where you need to say *.* to list all files. With Linux, the * alone will do.

Also, unlike DOS, using more than one wildcard in a single argument doesn’t confuse Linux. So where a*b*c might confuse DOS, Linux will understand that you are saying a followed by 0 or more characters, followed by b, then 0 or more characters, and then c.

Well, this has been a bit of a whirlwind tour of command-line basics, but we’ve covered many of the commands that you should need on a daily basis. All of the commands we’ve discussed have options that weren’t mentioned here, so you should take a look at the man pages for more information and details.

You should also take the time to do some experimentation, although I strongly recommend that you do not do so when logged in as the root user on your system. Experiments that run amok can be very serious when you’re logged in as root. Also, be cautious when using commands such as cp (copy), mv (move), or rm (remove), as these can overwrite or delete files.

Just the Basics, Please

So what commands will you need to survive in Linux day-to-day? Here’s a quick rundown on some of the basic commands that you’ll almost certainly need. If you’ve ever used the DOS command line, you’ll notice that these Linux commands have similar counterparts in DOS.

Copy: cp file1 file2 -This command will copy file1 to file2. If file1 doesn’t exist then you get an error. If file2 already exists then you may be asked if you want to replace it. Be careful. Depending on how your system is set up you may not be asked. file2 will now be an exact copy of file1. This is similar to the DOS copy command.

Concatenate: cat file1 -You can use the cat command to print the contents of a text file onto your screen. Be careful to use cat only with text files. If you use it on a WordPerfect file, for example, you’ll see a whole mess of incomprehensible formatting information. This is similar to the DOS type command.

Move: mv file1 file2 - Here mv will rename file1 to be called file2. If file2 already exists then it will be replaced by file1. Be careful when you use this command because if you choose to move to a filename that’s already in use, you might lose the contents of that file. This is similar to the DOS rename command.

Remove: rm file1 file2 file3 – Like the DOS del command, rm deletes the files you specify. Be careful with this one; once you’ve rmed something, you can’t get it back.

Print Working Directory: pwd – This command lets you know the full name of the directory you happen to be working in.

Change Directory: cd directoryname - The CD command will switch the directory you are working in. It is just like the DOS command of the same name. To move up in the directory chain, type the command cd .. — this will move you up to the directory that your working directory lives in.

More: more file1 file2 file3 – If you want to quickly glance through a file while you’re in the command line, more is the way to go. It will print the file or files you specify on your screen, one screenful at a time. Once more is running, you can press the space key to advance to the next screenful, the “Enter” key to advance one line, b to back up a screenful, or q to exit more altogether. Like cat, you’ll want to use this one with text files only. More is just like the DOS more command.

ls: The King of Commands

The ls, or List Directory command, is without a doubt the command you’ll get the most mileage out of on your Linux system. If you simply type ls, the command will print out the names of the files in the directory where you’re currently working (this is called your “working directory”).

[bob@localhost linuxmag]$ ls
business/ foo/ style
copy/ photos/ website/
editcalendar/ stories/ writer.contract

Here, ls lists the contents of the linuxmag directory.

[bob@localhost linuxmag]$ ls -a foo
./ ../ file1.txt file2.abw file.html

The command ls -a directoryname will additionally list the hidden contents of a particular directory. Here it’s listing the contents of the directory foo.

Linux is pretty flexible when it comes to naming files. Filenames can be up to 256 characters long and are case-sensitive. Special characters, like $, #, and ~ are generally allowed in filenames, but some special characters (* and ? in particular) have specific meanings at certain times. So it’s best to avoid them when naming files.

One important character is the dot (.). If a filename starts with a dot, then it is a hidden file, and a simple ls command won’t show it to you. That’s why I chose to use the -a operator to list the foo directory just now. -a tells Linux to show you all files, including those that start with a dot.

Notice that, when using the ls -a command, there are two entries at the top of the list — a single dot (.) and a double dot (..). The single dot represents the current directory, and the double dot represents the directory immediately above the current directory (this is called the parent directory). You can use these directory representations on the command line. For example, typing cd .. will move you up to your working directory’s parent directory.

Hal Moroff has been developing Unix systems and applications for 20 plus years. He’s new enough to Linux that he’s finding new things every day. He can be reached at halm@ieee.org.

Comments are closed.