1.3 Command Line

The Command Line

For the duration of this course, you'll be using the command line interface (CLI) rather than the GUI.  There are several reasons for this. 

The terminology surrounding the CLI might be confusing at first.  The shell, terminal, command prompt, and CLI all have different meanings, but are basically interchangeable when telling someone where to enter a command. 

The shell is the interpreter between the user and the OS.  The default shell for Linux is bash (the Bourne Again SHell) although options include csh (the C Shell), ksh (Korn Shell), and others. Certification exams use  bash, so that is what this course will use.

A terminal emulator is the program that runs the shell in a window within a GUI.

A virtual terminal is used when outside the GUI. You can switch between them by pressing Ctrl+Alt+F1 through F7 on most systems. One of them will also probably contain X and the GUI, most often the 7th one.

The command line is the line you type commands on to feed to the shell. The command prompt refers to the output that tells the user that the terminal is ready to accept commands. Since the prompt can be customized, it is typically simplified to $ for users and # for root.

Throughout the course, whenever you are told to type something into the shell/terminal/command line/command prompt/CLI, it all refers to the same action.

If you're not in a GUI, you're probably already at the command line (unless you're at a login screen).  If you are in a GUI, you'll need to open a terminal emulator.  In Knoppix, click on the Terminal Emulator button in the bottom left to open a terminal. Look for an icon that looks like a monitor or a program that has terminal, term, or console in the name. Some of the more common terminal emulators are xterm, rxvt, Konsole (in KDE), GNOME Terminal, eTerm, and so forth.

Using the Command Line

When using the command line, most of the time you'll be operating as a normal user and see $ as part of the prompt. If opening a terminal emulator from a GUI, you won't have to login, but if not using a GUI, you'll start at a login prompt. Type your username, press Enter, and you will be prompted for your password. Type your password and press Enter again. You will now be at the command prompt. To logout, use the logout command.

Now you'll have a command prompt:

$

Many of the topics covered in this course will require you to be root/have admin privileges. One way to become root is using the su command. Type su in the command prompt and enter the password for the root account when prompted for it. You will then be logged in as root. Type exit at the command prompt when you wish to return to your normal user. Another option is to use sudo to start a line with a command on it. This will allow that single command to be run as root.

To navigate around the file system and learn how to use the CLI, you will need to enter some commands.  Remember that all commands, files, paths, etc., are case sensitive in Linux.

$ pwd

This is the "You are here" of the CLI.  It prints the working directory.  The output it generates will look something like this:

Paths

Above is an example of an absolute path (the full path from the root directory, /).  A relative path, on the other hand, is in relation to the current working directory.

Now that you know where you are, you can go somewhere else with the cd command (change directory).  Using the cd command without any options will change the directory to your home directory, usually at /home/yourUserName/.  To go to another directory, you can use cd with either a relative an absolute path:

$ cd /usr/bin

or an absolute a relative path:

$ cd ../Downloads

Two symbols that appear frequently in relative paths are . (period) and .. (double period).  The single period represents the current working directory, while the double period represents the parent directory.  Thus, to go to the parent directory:

$ cd ..

Many commands take files as arguments.  Editors need a file to edit and so forth.  To point to a file in the working directory, just use its name.  To point to a file that is in some other directory, an absolute or relative path will both work.

$ nano foo # textfile foo in current directory
$ nano ../bar/foo # textfile foo with relative path
# nano /etc/fstab # textfile fstab with absolute path
$ nano $HOME/foo # textfile foo in the user's home directory
$ /usr/bin/free # the free command in the /usr/bin directory
$ ./script5 # the script5 command in the current directory

nano is a text editor. If you try out some of these commands and find yourself stuck in nano, use Ctrl-x. At the bottom of its window, you can find commands for nano that can be used via Ctrl and the appropriate letter.

To see directory navigation in action, play the video below.

Commands can also be called using absolute and relative paths.  This is usually unnecessary, but executing programs outside of the $PATH variable (which tells the shell where to look for executable commands), like a script in the $HOME directory, will require an explicit path to the command.

Directory Contents

To see what's files are in the a directory you're in, use the ls command.

$ ls

Without any options, ls lists most of the contents of a the working directory.  If you want all the contents of a directory (including hidden files that begin with a period, ie .xinitrc), use:

$ ls -a

This will include even . (the current directory) and .. (the parent directory).  For more details on the contents, use the long option, -l, like so:

$ ls -l

To show more information about all the items in the current directory, just combine the options:

$ ls –la

Even the basic command ls has over 50 options that can be specified from the command line.  But don't worry, you don't have to keep track of them all.  That's what man pages are for.

Bash Conveniences

Tab completion is a very useful feature of bash.  The shell will try to complete whatever you've begun to type.  It will complete commands, file names, and directory names.  For example, you want to cd to ./a_Very_Long_Directory_Name, you could type

$ cd a_

and press tab.  If the beginning is unambiguous, the shell will expand it into what it thinks you're typing.  If ambiguous, the shell will list the possibilities.  Please note that this will not complete options for commands.

bash also keeps the last several commands entered in the shell history.  Scroll through recent commands by using the up and down arrows on the keyboard.  You can then edit or enter then command again. To seach the history, use ctrl r and then escape once you have found the command you want to edit. There are many other shortcuts to use on the command line, but these will suffice for anyone short of a power-user.

Between these two features, the number of keystrokes entered on the command line can be greatly reduced.  On the other hand, don't let these features hinder your learning of commands.