A beginner's guide to UNIX/Linux

  1. Introduction
  2. Command line
  3. SSH
  4. File system
  5. Looking around
  6. Managing files and directories
  7. Printing
  8. UNIX shells
  9. Setting up your local environment
  10. Compilers
  11. File permissions
  12. File transfer
  13. I/O redirection

Introduction

UNIX is an operating system developed in the late 1960's. Today there are many different operating systems based on the same principle as UNIX, the best known are Linux and BSD (MAC OSX is based on the latter). These operating systems are popular in scientific communities due to them being excellent to program in. 

UNIX terms

  • process - a program loaded into the computer memory and running on the CPU
  • multitasking - enables multiple programs to be run at the same time by frequently switching between the active processes. Many active processes naturally reduce the computing time of each process and hence increase the CPU "load"
  • swapping - enables more than the actual memory of the computer to be used by adding virtual memory by using diskspace. This can dramatically reduce the computer performance.

Most of the computers at ITA are running Red Hat Enterprise Linux (RHEL) or MAC OS-X. There are also a few computers left running UNIX in addition to a few client computers running Windows.

Command line

A Unix command is generally a short word in lowercase letters. Please note that Unix is case-sensitive! The syntax of most commands is:

> command  -options  [arguments]

Here, "command" is the name of the Unix command; "options" allow you to specify exactly how the command will work; the "argument" is the information that will be effected by command (usually a file or directory name). The parts of a Unix command line must be typed in the order specified above and be separated by spaces. Options are always preceded by a hyphen (also called a flag, "-") and can usually be listed in any order. Many commands do not require you to specify options or file names, because they assume defaults. Commands which do allow options generally offer quite a few. The man command displays information from the Digital UNIX manual -- also called the "man pages." The "man" command requires the name of the command you want more information for as an argument. The "passwd" command lets you change your password To see the "man page" for "passwd," type:

man passwd <ENTER>

Press the space bar to progress through the explanation screen by screen.

The "k" option for the "man" command lists all commands relating to a specified term. To see all commands relating to "password," type:

man -k password <ENTER>

In this example, "man" is the command, "k" is the option, and "password" is the argument. Consult the "man page" on any command to see what arguments and options you can use with that command (there is even a "man page" for the "man" command).

SSH

SSH is a utility program that enables you to connect to other computers over the Internet (or local network).

> ssh -Y username@nekkar.uio.no

