[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ A ] [ B ] [ C ] [ D ] [ next ]

User's Guide
Chapter 5 - Getting to Know the Command Line


A command line is a text-based interface for interacting with your computer. You type a command, then press the Enter key to run it. If you have used DOS , then you have used a command line.

The command line is also known as the terminal, xterm, console, command shell, or shell. Some of these terms make technical distinctions, but, for most purposes, you can consider them identical.

This chapter explains the basics of using the Linux command line. It covers:

The rest of the manual also contains command line solutions to different computing tasks.


5.1 Why Use the Command Line?

Most Windows and Mac OS users work with a graphical desktop and ignore the command line. Increasingly, many Linux users are doing the same. However, you should learn how to use the command line for several reasons:

These reasons are important enough that many Linux users still prefer to work from the command line instead of the desktop.

If you have used DOS, you will find the Linux command line much easier and much more powerful. Not only does it have many shortcuts to save you time, but you can even choose the command line interface - the shell - that best suits your needs. This manual assumes that you are using bash (Bourne Again Shell), the default for Progeny Debian and most other Linux distributions.


5.2 Starting a Command Line

If your computer is not set up to use a graphical desktop, it starts with the command line already running.

To start a command line while running a graphical desktop, do one of the following:


5.3 Entering Commands

To new users, a command can look overwhelming. However, once you realize that all commands follows the same basic structure, you can start using them efficiently.

This section covers:


5.3.1 Understanding the Parts of a Command

A command is sometimes a single word. However, a command may include:

What a command does can vary greatly, depending on what options, arguments, and files are used. For example, If you simply type date , then the current date and time display. However, if you type date 0707132101, then your computer's date is changed to 1:21 pm on July 7, 2001.

[Note]

Commands, options, and arguments are often abbreviated descriptions of what they do. For example, cp copies a file, while su switches the user account you are using. Similarly, in some commands, the -V option is short for Version, and lists the version number of the command. Knowing the full description is a useful way to memorize commands.


5.3.2 Understanding Command Structures

The parts of a command are always entered in the same order:

command options argument files

A typical command is chgrp -Rv root /home/jtang/ToDo.

In this example:


5.3.3 Understanding Common Options

Many options differ with each command. However, a few basic options are used reasonably consistently. Some may have slight variations, such as using upper or lower case letters. If you have any doubt about an option, check the help before using it.

                            Common Command Options                        
     +-------------------------------------------------------------------+
     |     Option      |                  What It Does                   |
     |-----------------+-------------------------------------------------|
     |  * -d           |Displays debugging information                   |
     |  * --debug      |                                                 |
     |-----------------+-------------------------------------------------|
     |  * -f           |Runs the command no matter what the consequences,|
     |  * --force      |such as over-writing existing files.             |
     |-----------------+-------------------------------------------------|
     |--help           |Displays info help for the command               |
     |-----------------+-------------------------------------------------|
     |                 |  * Waits for confirmation before doing each part|
     |                 |    of the command ( i, interactive ).           |
     |  * -i,          |                                                 |
     |    --interactive|    or                                           |
     |  * --ignore-case|                                                 |
     |                 |  * Ignores the distinction between lower case   |
     |                 |    and capital letters ( insensitive ).         |
     |-----------------+-------------------------------------------------|
     |  * -R, -r       |Includes files and sub-directories of the        |
     |  * --recursive  |directory name in the command.                   |
     |-----------------+-------------------------------------------------|
     |  * -V, -v       |Displays what a command is doing so you can see  |
     |  * --verbose    |its progress. In some cases, -V or -v gives the  |
     |                 |version number of the command.                   |
     +-------------------------------------------------------------------+

5.3.4 Entering Multiple File Names for a Command

The simplest way to have a command affect multiple files is to enter the file names with a space between them. Because this method specifies which files are affected by the command, it reduces the chances of accidently applying the command to the wrong file.

For example, if a user called mary wanted to copy files called tasklist and notes to her backup directory, she would type:

cp /home/mary/tasklist notes /home/mary/backup/.


5.3.5 Entering Commands with Strings

Some commands use a string as an argument. A string is a combination of letters, numbers, characters and/or spaces inside single or double quotation marks.

Within a string, you can type in multiple words, or disable metacharacters (also known as wild cards). A string that uses metacharacters is referred to as a regular expression. See Using Metacharacters or Wild Cards, Section 5.3.6.

If you use double straight quotation marks, all metacharacters are disabled except environment variables, such as $ home.

Single straight quotation marks disable all special characters. In most cases, use single quotation marks.

Do not use true quotation marks (" "). True quotation marks may be a problem if you copy and paste from a word processor.

