Using the Command Line

Many system management tasks can be performed using either of the graphical user interfaces provided with Fedora (i.e., GNOME or KDE). However, most power users prefer the command line for system management work because they find it faster, more consistent between different versions of Linux, and easier to access remotely. The command line is also called a shell prompt, because the commands are processed by a program called a shell; the standard shell on a Fedora system is the Bourne-again shell (bash).

How Do I Do That?

If you are logged in to the system through the graphical user interface, access the command line through the terminal program. Select the menu option Applications→Accessories→Terminal (System→Terminal in KDE), or right-click on the desktop background and select Konsole under KDE.

Tip

If you find yourself using the terminal frequently, you can make it easier to launch: right-click on the Terminal option in the application menu and select “Add this launcher to panel.” A new panel icon will appear that will launch a new terminal when clicked.

If you have logged in to the system through a character-mode login screen or an SSH login, you will automatically be presented with a command line.

Understanding the shell prompt

The standard shell prompt looks like this:

[chris@concord2 ~]$

This message is an invitation to enter a command. It shows the name of the user (chris), the computer being used (concord2), and the current working directory within the filesystem (~, meaning the user’s home directory). The last character of the prompt, $, indicates that this is a normal user’s prompt, as opposed to the system administrator’s prompt, which ends with #.

Entering commands

To enter a command, simply type it, and then press Enter to execute it. The output from the command will appear after the command (scrolling the screen if necessary), and when the command is done a new prompt will be printed.

To edit a command line, use the left and right arrow keys to position within the line, and the Backspace and Delete keys to delete characters to the left or right of the cursor, respectively. To insert text, simply type it. You can press Enter with the cursor located anywhere on the line to execute the command. Other editing keys are available; Table 4-1 shows the most useful ones.

Table 4-1. Useful editing keys

Key or key sequenceDescription
Left arrowMove left one character.
Right arrowMove right one character.
BackspaceDelete the character to the left of the cursor.
DeleteDelete the character under/to the right of the cursor.
Ctrl-UDelete to the start of the line.
Ctrl-left arrowMove one word to the left.
Ctrl-right arrowMove one word to the right.
Esc, DAlt-DDelete to the end of the current word.
Esc, BackspaceAlt-BackspaceDelete to the start of the current word.
HomeCtrl-AGo to the start of the line.
EndCtrl-EGo to the end of the line.

Accessing previous commands

You can scroll through the history of previously entered commands using the up and down arrow keys. This enables you to easily re-enter a command, either exactly as you previously entered it or after editing.

You can also search for a previous command by pressing Ctrl-R (for reverse search) and then typing a few characters that appear in the command. For example, if you had at some previous point typed cat /etc/hosts and you pressed Ctrl-R and typed hos, the cat /etc/hosts command would appear (providing that no intervening commands contained the letter sequence hos).

Obtaining a root prompt to enter commands as the superuser

The superuser account, root, is also called the privileged account, because it is not subject to the security restrictions that are applied to regular user accounts. root access is required for many system administration commands. Although it’s tempting to use the root account all the time on a single-user computer, it is unwise because Fedora assumes that you know what you’re doing and won’t ask for confirmation if you enter a dangerous command; it will just go ahead and execute it. If you’re using the root account, an incorrect command can cause a lot more damage than the same command executed in a normal account.

Although you can directly log in as a root user, it’s usually much safer to take on root privilege only when necessary, using the su (switch user) command:

$ su
Password: 
                     root-password
                  
#

