LINFO

How to Use The Command Line

A First Lesson


The command line (i.e., all-text display mode) is a key part of any truly modern computer operating system1.

Because graphical user interfaces (GUIs) have become very easy to use in the past decade or so, it is no longer common for most computer users to utilize the command line, especially for simple tasks such as word processing, searching the web and sending e-mail. Thus, the command line is frequently perceived as being unnecessary, intimidating and even obsolete. A GUI is a display mode that contains images, windows and menus and which is manipulated primarily with a mouse.

However, the command line, also referred to as the shell (although the shell is actually a program the provides the command line), can be quite easy to learn and use, and its value soon becomes apparent after a little practice. Even a basic familiarity with it can make computers easier to use and facilitate performing tasks that might be difficult or impossible with a GUI. Such familiarity can also lead to an improved understanding of how computers actually work.

Many people who are new to the Linux command line will be familiar with the command line used in MS-DOS and think that the two are similar. However, the similarities are largely just superficial, and there are vast differences. The Linux command line is far more powerful (i.e., it is much more flexible and can do many more things), and in some ways it is much more user friendly.

Studying the Linux command line provides an education in much more than just how to use a specific operating system. One reason is that the Linux command line is virtually identical to the command line used on every other Unix-like operating system, e.g., Solaris, FreeBSD and Mac OS X. Thus, multiple operating systems are being learned simultaneously.

Studying the command line also provides insight into how computers really work. This is because the command line is much closer to the internal functioning of computers than are GUIs, which are generally just front ends for the commands used on the command line. Moreover, the underlying commands typically have greater flexibility than their GUI counterparts, they can easily be combined with other commands, and they can be used in situations where a GUI is not available (e.g., making system repairs) or is not functioning properly.

Accessing the Command Line

A first step in learning about the command line is accessing it. Usually, the easiest way to do this is to open a terminal window, which is an all-text window in a GUI. This can be accomplished by merely clicking on the appropriate menu item or icon (i.e., small image). For example, in the case of Red Hat Linux, all that is necessary is to select the item called Terminal which appears in the System Tools menu. On some distributions (i.e., versions) of Linux, a terminal window can be opened by clicking an appropriate icon that sits on the toolbar (i.e., strip of icons) along the bottom of the screen.

Another way to open the command line is to switch to a console, which is a display mode in which the entire screen is text only and there are no buttons, windows or other GUI objects. The easiest way to accomplish this is to simultaneously press the CRTL, ALT and F1 keys while in a GUI. The GUI disappears and the entire screen becomes black except for a few white characters (which constitute the command prompt) in the upper left-hand corner. Fortunately, the GUI can be restored at any time on a typical system by simultaneously pressing the CTRL, ALT and F8 keys.

The pwd Command

A good first command to learn is pwd, which stands for present working directory. This command shows the name and location of the current directory, which is the directory (also called a folder on some operating systems) in which the user is currently working. All that is necessary in order to use this command is to type the word pwd in at the keyboard as follows and then press the ENTER key:

pwd

If the terminal window or console has just been opened, what will typically be shown on the next line on the monitor screen will be /home followed by another forward slash and then the name of the user's home directory (which is usually the same as the user name). For example, if the user has a user name of john, the line would say /home/john. This is because a user begins working at the command line in its home directory, home directories are located in the directory named /home, and a user's home directory typically has the same name as the user name.

Commands (and virtually everything else) in Unix-like operating systems are case sensitive, in contrast to MS-DOS, which is case insensitive. That is, lower case and upper case letters are treated as completely different characters. Thus, for example, pwd must always be written in all lower case letters, not as PWD or Pwd2.

pwd becomes more useful after the current directory has been changed a few times, because then the location and name of the current directory are not always as obvious.

The ls Command

ls is another of the most basic and frequently used commands on Unix-like operating systems. It is equivalent to DIR on MS-DOS systems, and it lists the contents of a directory. When used just by itself, it provides a list of the names of the objects (i.e., files, directories and links) in the current directory, i.e.,