For example, if you wanted to search user jsmith 's home directory for a file that contained the phrase "business expenses," you would use the grep command with the phrase as an argument: grep 'business expenses' /home/jsmith/.


5.3.6 Using Metacharacters or Wild Cards

Metacharacters are characters that designate a range of choices. They are also called special characters or wild cards.

You may know the basic metacharacters from other operating systems:

[Warning]

Metacharacters can be extremely powerful, especially when used in combination. Be especially careful about using them when you are deleting and moving files. You may affect more files than you realize.

[Note]

If you want to search for a character that is usually used as a metacharacter, place a backward slash ( ) in front of it. For example, ? searches for a question mark, instead of any character.


5.3.6.1 Metacharacters or Wild Cards

     +-------------------------------------------------------------------+
     |  Metacharacter   |                   Stands for                   |
     |------------------+------------------------------------------------|
     |.                 |Any character except a new line marker.         |
     |------------------+------------------------------------------------|
     |?                 |Any one character, or none.                     |
     |------------------+------------------------------------------------|
     |[ ]               |Any one of the enclosed characters.             |
     |------------------+------------------------------------------------|
     |*                 |Any number of characters, or none.              |
     |------------------+------------------------------------------------|
     |^ string          |The string at the start of the line.            |
     |------------------+------------------------------------------------|
     |string $          |The string at the end of the line.              |
     |------------------+------------------------------------------------|
     |string +          |Match one or more of the characters in the      |
     |                  |string                                          |
     |------------------+------------------------------------------------|
     |string            |Match a range of instances                      |
     |------------------+------------------------------------------------|
     |( string | string |One of the strings.                             |
     |)                 |                                                |
     +-------------------------------------------------------------------+

Metacharacters are especially powerful when used together. For example:


5.3.7 Entering Multiple Commands

Separate multiple commands by a semi-colon.

For example, if you type cd /backup;ls s* , you move to the /backup directory (using cd), and list all files in the directory that start with s .


5.3.8 Combining Commands

You can combine commands by typing them with the pipe character (|) between them. This technique is known as piping.

For example, if you type ls|less , you display the files of the current directory ( ls ) using the less command, which allows you to scroll back and forth in the list. This combination would be useful if you had a long list of files.

[Warning]

Use piping carefully. Sometimes, it has unwanted or unexpected results.


5.3.9 Running Programs in the Background

If you are using the X Window System, the command prompt is generally in its own window. As a result, you can easily do another task while a command is running. However, if you are using only the command line, you can run a command in the background, so that you can use the shell for another task while the command is running.

To run a command in the background, type command & .


5.3.10 Stopping Commands from Running

Press Ctrl-C.

The command that is running stops. Then a new command prompt displays below the previous one.


5.4 Navigating the Command Line

Linux systems store files in directories. The directories are arranged in a tree-like structure. At the top of the tree is the root directory (/). Even directories on another partition are sub-directories of the root directory. See Searching for Files and Text, Chapter 8.

Using a few basic commands, you can


5.4.1 Changing directories

Type cd directory .

Unless the directory you are changing to is a sub-directory of the current one, usually you should type the full path name to the directory.

To change to the directory immediately above the present one, type "cd .." (two periods).


5.4.2 Reading the Contents of Directories

Type ls options directory .

By carefully selecting your options, you can use the ls (list command to help you locate files. For example, the -S option lists files in order of size, while the -t option lists files by when they were modified.

Another trick is to set an alias, so that typing list results in ls -- color , which prints files in different colors, depending on what kind they are. See Creating an Alias, Section 5.5.4.1.


5.4.2.1 The ls Command (List)

     +-------------------------------------------------------------------+
     |     Option      |           Action           |      Comments      |
     |-----------------+----------------------------+--------------------|
     |-a               |Includes hidden files in the|Most hidden files   |
     |                 |list.                       |start with a period.|
     |-----------------+----------------------------+--------------------|
     |  * --color      |Displays types of files in  |                    |
     |  * --colour     |color.                      |                    |
     |-----------------+----------------------------+--------------------|
     |                 |                            |Common symbols are: |
     |                 |                            |                    |
     |  * -F           |Displays types of files     |  * /: Directories. |
     |  * --classify   |using symbols.              |  * *: Executable   |
     |                 |                            |       files.       |
     |                 |                            |  * @: Symbolic     |
     |                 |                            |       links.       |
     |-----------------+----------------------------+--------------------|
     |--full-time      |Prints complete file        |                    |
     |                 |attributes.                 |                    |
     |-----------------+----------------------------+--------------------|
     |  * -l           |Displays file attributes.   |                    |
     |  * format=long  |                            |                    |
     |-----------------+----------------------------+--------------------|
     |  * -L           |Lists the files referenced  |See Working With    |
     |  * --dereference|by symbolic links instead of|Symbolic Links.     |
     |                 |the links themselves.       |(***)               |
     |-----------------+----------------------------+--------------------|
     |  * -R           |Includes sub-directories.   |                    |
     |  * --recursive  |                            |                    |
     |-----------------+----------------------------+--------------------|
     |  * -S           |Sorts files by size.        |Largest files are   |
     |  * sort=size    |                            |displayed first.    |
     |-----------------+----------------------------+--------------------|
     |  * -t           |Displays files in the order |The most recently   |
     |  * --sort=time  |in which they were modified.|modified files are  |
     |                 |                            |displayed first.    |
     |-----------------+----------------------------+--------------------|
     |  * -X           |                            |Not all files may   |
     |  * --sort=      |Sorts by file extensions.   |have extensions.    |
     |    extension    |                            |                    |
     |-----------------+----------------------------+--------------------|
     |  * -x           |Lists files in multiple     |                    |
     |  * --format=    |columns.                    |                    |
     |    across       |                            |                    |
     +-------------------------------------------------------------------+

***) See Working With Symbolic Links, Section 9.16.


