UNIX, and UNIX-like operating systems such as Linux, consist of a large collection of tools and utilities, each of which performs a single function extremely well. A large number of the tasks you will be performing here involve interacting with a UNIX command-line interpreter, called a UNIX shell. In many cases, even though it may be possible to perform these tasks through a GUI, you may find that, with a little practice, you are able to do so more quickly and efficiently via a terminal shell.
In this lesson you will
- Familiarize yourself with and differentiate between the three UNIX shells most commonly used for LinkSCEEM computing.
- Familiarize yourself with the UNIX command line and some basic commands, tools, and syntax that will be useful later for getting around and manipulating files and data on LinkSCEEM resources. Many of these commands will be discussed in more detail in later sections of this tutorial.
This lesson is not intended to be a complete introduction to the UNIX shell. There are a large number of excellent resources both in print and online that serve this purpose. See the Resource section of this chapter for a list of recommended references.
Currently, while using the LinkSCEEM resources you may encounter one of five different, commonly-used UNIX shells.
- bash (short for Bourne-Again Shell) is the default shell for most UNIX-like systems. You will find it on Mac OS 10.4 and 10.5, as well as on Windows terminal emulators like PuTTY.
- csh (C-shell), modeled after the C programming language, is no longer as widely used on UNIX and Linux OSs, but provided many of the features that are now standard in most UNIX shells.
- tcsh (the TENEX C shell, pronounced "tish") is a modified C shell that incorporates several features of TENEX, another UNIX-like operating system, such as filename completion and command line editing. It was used on Mac OS X prior to version 10.4 and is still the default shell for many OSs descended from FreeBSD.
- sh (Bourne Shell) was the default Unix shell of Unix Version 7. Most Unix-like systems continue to have /bin/sh—which will be the Bourne shell, or a symbolic link or hard link to a compatible shell—even when other shells are used by most users.
- ksh (Korn Shell) is a Unix shell which was developed by David Korn at Bell Labs in the early 1980s. KornShell is backward-compatible with the Bourne shell and includes many features of the C shell, inspired by the requests of Bell Labs users.
While some tools and utilities may differ from shell to shell, the basic commands remain the same.
Some UNIX commands can be used alone as a single word.
- ls : lists the contents of the user's current directory.
- finger : tells you who else is currently signed into the server, how long they've been signed on, etc.
A typical UNIX command looks like this:
For example, the following command provides a long, detailed list of the contents of the user's public_html directory:
This command has three elements.
- The first, the base command, is required: ls, which lists the items in the directory.
- The second and third are not necessary for the command to run without an error message, but they provide more information about what the command should do:
- The option or flag (-l), which usually begins with a hyphen, specifies that the list should be long format (i.e., detailed). Other options can be used:
- -a shows all hidden files (names begin with a . )
- -i shows all symbolic links
- The argument (~/public_html) is the directory, file, or other object on which the command is acting. In this case, ~ stands for the user's home directory, and /public_html is a subdirectory.
In this section we introduce you to some of the basic UNIX commands you'll need to get around in the shell and perform basic tasks that will be referenced in later sections of this tutorial.
UNIX utilizes two different kinds of commands:
- UNIX commands, which are shell-independent and the same across all distributions
- shell commands or built-ins, which may differ depending on which shell you're using
The info command can be used to find out whether a command is a UNIX command or a built-in. If the command is a built-in, it will say so:
If the command is a builtin, it will say so at the top of the entry.
There are several ways to get information about specific commands and utilities in the shell.
- man <command> will bring up the man (manual) page for a given UNIX-wide command. Man pages describe what a command does in detail, its usage, and options you can specify to modify its parameters.
- help <command> describes built-in utilities.
- whereis <command> locates directory paths for specific commands and executables (such as shell scripts).
Other useful commands are:
- cd changes directories
- cd.. takes you to the parent directory
- pwd prints, or outputs to the command line, the path of the current working directory, which can help prevent you from getting lost in the directory structure.
- ls displays a list of just the filenames and subdirectories in your current directory.
- ls <directory> lists the contents of the directory
- ls -l displays a long (or detailed) list of all files and subdirectories and information about their size, when they were last modified, and who has permission to read, change, or execute them.
- ls -a lists hidden files (usually begining with a . ,)
- cat writes the contents of an entire file to standard input or output (the command line), which can be useful for looking to see what is in a file without having to open it up in an editor or running it. For longer files, the command cat [~tloizou:filename] | more breaks up the file into screens which you page through using the spacebar.
- cp copies one file to another location: cp <file1> <directory> or duplicates it under a different filename: cp <file1> <file2>
- rm deletes files or directories
- rm -r removes the entire directory
tar (tape archive) and gzip
While you can read and write files to a tape archive using tar, it is also useful for creating and extracting from tar files, which bundle a group of files into a single file called a tarball.
The most common use of tar is
The -c option creates a tarball; the -x option extracts from a tar ball. These options are generally used together with -v (verbose, where all files in the tarball are listed one by one, and -f indicates the file that follows, to or which tar will write or read.
tar is often used in conjunction with gzip, which compresses a tarball. Use -z to compress or uncompress a gzipped tarball (which can be identified by the extension .tgz).
If you are copying, moving, deleting, or performing an action on a large number of related files with similar names, a wildcard character can provide a shortcut:
moves the c program product.c and related files product and product.out to a directory called product. The shell reads (or expands) product* into any set of files whose names begin with the word "product" and can be used to stand for any combination and number of characters except for a leading period.
" * " is probably the most commonly used wildcard character, but others include ? (any single character) or [~tloizou:set], in which set can be any single character included within the brackets.
There are a large number of text editors from which you can choose to edit files in a Unix shell. They range from easy to learn and user-friendly to feature-rich. Here we list three of the most common.
- nano - A very simple text editor, with an interface somewhat similar to graphical user interface editors such as Notepad. It has text search capabilities, but is less useful for searching through scripts for regular expressions. nano-like editors include joe and pico.
- vi - the current standard Unix editor. It is modal, in that the user switches between insert mode (text input) and command mode (which enables users to navigate through a file, edit, search and replace, and perform many other more complex editing tasks. While getting accustomed to switching between modes and learning commands may take some time, with practice Unix users find that vi is a very useful, powerful, lightweight editor.
- Emacs - A feature-rich text editor also included with most Unix distributions. More initially user-friendly and intuitive (does not require switching between modes) and customizable than vi, it is larger and requires more memory and more startup time, but also includes significantly more commands and the ability to create and use macros.
- List of Unix utilities (Wikipedia) – Reference in table form, with links to entries on most basic Unix utilities.
- Rosetta Stone for Unix – Provides basic commands and their counterparts in many different flavors of Unix, Linux, and other *nix-like operating systems.
- Unix Primer – Basic Commands in the Unix Shell(University of Illinois) Good introductory Unix tutorial.
- Unix shells (Wikipedia) – Provides an overview of Unix shells with links to those most likely to be encountered.
- Unix Tutorial for Beginners (University of Surrey) – Eight tutorials covering the basics from listing files and directories to creating shell scripts. May be downloaded for offline use.