September 25, 2007

Introduction to the command line

People can interact with computers running Linux in two ways -- using the graphical user interface (GUI) or the command line interface (CLI). If you're familiar with the GUI, you may find the CLI intimidating at first sight. Instead of pretty buttons, you get the computer equivalent of a blank, empty sheet: the whole screen, or at least a whole window, with nothing but the borders and the actual command line. But by learning a few fundamentals, you can use the command line to accomplish miracles.

At the command line, you type one or more commands, all in one line, then press the Enter key; the computer analyzes what you typed, executes it, and (sometimes) prints the result just below your instructions. Then you type another command, and so on. You have to know in advance which commands exist, what they do, and how they interact with each other.

Often, a graphical interface is the best, if not the only, way to go: the most common example is advanced photo editing. A mouse-only approach, however, can be extremely limiting. In a graphical interface you only find the buttons that somebody else considered necessary. Above all, you must be there to push them, so it may be impossible to automate anything but the simplest button sequences. If doing something requires just three mouse clicks, doing it 100 times may require 300 mouse clicks. Doing the same thing at the CLI, however, would be just require an action loop. This command, for example, is all you need to find all the JPG images in the current folder and place a thumbnail of each of them in /tmp/thumbnails/:

for picture in `find . -name "*jpg"`; do convert -sample 80x40 $picture /tmp/thumbnails/$picture ; done

The shell

You can get to the CLI in two ways. The first is the standard Linux console, which you'll see if your system isn't running X or if you switch to one of the virtual consoles while X is running. To switch to a virtual terminal, press Ctrl-Alt-F1. Most Linux distributions are set up to have six virtual consoles, and you can access each by using Ctrl-Alt-F2, Ctrl-Alt-F3, and so forth.

A more convenient way, if you're using X, is to use a terminal emulator. Linux distros come with a wide variety of terminal emulators -- from the basic xterm that has been part of X for years, to Konsole and Gnome-Terminal, which have additional features like tabbed windows.

No matter how you access the command line, it always works in the same way. Everything you enter at the command line is interpreted by the shell, which is a program that interprets and executes commands run at the command line or read from a script. To do their job, a shell or its user can create, read or modify variables, which are containers for storing numbers, names, or any other data. Data and variables made only of text are also called strings.

You have the choice of several shells on Linux, each with a slightly different specialization. Some are optimized to reduce memory consumptions, others to perform calculations. The default shell on most Linux distributions is the Bourne again shell, or bash.

The shell looks for executable programs by default in any folders listed in a system-defined variable called $PATH. You can see what folders are defined on your system by typing the command echo $PATH. You can change the value of this or any other shell variable by redefining them in configuration files like /etc/bashrc (for all users) or $HOME/.bashrc (just for yourself), and thus make bash work just as you like. You can also define aliases, or short strings to type in place of commonly used but much longer instructions.

Every time you type a command, the shell does one of two things. If the first argument -- that is, the first sequence of non-whitespace characters -- is the name of a program, then the shell launches that program, passing as arguments to it everything you wrote after that string.

If what you typed isn't a program name, the shell interprets it as a command. The shell also has a simple programming language built in, with the possibility to read or create files and many reserved words associated to the most common functions and operations. For example, cd means "change directory," pwd means "print the name of the current working directory," and history lists the most recent commands you typed.

Combining and mixing commands

You can execute two or more unrelated instructions one after each other by separating them with a semicolon. For instance, if you need to create a backup archive in tar format of all the files in your mywork directory and copy it to some remote server using an encrypted connection, use a command like:

tar cf monthly_backup.tar work/ ; scp monthly_backup.tar myaccount@some.remote.server:monthly_backup.tar

Normally the doesn't allow you to enter further commands until the last one is finished and has displayed its output on the screen. To have the CLI return to the command prompt without waiting for the current task to finish, you have to put the first command in the background -- that is, keep it running but without locking the terminal window. To do this, type Ctrl-Z and then the bg (background) command:

tar cf monthly_backup.tar work/
(the prompt disappears while tar is running)
(the prompt reappears and you can type another command)

To start a program directly in background mode so that it doesn't lock your terminal, add an ampersand to the end of the command before pressing you press Enter:

tar cf monthly_backup.tar work &

You can also combine commands in a more powerful way. The pipe operator (|) attaches one program to another, so that the latter automatically receives and processes all the data created by the former. You can, therefore, type something like:

find . -type f -exec ls -s {} \; | sort -n -r | head -3
  5500 ./monthly_backup.tar
  287 ./picture.jpg
  150 ./diary.odt

This command lists by size (ls -s) all the files in the current directory, sorts them in reverse numerical order (-n -r), and displays only the first three elements (head -3) of the ordered list. While every command performs one generic task, you get only one final result, which is the only thing you were interested in: the three biggest files in the current directory.

You don't have to sit at the keyboard to look at the output of commands. You can use redirection to save the results to a text file. If you add > bigger_files.txt to the previous command:

  find . -type f -exec ls -s {} \; | sort -n -r | head -3 > bigger_documents.txt

the > operator redirects the output of the command to bigger_documents.txt.

Shells are extremely powerful also thanks to another feature: any sequence of commands can be saved to a plain text file and executed again, any time you wish, without retyping everything. All you have to do is to write as the first line in that file the string:

#! /bin/bash

This tells the system that the rest of the file should be directly interpreted by bash. You must also make the file executable with the chmod command:

  chmod 755 my_shell_script_file

This form of programming, called scripting, may be all you ever need to fully customize your Linux computer.

What you can do with bash: practical examples

The point of this article is not so much to teach the basics of shell programming, but to help you understand what the command line is, how it works, and why you should learn more about it.

The best documentation freely available online to help you become a shell guru is probably the Advanced Bash Scripting Guide. As far as quick and practical tips go, instead, there are plenty of them in the "CLI Magic" series on Some are related to system administration, from knowing what is happening in your computer to automating file searches and other operations or discovering malware. Desktop-wise, is is possible to access a Bluetooth phone or manage your contacts.

Together with the serious stuff, however, you can also learn how to play music, rip and convert songs from audio CDs, download and play podcasts, and even upload videos to YouTube. Finally, if you're just bored, you can play at the command line Miller's Quest, Nethack and other games.

Using documentation: man and info

Many Linux commands and programs have documentation available right at the command line. When you need to know how some program works or what are its options, type man or info followed by the program name, and most of times you'll have the answer.


  • Linux
Click Here!