The -Y option is useful if you want to open windows on the external computer (the one you're connected to).

File system

UNIX makes use of an inverted branching-tree file structure. The tree trunk consists of a single large file, "/" (called root), which contains all the files on the system. (In this structure, all files which contain other files are called "directories"). Any number of subdirectories may branch from this main trunk. However, the hierarchical structure does not end there, for each major branch may also spawn any number of further branches (sub-subdirectories). These subdirectories may, in their turn, send forth yet more levels of files...and so on. All successive directory names are separated by "/", so you'll find directory names like "/local/astro/bin/".

Looking around

To see in which directory you are, type

> pwd
/mn/mitra/u1/myusername

Listing files (ls)

To show what files are contained in a specified directory, use the "ls" (list files) command. Options for the "ls" command include:

  • -F: displays directory names with a "/", executable files with a "*", links to other files with a "@"
  • -a: shows all files in the directory, including those beginning with "."; also lists . and .. directories
  • -l: displays in long (detailed) format, including information on each file's owner and access privileges (see Section 4, "File and directory permissions," for a discussion of these things).
  • -t: sorts by time, with last-written first

Moving around (cd)

To connect to other directories, use the "cd" (change directory) command. To move to a directory called "/local/doc" (a public directory elsewhere on the system), type:

> cd /local/doc

To return to your home directory from any other location, type:

> cd

Your home directory can also be abbreviated by "~/". To see the file ".cshrc" in your home directory from where ever you may be, type

> cat ~/.cshrc

Reading files (cat and more)

The "cat" and "more" commands display files on the screen. The simplest of these, "cat," lists the entire file without stopping. The "more" command displays a file screen by screen. To read a file in the directory "/local/doc" called "getting-help", type:

> more /local/doc/getting-help

Managing files and directories

Creating directories

To create a subdirectory within your home directory, use the "mkdir" command. For example, to create a directory called Information, type:

> mkdir Information

Copying files (cp)

The cp command makes a copy of a specified file. To copy /usr/local/doc/getting-help to a file in your home directory called myhelp, type:

> cp /local/doc/getting-help myhelp

Note: if the file name you specify for the new file already exists (in this case, "myhelp"), it will be overwritten by the file you are copying; "cp" has an "-i" (interactive) option which will warn you before overwriting files:

> cp -i firstfile secondfile

Renaming and moving files (mv)

A Unix file is renamed by "moving" it from one file name to another using the "mv" command. To rename the file myfile as MyHelpFile, type:

> mv myfile MyHelpFile

You can also move a file from one directory to another. To move the file MyHelpFile to the Information directory, type:

> mv MyHelpFile Information

The "mv" command works exactly like "cp," except that the original file is deleted. Like "cp," "mv" has an "-i" option which will warn you if a file with the name you have indicated (in this case, MyHelpFile) already exists.

Deleting files (rm)

To delete a file or files from a directory, use the "rm" command. Before you start experimenting, however, be aware that "rm" removes files permanently. Unless you are certain that the file is on a system backup (it is older than a week, say, and you have not touched it since), you should treat "rm" with a great deal of caution. To remove the file MyHelpFile from the Information directory, type:

> rm -i Information/MyHelpFile

Deleting directories (rmdir)

The "rmdir" command removes a directory. Only empty directories can be deleted. To remove the directory Information, type:

> rmdir -i Information

Linking files/directories

The "ln" command is used to create a link to an existing file/directory (directories are considered files in UNIX). A link is useful if you wish to access a file from some directory frequently without having to write its entire path each time. Usually you want to create a symbolic link, which is considered a pointer to a file, while a hard link is indistinguishable from the file it points to. The first argument is the file to be pointed to, the second being the name of the link:

> ln -s /mn/procyon/u1/per/myfiles data

You remove a link in the same way you remove a file, using "rm".

Printing

Printing is done using the command ppr. It has various options. Use -P<printername> to specify the printer to use, eg. to print the PostScript file "document.ps" type :

> ppr -Pastro document.ps

Set default printer

If you primarily use one printer (eg. astro) this can be set to default by setting the UNIX variable PRINTER to "astro". This is done by giving the command :

> setenv PRINTER astro

or by adding it to your setup file (see Setting up your local environment for details on how to do this). Furthermore, there are so called X-options for particular printers (eg. astro) allowing the user to specify special features of the printer. The options are given as

> ppr -Pastro -X<option-name>=<value> document.ps 

Other options to ppr are "-D" to specify document type and "-#" to specify number of copies. These and other options are described in the man-pages (man ppr).

To check printer queue type

> ppq -Pastro

 

UNIX shells

A shell is a command interpreter and acts as an interface between the user and the operatingsystem (OS). The shell allows you to give multiple commands on a line

> date ; pwd

or options to programs

> ls -l

or start programs that run in the background

> my-prog &

Furthermore, most shells allow you to control your running jobs/process, edit previous command lines using the cursor keys and complete filenames or commands using the TAB-key (or Ctrl-d). The user is free to choose a shell among the many available ones or write his/her own. Below are listed a few of the most commonly used UNIX shells :

Shell name Command to start Command editing Filename completion Job control Config file
Korn shell ksh yes yes yes .profile
Bourne shell sh no no no .profile
C-shell csh yes yes yes .cshrc
TC-shell tcsh yes yes yes .tcshrc/.cshrc
Bourne again shell bash yes yes yes

.bashrc

Each shell can be configured by the user in the respective configuration files. Tcsh is the default shell at ITA, but this may be changed using the command chsh or bash.

Setting up your local environment

This section assumes you are using the default shell tcsh or csh.

Your account can be setup to your own liking using a few configuration files. By default these files are setup by the system administrator and you should not change them unless you know what you are doing. The files are:

  • .login - setup file for your session
  • .cshrc - setup file for your shell, including environment variables

Usually you might like to add an "alias" or set an environment variable. This can be done by editing the .cshrc file using an editor. To eg. set the default printer to "astro" you will have to set the environment variable "PRINTER" by adding the following line to your .cshrc file :

setenv PRINTER astro

To apply the changes in your .cshrc file, type :

> source ~/.cshrc

You can setup your shell prompt to eg. display the hostname and the two parent-directories of your current directory :

>set prompt="${HOST}:%c2> "

Xdefaults

There is also a file called .Xdefaults which is for setting up your X-windows, eg Emacs. If you are not terribly annoyed by the colours and appearance of your windows then leave this file as it is. For illustration, Emacs-window properties can be setup as in the example below:

Emacs.geometry: 80x60+0-100 Emacs*Font: -adobe-courier-medium-r-normal--12-120-75-75-m-70-iso8859-1 Emacs*SelectionBackground: purple 
Emacs*SelectionForeground: Gray93 Emacs*background: Gray Emacs*borderColor: Black Emacs*cursorColor: Black Emacs*foreground: Black 
Emacs*modeline.AttributeBackground: rgb:69/20/ac Emacs*modeline.AttributeForeground: gold Emacs*pointerColor: ForestGreen Emacs*selectionFont: -adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859- 1

Compilers

A program is written using an editor and is sub-sequently compiled using a compiler. A compiler translates the program into executable binary machine-code. A forest of programming languages exist and compilers for many of the most frequently used are available at ITA.

Compilors Command
C (GNU) gcc
C (Digital) cc
C++ (GNU) c++
Fortran 95 (Digital) f95
Perl (interpreter) perl

For example, to compile a simple C program :

#include <stdio.h>
main()
{
  printf("hello, world !\n");
}

you would write :

> cc -o hello hello.c

and running the executable program :

> hello
hello, world !
>

File permissions

File permissions can be set for files which are owned by the user. To view the file permissions we use the ls-command with the long-listing option :

> ls -l aFile
-rw-r--r--   1 jdoe     staff         406 Jan  6 14:17 aFile

The information contained in the file listing has the following meaning :

 Looking more closely now at the permission string :

The permission string consists of 10 bits, the first being file-type (d=directory, -=file, s=symbolic link, and various other device types). The three next bits show the read, write and execute permissions set for the owner (in this case "jdoe"). The next three bits show the permissions for the group (in this case "staff") and the last three bits represent the permissions for others (other users than the owner and those defined in the group). The permissions of a file can be set using

> chmod u=rw aFile; chmod go= aFile

or alternatively

> chmod 0600 aFile

The first method is intuitive, the second numeric method give the permissions for file ID, user, group and others using the added bits of read(4), write(2) and execute(1). The file permission then becomes

-rw-------   1 jdoe     staff         406 Jan  6 14:17 aFile

To add eg. execute permission for the owner and group use

> chmod ug+x myfile

giving

-rwx--x---   1 jdoe     staff         406 Jan  6 14:17 aFile

File transfer

To transfer files between machines it is neccessary for invoke a program which can communicate with the host-machine using a specified protocol. The dominantly used protocol is ftp (file-transfer-protocol). Ftp allows the user to both send and recieve files using simple intutive commands. A connection is invoked in the following way

mirfak> sftp procyon.uio.no

A list of the available commands is obtained by using "?". Commands may be abbreviated.  Commands are:

!               debug           mget            put             send
$               dir             mkdir           pwd             site
account         disconnect      mls             quit            size
append          form            mode            quote           status
ascii           get             modtime         recv            struct
bell            glob            mput            remotehelp      sunique
binary          hash            nmap            rstatus         system
bye             help            nlist           rhelp           tenex
case            image           ntrans          rename          trace
cd              lcd             open            reget           type
cdup            ls              pipe            restart         user
close           macdef          prompt          reset           verbose
cr              mdelete         proxy           rmdir           ?
delete          mdir            sendport        runique

Most commands are identical to UNIX-commands, eg.

  • cd -- change directory on the host
  • lcd -- change director locally
  • ls -- list files on host
  • !ls -- execute a shell command, in this case ls

Retrive a file using get

sftp> get file01

Multiple files are downloaded using mget and wildcards. Send a file using put or mput.

sftp> mput myfile*

Quit ftp using bye or quit.

I/O redirection

I/O redirection and piping are two useful features of UNIX. The following special characters are used when connecting commands together or directing output: character description

  • > - directs command output to a file
  • >> - directs command output to an existing file
  • <  - directs command input coming from a file
  • |  - pipes the output from one command into another command
  • >& - directs command errors to a file

This UNIX feature is very convenient and allows the user to handle files/data in the shell. Here are a few examples :

> ls /usr/man/man1

Contents of the directory /usr/man/man1 are displayed.

> ls /usr/man/man1 | more

Contents are displayed pausing for each screenful.

> ls > filelist
> wc -w < filelist
10
> ls | wc -w
10
> ls /local/bin >> filelist
> ls -1 | sort | ppr -Pastro
> uncompress -c /astro/local/mpi/doc/mpiman.ps.Z | ghostview -
> tail -n +10  < filelist | awk '{printf("%s\n", $5)}'
Published June 22, 2011 3:53 PM - Last modified Feb. 24, 2012 2:04 PM