ls

It can be seen that various names are listed but that no additional information is provided about them. This is often sufficient, but in many cases it is desired to obtain additional information about each directory object. Obtaining such information can be easily accomplished by using ls together with one or more of its options. An option is a letter (or in some cases a word) that follows the command after a space and that tells the command how to behave. Multiple options can usually be used together, and they are usually preceded directly by a hyphen (without any intervening space). The options available can differ substantially according to the specific command.

For example, the -a option tells ls to show all objects in the directory, inclusive of hidden files or hidden directories. Hidden objects are those whose names are normally not visible either in the command line or when examining the contents of a directory in a GUI. Their names are preceded directly by a period, such as .file1. Thus to see the names of all objects in the current directory, the following command should be typed in and then the ENTER key pressed:

ls -a

Many commands have numerous options, and ls is no exception. However, typically only a few of them are frequently used. Another commonly employed option for ls is -l, which provides a long listing, i.e., it provides much information about each object in addition to just its name. This additional information includes the type of object (e.g., file, directory or link), its permissions (i.e., who has access to it for reading, writing and/or executing), its owner (which is by default the same as its creator), and the date and time of creation.

When used together with both its -a and -l options, the command would be written as

ls -al

Another common option for ls is -s, which shows the size of each file (but not directory 3) in kilobytes. This option is often used together with the above two options as

ls -als

Like most commands, but unlike pwd, ls can accept input data, which is referred to as an argument. In the case of ls, arguments are the names of directories about which it is desired to obtain information. Any number of directories can be listed as arguments. They all follow ls and any options, and they are separated by at least one blank space.

For example, if it is desired to see the names of all the objects in both the directories /bin and /home, the following command would be used:

ls -a /bin /home

As is the case with /home, /bin is a standard first tier directory in the root directory, which is the single directory that contains all other directories and their subdirectories on a Unix-like operating system and which is represented by a forward slash ( / ). Like all other first-tier directories in the root directory, their names begin with a forward slash, in part so that they will not be confused with directories with similar names that are not first tier directories of the root directory.

The cd Command

The last of what are perhaps the three most basic commands on Unix-like operating systems is cd, which is used to change the current directory. This is equivalent to the CD and CHDIR commands in MS-DOS.

Thus, for example, to change the current directory to the /bin directory, the following would be used:

cd /bin

Likewise, to change to the root directory, the following would be used:

cd /

Any user can return to its home directory by using a tilde as the argument for cd. A tilde is a short, wavy, horizontal line that represents the home directory of the current user, and this character is located in the upper left hand corner on most standard English language keyboards. That is, any user can return immediately to its home directory by typing the following and then pressing the ENTER key:

cd ~

This is easier than typing the full name of the user's home directory as an argument, for example /home/josephine in the case of a user named josephine. It is just one of the numerous shortcuts that help make the command line on Unix-like operating systems so easy to use4.

For additional introductory information on the use of the command line, see How to Use the Command Line, A Second Lesson.


________
1Microsoft downgraded the command line in its operating systems subsequent to Microsoft Windows 95 in an attempt to allow virtually all operations to be performed in the GUI. However, this has not been successful in the opinion of many computer experts, and the company has thus been developing a command line (code named Monad) similar to those in Unix-like systems for use in its future operating systems.

2 For purposes of clarity and consistency, the case of commands, keywords, etc. that are always written in lower case is maintained when discussing them in text in The Linux Information Project, even when they are used at the beginning of sentences or in titles (although this may at first appear odd or even erroneous).

3 In Unix-like operating systems everything is considered to be a file, and that includes directories, which are just a special type of file. However, there are some differences in behavior, of which this is an example.

4 Among the others are command completion, which is the automatic completion of the names of commands and their arguments that have only partially been typed by a user, command history, which allows the user to view, edit and reissue previously executed commands, and wildcards, which can represent any group of files and directories.






Created August 9, 2005. Updated April 17, 2006.
Copyright © 2005 - 2006 The Linux Information Project. All Rights Reserved.