The Linux Command Line, Revisited

The Linux command line. It strikes terror (or, at least, queasiness) in some. It’s arcane, it’s not point-and-click, it just doesn’t look good. But as a previous TechTip explained, if you’re running Linux on your desktop computer or laptop computer, a knowledge of the command line can make your life much easier.

While the previous TechTip explained the basics of the command line, this one looks at several useful commands and functions that you can use in a terminal window.

Viewing running processes

Like any other operating system, Linux has a number of system processes running in the background. These are in addition to whatever applications that you may be running. Using the ps command, you can view a list of the applications and processes that are running on your system.

When you type ps, you’ll probably only get a list of two or three processes. To view the process and applications that are running during your session, type ps -u <username>, where <username> is the name that you use to log into Linux — for example, ps -u scott.

PS_IMNotice that in addition to the name of each application or process, they have a number identifying them. This is called a pid (short for process ID). In the accompanying screenshot, notice that the application Thunar (a file manager) has the pid 5214.

I bet you’re thinking that this is all interesting, but how is viewing processes useful? Keep reading …

Killing processes

No matter how well coded an application is, it may hang. When it does, you can shut it down completely using the kill command.

So, let’s say the Thunar file manager has hung. Actually, it did hang on me a week ago. To kill it, first run ps -u <username> to find its the pid. In the screenshot above, the pid for Thunar is 5214. To kill it, enter the command kill 5214.

Of course, the kill command can be quite dangerous. You need to make sure that you specify the right pid, or else you’ll stop and application or process that could be important. With an application, you can lose your data. You won’t get a warning to save a document or file; the application just shuts down.

Viewing text files with less

A friend of mine jokes that Linux isn’t a set of programs but just a bunch of text files that simulate an operating system. That’s a bit far-fetched, but Linux is known for the large number of text files that it does employ. Files like system configuration files, application parameters and configuration files, scripts, and text files that you create with an editor or which come with an application (like readme files). Instead of firing up a text editor to view these files, use the less command.

Just make sure that you’re in the same directory as the file that you want to view and type less <filename>, where <filename> is the name of the file that you want to browse.

The less command displays about 25 lines of the file at a time. To move down the file, you can press Enter or the down arrow key (to move one line at a time), or press the spacebar to move one screen at a time. You can move up by pressing the up arrow key on your keyboard, or by pressing b.

You can even search a file while viewing it with less. Just press the forward slash key (/), type in the word or phrase that you want to find, and then press Enter. Note that the search is case sensitive. Typing   isn’t the same as typing Linux. Often, though, you won’t find exactly what you’re looking for on the first try. To keep searching, press n on your keyboard.

If you want to get out of the viewer, press q on your keyboard.

Using sudo

One of the great things about Linux is that it doesn’t allow you to tamper with certain system files and directories. You might be able to view or run the files in those directories, but you can’t delete the files or copy things in or out of those directories.

You can, however, get access to those files and directories using the sudo command. sudo is short for superuser do. It gives you what’s called root access but only for the single run of a program or script.

Why would you need to use the sudo command? You may, for example, need to install fonts for a particular application or install a program that you’ve compiled. Or, you might need to back a file up sudofrom a protected directory to a location in your home directory.

Use this command by typing sudo followed by the name of the command that you want to run. In some Linux distributions, you’ll be asked for the password for the root account. Other distros, like Ubuntu and its variants, don’t have a true root account; you’re prompted for your login password.

Here’s an example: sometimes, I download a useful program or script. I like putting it in the directory /usr/local/bin/, which lets me run it no matter where I am in my Linux file system. But /usr/local/bin/ is one of those protected directories. To copy the file, I type sudo cp <file> /usr/local/bin/.

Sudo is another command that is potentially dangerous. If you’re not careful, you can overwrite or delete an important file. It happened to a friend of mine, who accidentally zapped a library file used by his window manager. Without that file, the window manager wouldn’t start.

Running programs from the command line

Obviously, you can run any application — graphical or terminal-based — by typing its name at the command line and pressing Enter. Graphical applications will run in your window manager; command line applications in the terminal window.

You’ll notice, though, that when you run a graphical application you can’t run any other application in the same terminal window. Sure, you can open another terminal or pop a new tab (if your terminal program supports that), but why bother? Why not run your program in the background instead?

To run a program in the background, type the name of the program, followed by a space, followed by an ampersand (&), and then press Enter — for example, gimp &.

Sometimes, you may try to run a program or script that resides in the directory that you happen to be in. When that happens, you might get an error message like command not found. Nothing’s more frustrating that trying to run an application or script that you know is there. You can get around this by typing a dot and a slash (./) before the name of the program or script. To run the script to back up my company’s Web site, I change to the directory in which the script resides and type ./dmn_bkup.sh.

The wonders of autocompletion

Typing a command or a file name at the command line, especially if it’s a long one, can be a real chore. And if you’re not paying attention, you could type the wrong thing. Instead of typing a long string, press the Tab key on your keyboard to complete the name of the program or file. The Linux shell (another term for the command line) scans the file in your path and in a directory, picks the one that matches what you typed before pressing Tab, and automatically fills it in at the command line.

Autocompletion is incredibly useful when moving around at the command line, or when copying files from one location to another. How? If, say, I want to change to the directory /usr/local/bin, instead of typing 14 characters I just type /usr/l, and press the Tab key — which completes /usr/local/. Then, I type b and press Tab again, which completes the full path. I manage to do the same job with half the number of keystrokes.

Conclusion

The Linux command line is definitely a powerful tool. You can find good references online here and here. If your reading tastes lean more towards dead trees, then definitely check out The Linux Cookbook and the Linux Phrasebook.

Regardless of your level of experience with Linux, understanding the command line and how to use it can definitely come in handy. You might not pop into a terminal window daily or even regularly, but when you do having some knowledge means that you’ll be better equipped to handle yourself.

About Nathaniel Fleming 16 Articles
American economist. Nobel Laureate in Economics in 2017 for his contribution to the field of behavioral economics. Honorary Professor of Behavioral Science and Economics at the School of Business of the University of Chicago.

Leave a Reply

Your email address will not be published.


*