The shell prompt will change to end in a pound sign (#) instead of a dollar sign ($) when you are in root mode. Press Ctrl-D or type exit to drop superuser access and return to your regular shell prompt.

Tip

In this book, I’ll use $ to indicate any normal user’s prompt, user $ to specifically indicate user’s prompt, and # to indicate the root prompt. Avoid entering commands as root unnecessarily!

Linux error messages

Many Linux commands will output a message only if something goes wrong. For example, if you try to remove a file using the rm command, no message will be displayed if the file is successfully deleted, but an error message will be generated if the file does not exist:

$ rm 
                  
                     barbeque
                  
rm: cannot remove \Qbarbeque': No such file or directory

Most error messages start with the name of the command that produced the message.

Logging out of a shell prompt

You can leave a shell by pressing Ctrl-D or typing exit. If you are using a terminal window and don’t have any programs running, you can simply close the window using the X button on the title bar.

How Does It Work?

The shell prompt is managed by bash, the Bourne-again shell. bash got its name from the fact that it is a successor to the original Unix shell, sh, which is also known as the Bourne shell (after its author, Steve Bourne). bash is a command editor, command interpreter, job controller, and programming language.

When bash receives a command, it splits it into words and uses globbing to expand any ambiguous filenames. bash next checks to see if the first word is a built-in command. If not, it treats it as an external command or program and searches a list of directories to find that program. It executes that program, passing the other words to the program as arguments. Almost all Linux commands are external programs.

Linux commands generally accept three types of arguments:

Options

These start with a hyphen or double-hyphen ( - or --) and modify the way the command operates. For example, the ls (list-files) command will include hidden files in its output if the -a argument is given, and will list detailed information about files when the -l option is specified. These options may be used individually, used together in any order, or combined after one hyphen if they all use a single hyphen:

$ ls -l
$ ls -a
$ ls -l -a
$ ls -a -l
$ ls -al
$ ls -la
Positional arguments

These have significance according to the order in which they are specified. For example, the cp (copy) command accepts two or more filenames:

$ cp 
                        
                           one two
                        

one is the name of the file being copied, and two is the name that will be given to the new copy. If you swap the position of the two arguments, the meaning of the command is changed. Options may be placed before, between, or after positional arguments; usually, the positions of the options don’t matter.

Options with a value

These combine options with positional arguments. An option with a value may be placed before or after other arguments, but the value must be placed directly after the option.

For example, the ls command accepts the -w option (width of output), which is specified along with a number indicating the desired width of output in characters. This can be combined with the -a and -l options in any order, as long as the number immediately follows the -w option:

$ ls -a -l -w 60
$ ls -w 60 -al
$ ls -l -w 60 -a
$ ls -l -w60  -a
$ ls -alw60

What About...

...accessing a character-mode display when the graphical user interface is running?

Fedora is configured to allow you to log in using a character-mode display even if the graphical user interface is running. In fact, you can log in up to six times, using the same or different user IDs.

The key is Virtual Terminals (VTs). There are 12 virtual terminals that can be accessed easily: VT1 through VT6 are configured for character-mode login, VT7 is used for graphical login, and VT8 through VT12 are not normally used.

To switch to a specific VT, press Ctrl-Alt and the function key that corresponds to the virtual terminal you wish to access (Ctrl-Alt-F1 for VT1, Ctrl-Alt-F7 for VT7, etc.).

Tip

There are actually 64 virtual terminals, but virtual terminals above number 12 are not directly accessible from the keyboard and are therefore rarely used.

You can log in on multiple VTs simultaneously and switch back and forth between them. This is particularly useful when you bring up documentation on one VT and enter commands on another.

...finding out where a program is located?

The type, which, and whereis commands all provide information about the location of programs.

type will tell you where a command is located in a verbose way, along with an indication of whether the command location is hashed (stored in the shell for quick reference because the command has already been used recently). If there is more than one command with the same name, the location shown is the first one found using your $PATH:

$ type 
                  
                     cat
                  
cat is hashed (/bin/cat)

which is similar, but shows only the command location:

$ which 
                  
                     cat
                  
/bin/cat

whereis will show you all of the locations for the command (and sometimes there are several, if different versions of the same program are installed), along with the location of its manpage documentation:

$ whereis 
                  
                     cat
                  
cat: /bin/cat /usr/share/man/man1p/cat.1p.gz /usr/share/man/man1/cat.1.gz

...starting graphical programs?

Programs with a graphical user interface are started in exactly the same way as programs with a character-based user interface. GUI-based programs use the DISPLAY environment variable to determine if a graphical display is available and to connect to that display. Some programs, such as system-config-printer, will automatically start up with a graphical or a character-based user interface according to the type of display that is available.

...quickly entering a single command?

Typing Alt-F2 will open a Run Application dialog (in KDE, it’s called Run Command), which enables you to enter a single command and run it. This is most useful for starting graphical programs that aren’t on the menu.

You can also add an applet to your panel bar that does the same thing.

...the difference between commands, utilities, applications, and programs?

There isn’t any! Linux does not make any distinction between categories of programs.

...using a different shell?

Fedora offers four different command shells: csh (a.k.a. tcsh), bash (a.k.a. sh), ksh, and zsh. You can temporarily start a different shell just by typing the shell name:

$ 
                     csh
                  

Press Ctrl-D or type exit to return to the original shell. You can permanently change your default shell using the chsh (change shell) command:

$ chsh
Password: 
                     bigsecret
                  
New shell [/bin/bash]: 
                     /bin/csh
                  
Shell changed.

The password requested is your normal login password; the change will take effect the next time you log in.

chsh requires that you enter the full pathname of the new shell. To see a list of available shells, use chsh with the -l (list) option:

$ chsh -l
/bin/sh
/bin/bash
/sbin/nologin
/bin/ksh
/bin/tcsh
/bin/csh
/bin/zsh

zsh, ksh, and bash each use a syntax related to the original Bourne shell (sh). csh uses a very different syntax, which C programmers often find comfortable.

Where Can I Learn More?

  • The bash, chsh, csh, zsh, and ash manpages

Get Fedora Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.