Newer
Older
labs / tiddlers / content / labs / 01 / _Labs_01_Command-Line Interface (CLI).md

{{/Labs/01/terminal.png}}

Linux supports modern desktop-style graphical user interfaces, but it also provides a powerful textual shell or command-line interface (CLI), which we will use fairly heavily in this paper. Such a command-driven interface might seem somewhat old-fashioned, but it has a number of benefits:

  • Commands can be precisely and concisely expressed as text.
  • Commands can easily be recalled and modified during testing and development.
  • Commands can easily be shared and replayed by yourself or others.
  • Groups of commands can be saved in scripts or functions for flexible reuse.
  • General programming features (variables, loops, conditionals, functions, etc.) are also available, allowing powerful automation of tasks.
  • Input and output can be redirected to/from files, using the > and < operators.
  • Commands can be combined into pipelines (where the output of one program is used as the input to another) for more complex operations. This is done using the | operator.

If you have not used the Linux (or any) CLI before, you might need to read this section (and seek out further reading), and get some practice using the commands.

Technically, the commands are initially parsed and processed by the shell, commonly bash, the "Bourne-again shell". Interactive shell input and output is handled by software such as Sakura or xterm that emulates an old-style terminal.

The basic interaction for the CLI is as follows:

  • If the prompt (usually $) is shown with the text cursor following it, the shell is ready for your input.
  • Compose a line of text, and press Enter (or Return) to run it. The command name itself will be the first element, and other arguments such as settings flags or input/output file names may follow.
  • If the command runs successfully, you might or might not see any output. Some commands (such as mkdir for creating a new directory or folder) work entirely by side-effects and do not normally produce any output.
  • If there was an error, a message will normally be printed. Read and try to understand the error before proceeding.
  • Whitespace is used to separate keywords (tokens) on the command line. Files or folders with names containing spaces can cause problems. You may have to resort to (single- or double-) quoting them or using the escape character, \, before the space.

Some common, basic commands you should know about:

  • ls to obtain a list of files in the current folder (or elsewhere).
  • cd to change directory (folder), i.e. navigate to a different place within the filesystem.
  • mkdir to create a new directory.
  • cat to print or concatenate the contents of a file or files.
  • less, a "pager" program used to view a file with the ability to navigate forwards and backwards.
  • file to identify what type a file is, based on its contents.
  • mv to move or rename a file or directory.
  • touch to create a new empty file or update the timestamp on an existing one.
  • rm and rmdir to delete a file or directory respectively.

(Yes, the shell command names do tend to be abbreviated to the point of being cryptic to new users! We will introduce more as the course proceeds.)

Some useful command-line tips and tricks:

  • The mouse will be of limited use, so get used to keeping your hands over the keyboard. The keyboard does provide some useful shortcuts, however.
  • Use the Tab key to auto-complete the names of files, folders and commands. This helps avoid typos and misspellings, and is also a useful time-saver. You might have to press Tab repeatedly in some situations (e.g. if there are multiple matches).
  • Use the left and right arrow keys to move the cursor by character on the current command line.
  • Use Alt + left/right arrow to move the text cursor by word.
  • Ctrl-A and Ctrl-E will jump the cursor to the start and end of the line respectively.
  • Ctrl-W will delete the preceding word.
  • The shell maintains a command history. Use the up and down arrow keys to navigate the history.
  • Copy and paste work a little differently. Ctrl-C is already reserved for the kill signal
  • Use Ctrl-D to signal end-of-file to a command that expects text input.
  • Use Ctrl-C to interrupt or kill a process that might be stuck (sends the SIGINT signal).
  • Use Ctrl-Z (SIGSTOP signal) to suspend the current process (if any), and bg to have it continue running in the background. You can also use & at the end of the command line to have it run in the background immediately. Use fg to bring it back to the foreground (for further interaction).

Getting Around

Linux uses a hierarchical filesystem, in which files and other filesystem objects reside within containers known as directories. Directories can be nested within other directories, which provides great flexibility in organising large numbers of files while avoiding duplication of file names (and allowing files with the same name in different directories).

(Directories are actually implemented as files, but you can generally think of them as equivalent to folders within the more modern desktop computer metaphor. Many of the commands that operate on ordinary files can also operate on directories.)

Objects in the filesystem are identified by their path, which is a list of the directories (folders) traversed to locate the file, followed by the filename itself. For example, your home folder might resemble the path expression /home/bonja007.

To list the items within the current directory, use the ls command.

The Linux filesystem also contains some unconventional objects that manifest themselves as file-like structures, such as files in the /dev directory that correspond to hardware devices, and FIFOs (first-in-first-out objects) that facilitate inter-process communication (a bit like pipes).

There are some special directories worthy of note:

  • / is the top-level root directory, the directory under which all other filesystem objects exist.
  • ~ is shorthand for your home directory.
  • . refers to the current directory.
  • .. refers to the parent directory (making it easy to navigate upwards towards the root).
  • /tmp is the system-wide location for temporary files (note that the lab environment provides per-user temporary folders).

There are two main types of path expression:

Absolute paths are those that show the location of the object with respect to the root directory, i.e. they start with a /, and show the full path to the object. Relative paths refer to a location relative to the current directory, using some combination of ., .., and file/directory names. These are generally shorter and are useful when referring to nearby items irrespective of the beginning location within the filesystem.

File and directory names may also begin with a .. These "dot-files" are commonly used for settings files, and are hidden by default by many tools. Use the -a flag with the ls command to show hidden items.

Much like a graphical file browser, the shell maintains the notion of the current directory, i.e., the path to the folder that you are currently working in. You can move around the filesystem using the cd (change directory) command, e.g.

When you `cd` with no argument, you will be moved to your home directory.

You can use the `pwd` command to print the current working directory (though it will usually be shown as part of the command prompt).


## Environment Variables

Stored within the working memory of the shell is a set of variables known as *environment variables* ("env vars"). These are conventionally written in all caps, and can be used for a variety of purposes. You can obtain the value of an environment variable with the `$` prefix; for example, to print the current working directory without using the `pwd` command:

```echo $PWD

Many environment variables have standardised meanings:

  • $HOME stores the path to the session's home directory.
  • $TEMP stores the path to the preferred location for temporary filesystem objects.
  • $USER stores the name of the current user.
  • $PWD stores the present working directory of the shell.
  • $HOSTNAME stores the name of the computer the shell is running on.
  • $PATH stores the paths to search for an executable file when a command is run.

Typically the shell will print some of these in the status line as part of the prompt. This can be customised through further environment variables such as $PS1.

You can view all the variables in your current shell using the set command with no arguments.

To set the value of an environment variable in the standard bash shell, use the = operator, e.g.

To make the variable available to commands and subshells run from the initiating shell session, use the `export` command, e.g.

```export MYNAME=Chris

You can remove a variable using unset:


## Getting Help

The system documentation on Linux is based around the *manual pages* infrastructure. The main front-end commands for this are `man` for viewing a particular manual page, and `apropos` for finding manual pages that relate to some keyword. For example:

```apropos delete

man rmdir

Many of the GNU tools provide their system documentation through the info command, either instead of or in addition to the manual pages, which provides a navigable hypertext system.

Graphical applications will generally provide their own help, often simply by opening a Web browser at a particular URL.

You can use the which command to see the full path to a command, which can be useful for troubleshooting.

Of course, you can also consult Stack Overflow/SuperUser/ServerFault, the Linux HowTo series, Google Web Search, etc.