5.4.3 Identifying Types of Directory Listings

Type: file options file .

To see the types of all the listings in a directory, type the directory name followed by the asterisk wild card. For example: file /root/* .

[Note]

The file command does not read compressed files or symbolic links unless the -z option is chosen.

[Note]

Another way to identify directory listing is to use the ls command with the --color option. See Reading the Contents of Directories, Section 5.4.2.


5.4.3.1 The file Command (Identifies File Types)

     +-------------------------------------------------------------------+
     |Option|                   Action                    |   Comment    |
     |------+---------------------------------------------+--------------|
     |-L    |Lists the files referenced by symbolic links,|Traces the    |
     |      |rather than the links themselves.            |original file.|
     |------+---------------------------------------------+--------------|
     |-z    |Reads compressed files.                      |              |
     +-------------------------------------------------------------------+

5.4.4 Finding Your Current Location

Type pwd .

The current or working direction displays on the screen.


5.4.5 Clearing the Screen

Type clear .

Only the screen display is affected. The command history is still usable.


5.5 Using Command Line Shortcuts

Linux has several shortcuts for entering commands:


5.5.1 Using Tab Completion

By pressing the Tab key, you can have the shell complete the command you are typing. You can also use Tab completion as a guide to commands if you are unsure of which command you want.

If only one command completes what you have typed, then the full command is entered automatically. However, you still need to add any options, arguments and files, and press the Enter key.

If more than one command completes what you typed, then a list of possible completions displays. You can use the list as a guide to commands. Or you can type another letter, press the Tab key, and display another list of completions.

For example, if you type chg then press the Tab key, the command chgrp is entered at the prompt. No other command completes chg .

On the other hand, if you type cha , you get the following list:

chage chat chattr

This time, these three command complete what you have typed.


5.5.2 Using Command History

The .bash_history file in your home directory lists all the commands that you have entered from the command line.

You can use this file to re-enter commands that you have already used. This ability is useful if you are doing repetitive work, or if you have made a mistake and want to correct it without re-typing.

To move through the command history, use the up arrow key to repeat the previous command in the history. Use the down arrow key to repeat the next command.

If you are at the last command, then using the arrow key deletes what you have entered at the command prompt.

If you change user accounts, you cannot use the command history of the original account.


5.5.3 Using Text Pasting

You can use the mouse to copy and paste text that still displays on the screen.

To paste a command:

  1. Press the left mouse button, and drag the cursor over the text that you want to paste. The text is highlighted.
  1. Release the mouse button.
  1. Place the cursor at the spot where you want to paste the text.

    Then click the middle mouse button. The text is pasted. If you selected an entire line with a command, then the command runs automatically.


5.5.4 Using Command Aliases

An alias is a shorter name for a command. Aliases are especially useful if you want a shortcut for a command with a set of commonly used options.

You can:


5.5.4.1 Creating an Alias

Type alias alias = command option .

If the original command includes options and arguments, enter it as a string in single quotation marks

For example, if you regularly printed three copies of everything to a printer called progeny, you could create an alias by typing alias p3= 'lpr -3 -P progeny' .

Once the alias is created, you can use the alias instead of the original command. For example, to send the file contactlist.txt to the printer, you would type p3 contact.txt .


5.5.4.2 Deleting an Alias

Type unalias alias .


5.5.4.3 Viewing Aliases

Type alias . A list of aliases displays.


5.5.5 Abbreviating File Path Names

One of the biggest drawbacks to the command line is that typing file names can be cumbersome. You can reduce the amount of typing by using:


5.5.5.1 Using Special Symbols

You can use the following special symbols to reduce the amount of typing you need to do in file names.

                     Special Symbols for the Command Line                 
     +-------------------------------------------------------------------+
     |Symbol|                     What it stands for                     |
     |------+------------------------------------------------------------|
     |.     |The directory that you are now in (the working directory).  |
     |------+------------------------------------------------------------|
     |..    |The directory above the one you are in now (the parent      |
     |      |directory).                                                 |
     |------+------------------------------------------------------------|
     |~     |Your home directory.                                        |
     |------+------------------------------------------------------------|
     |~user |The home directory for the user account listed.             |
     |------+------------------------------------------------------------|
     |/     |The root directory                                          |
     +-------------------------------------------------------------------+

5.5.5.2 Using Relative Path Names

A file's complete path name starts with the root directory. For example, the path for the file ToDo in the January folder of the home directory for jgill would be /home/jgill/January/ToDo .

A complete path is generally known as the absolute path.

Instead of typing the absolute path, you can save time by typing the relative path. A relative path:

For example, if you were in the /jgill/January directory, the relative path to ToDo would be January/ToDo . In the home directory, the relative path would be jgill/January/ToDo .


5.5.6 Redirecting Input and Output for Commands

In most cases, you enter a command from the keyboard, and the results and any error messages display on the screen.

In other words, the keyboard is the standard input, and the screen is the standard output and standard error.

By changing these standards, you can often save time and effort.

Uses for redirection include:

As you learn the command line, you may find other uses for redirection.

To redirect a command's input, output, or error, you use a small set of characters known as redirection operators.


5.5.6.1 Redirection Operators

     +-------------------------------------------------------------------+
     |Characters|                      What it does                      |
     |----------+--------------------------------------------------------|
     |          |Redirects the output to a file or a piece of hardware,  |
     |>         |such as the printer. If the file already exists, the    |
     |          |older file is automatically overwritten.                |
     |----------+--------------------------------------------------------|
     |          |Redirects the output, but if the output is a file that  |
     |          |already exists, adds the output to the end of the       |
     |>>        |existing file.                                          |
     |          |                                                        |
     |          |Usually, this character is safer to use than >, since it|
     |          |does not overwrite existing files.                      |
     |----------+--------------------------------------------------------|
     |2>        |Redirects the error output to a file or a piece of      |
     |          |hardware, such as the printer.                          |
     |----------+--------------------------------------------------------|
     |2>&1      |Redirects the error output to a standard output         |
     |----------+--------------------------------------------------------|
     |<         |Redirects the input of the command from a file or       |
     |          |device.                                                 |
     +-------------------------------------------------------------------+

The format for using redirection is:

command operator target

For example, if you wanted to save a copy of a search for the keyword "expenses" in a file, you could type grep 'expenses' January2001> expenses.txt .

In this example:


5.5.7 Using Environment Variables

Environment variables define different aspects of the way that you interact with the system. Some, like $home (the variable for your home directory) are set for you. However, you can also create your own variables to use in scripts or for saving time when you use the command line.

Variables that you create are stored in the .bash_profile file in your home directory.


5.5.7.1 Adding a Variable

Type: export variable=value .

For example, if you wanted to create the variable $ income to refer to the directory /home/jsmith/Expenses, you would type export $ income /home/jsmith/Expenses .


5.5.7.2 Using a Variable

To use a variable, type its name with a dollar sign in front of it where you would ordinarily type the value. For example, if you create the variable $ income to refer to the directory /home/jsmith/ Expenses, you can view the file /Expenses/january, by typing less $ income /January .


5.6 Finding Help for Commands

This manual lists only a few basic commands. A thorough guide to commands would require well over 1000 pages.

To find more information about Linux commands, you can use either the man or texinfo help systems. For quick help, you can use whatis .


5.6.1 Using man Pages

Man ("manual") pages are the traditional help files of unix and Linux. Man pages exist for both commands and configuration files.

Man pages are summaries rather than how-tos. In addition, many are no longer being updated. For this reason, man pages are often hard to use.

To open a man page, type man command or man file .


5.6.1.1 Searching man Pages

You can search man pages for all entries on a particular topic by typing: apropos keyword . The one drawback to this command is that common topics can list an overwhelming number of entries.


5.6.2 Using Info Documents

Info documents are gnu help. Usually, info files are more current and more thorough than man pages. Most people find them easier to use.

To open an info document, type command --help .


5.6.3 Using whatis

The whatis command gives a short summary of the topic entered.

Type: whatis keyword .


[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 12 ] [ 13 ] [ A ] [ B ] [ C ] [ D ] [ next ]

User's Guide


Version: 1.00p00, 2006.06.17-21:39

Progeny Linux Systems, Inc.