Logo of Science Foundation Ireland  Logo of the Higher Education Authority, Ireland7 CapacitiesGPGPU Research Projects
Ireland's High-Performance Computing Centre | ICHEC
Home | News | Infrastructure | Outreach | Services | Research | Support | Education & Training | Consultancy | About Us | Login

Unix tutorial

What is UNIX?

UNIX is an operating system. The job of an operating system is to orchestrate the various parts of the computer to perform useful tasks. The operating system is the master controller of the computer, the glue that holds together all the components of the system, including the administrators, programmers, and users. When you want the computer to do something for you, like start a program, copy a file, or display the contents of a directory, it is the operating system that must perform those tasks for you.

UNIX was created in the late 1960s, in an effort to provide a multiuser, multitasking system for use by programmers. The philosophy behind the design of UNIX was to provide simple, yet powerful utilities that could be pieced together in a flexible manner to perform a wide variety of tasks.

The UNIX operating system comprises three parts: The kernel, the standard utility programs, and the system configuration files.

The kernel

The kernel is the core of the UNIX operating system. Basically, the kernel is a large program that is loaded into memory when the machine is turned on, and it controls the allocation of hardware resources from that point forward.

The standard utility programs

These programs include simple utilities like cp, which copies files, and complex utilities, like the shell that allows you to issue commands to the operating system.

The system configuration files

The system configuration files are read by the kernel, and some of the standard utilities. The UNIX kernel and the utilities are flexible programs, and certain aspects of their behavior can be controlled by changing the standard configuration files. One example of a system configuration file is the filesystem table "fstab" , which tells the kernel where to find all the files on the disk drives. Another example is the system log configuration file "syslog.conf", which tells the kernel how to record the various kinds of events and errors it may encounter.

Getting Started

Listing files and directories

ls (list)

When you first login, your current working directory is your home directory. Your home directory has the same name as your user-name, for example gkinsella, and it is where your personal files and subdirectories are saved.

To find out what is in your home directory, type

% ls

ls does not, in fact, cause all the files in your home directory to be listed, but only those ones whose name does not begin with a dot (.) Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information. They are hidden because you should not change them unless you are familiar with UNIX!!!

To list all files in your home directory including those whose names begin with a dot, type

% ls -a

ls is an example of a command which can take options: -a is an example of an option. The options change the behaviour of the command. There are online manual pages that tell you which options a particular command can take, and how each option modifies the behaviour of the command. (See later in this tutorial)

Making Directories

mkdir (make directory)

To make a subdirectory called stuff in your current working directory type

% mkdir stuff

To see the directory you have just created, type

% ls

Changing to a different directory

cd (change directory)

The command cd directory means change the current working directory to "directory". The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.

To change to the directory you have just made, type

% cd stuff

Type ls to see the contents (which should be empty)

Exercise

Make another directory inside the stuff directory called backups

The directories . and ..

Still in the stuff directory, type

% ls -a

As you can see, in the stuff directory (and in all other directories), there are two directories called . and ..

In UNIX, . means the current directory, so typing

% cd .

Note: there is a space between cd and the dot means stay where you are (the stuff directory).

This may not seem very useful at first, but using . as the name of the current directory will save a lot of typing, as we shall see later in the tutorial.

.. means the parent of the current directory, so typing

% cd ..

will take you one directory up the hierarchy (back to your home directory). Try it now.

Note: typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system.

Pathnames

pwd (print working directory)

Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your current-directory type:

% pwd

The full pathname will look something like this:

/ichec/home/staff/gkinsella

Exercise

Use the commands ls, pwd and cd to explore the file system.

(Remember, if you get lost, type cd by itself to return to your home-directory)

More about home directories and pathnames

Understanding pathnames

First type cd to get back to your home-directory, then type

% ls stuff

to list the contents of your stuff directory. Now type

% ls backups

You will get a message like this:

backups: No such file or directory

The reason is that backups is not in your current working directory. To use a command on a file (or directory) not in the current working directory (the directory you are currently in), you must either cd to the correct directory, or specify its full pathname. To list the contents of your backups directory, you must type

% ls stuff/backups

~ (your home directory)

Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing

% ls ~/stuff

will list the contents of your stuff directory, no matter where you currently are in the file system.

Creating a File

If you are not familiar with a Unix editor, please look at the Emacs tutorial or Vi tutorial.

Generate a file (file1) with a few lines of text.

Copying Files

cp (copy)

cp file1 file2 is the command which makes a copy of file1 in the current working directory and calls it file2

Moving files

mv (move)

mv file1 file2 moves (or renames) file1 to file2

To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.

It can also be used to rename a file, by moving the file to the same directory, but giving it a different name.

Removing files and directories

rm (remove), rmdir (remove directory)

To delete (remove) a file, use the rm command.

Exercise

Create a directory called tempstuff using mkdir , then remove it using the rmdir command.

Displaying the contents of a file on the screen

clear (clear screen)

Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood.

At the prompt, type

% clear

This will clear all text and leave you with the % prompt at the top of the window.

cat (concatenate)

The command cat can be used to display the contents of a file on the screen. Type:

% cat file1

less

The command less writes the contents of a file onto the screen a page at a time. Type

% less file1

Press the [space-bar] if you want to see another page, type q if you want to quit reading. As you can see, less is used in preference to cat for long files.

head

The head command writes the first ten lines of a file to the screen.

First clear the screen then type

% head file1

Then type

% head -5 file1

What difference did the -5 do to the head command?

tail

The tail command writes the last ten lines of a file to the screen.

Clear the screen and type

% tail file1

How can you view the last 15 lines of the file?

wc (word count)

A handy little utility is the wc command, short for "word count". To do a word count on your file type

% wc -w file1

To find out how many lines the file has, type

% wc -l file1

Redirecting the Output

We use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of fruit, type

% cat > list1

Then type in the names of some fruit. Press Return after each one.

orange
banana
apple
^D (Control D to stop)

What happens is the cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called list1

To read the contents of the file, type

% cat list1

Exercise

Using the above method, create another file called list2 containing the following fruit: melon, plum, mango, grapefruit. Read the contents of list2

The form >> appends standard output to a file. So to add more items to the file list1, type

% cat >> list1

Then type in the names of more fruit

peach
grape
orange
^D (Control D to stop)

To read the contents of the file, type

% cat list1

You should now have two files. One contains six fruit, the other contains four fruit. We will now use the cat command to join (concatenate) list1 and list2 into a new file called biglist. Type

% cat list1 list2 > biglist

What this is doing is reading the contents of list1 and list2 in turn, then outputing the text to the file biglist

To read the contents of the new file, type

% cat biglist

Redirecting the Input

We use the < symbol to redirect the input of a command.

The command sort alphabetically or numerically sorts a list. Type

% sort

Then type in the names of some vegetables. Press Return after each one.

carrot
turnip
broccoli
^D (control d to stop)

The output will be

broccoli
carrot
turnip

Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of fruit, type

% sort < biglist

and the sorted list will be output to the screen. To output the sorted list to a file, type,

% sort < biglist > slist

Use cat to read the contents of the file slist

Pipes

To see who is on the system with you, type

% who

One method to get a sorted list of names is to type,

% who > names.txt
% sort < names.txt

This is a bit slow and you have to remember to remove the temporary file called names when you have finished. What you really want to do is connect the output of the who command directly to the input of the sort command. This is exactly what pipes do. The symbol for a pipe is the vertical bar |

For example, typing

% who | sort

will give the same result as above, but quicker and cleaner. To find out how many users are logged on, type

% who | wc -l

Wildcards

The characters * and ?

The character * is called a wildcard, and will match against none or more character(s) in a file (or directory) name. For example, in your stuff directory, type

% ls list*

This will list all files in the current directory starting with list... Try typing

% ls *list

This will list all files in the current directory ending with ...list. The character ? will match exactly one character. So ls ?ouse will match files like house and mouse, but not grouse. Try typing

% ls ?list

Filename conventions

We should note here that a directory is merely a special type of file. So the rules and conventions for naming files apply also to directories.

In naming files, characters with special meanings such as / * & % , should be avoided. Also, avoid using spaces within names. The safest way to name a file is to use only alphanumeric characters, that is, letters and numbers, together with _ (underscore) and . (dot).

File names conventionally start with a lower-case letter, and may end with a dot followed by a group of letters indicating the contents of the file. For example, all files consisting of C code may be named with the ending .c, for example, prog1.c . Then in order to list all files containing C code in your home directory, you need only type ls *.c in that directory.

Beware: some applications give the same name to all the output files they generate.

For example, some compilers, unless given the appropriate option, produce compiled files named a.out. Should you forget to use that option, you are advised to rename the compiled file immediately, otherwise the next such file will overwrite it and it will be lost.

Getting Help

On-line Manuals

There are on-line manuals which gives information about most commands. The manual pages tell you which options a particular command can take, and how each option modifies the behaviour of the command. Type man command to read the manual page for a particular command. For example, to find out more about the wc (word count) command, type

% man wc

Alternatively

% whatis wc

gives a one-line description of the command, but omits any information about options etc.

File system security (access rights)

In your unixstuff directory, type

% ls -l

You will see that you now get lots of details about the contents of your directory, similar to the example below.

Each file (and directory) has associated access rights, which may be found by typing ls -l (-l for long listing). Also, ls -lg gives additional information as to which group owns the file (ichec in the following example):

-rw-r--r-- 1 gkinsella ichec 25 Aug 10 12:20 file1

In the left-hand column is a 10 symbol string consisting of the symbols d, r, w, x, - and occasionally s or S. If d is present, it will be at the left hand end of the string, and indicates a directory: otherwise - will be the starting symbol of the string.

The 9 remaining symbols indicate the permissions, or access rights, and are taken as three groups of 3.

  • The left group of 3 gives the file permissions for the user that owns the file (or directory) (gkinsella in the above example);
  • the middle group gives the permissions for the group of people to whom the file (or directory) belongs (ichec in the above example);
  • the rightmost group gives the permissions for all others.

The symbols r, w, etc. have slightly different meanings depending on whether they refer to a simple file or to a directory.

Access rights on files.

  • r (or -), indicates read permission (or otherwise), that is, the presence or absence of permission to read and copy the file
  • w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to change a file
  • x (or -), indicates execution permission (or otherwise), that is, the permission to execute a file, where appropriate

Access rights on directories.

  • r allows users to list files in the directory;
  • w means that users may delete files from the directory or move files into it;
  • x means the right to access files in the directory. This implies that you may read files in the directory provided you have read permission on the individual files.

So, in order to read a file, you must have execute permission on the directory containing that file, and hence on any directory containing that directory as a subdirectory, and so on, up the tree.

Some examples

-rwxrwxrwx a file that everyone can read, write and execute (and delete).
-rw------- a file that only the owner can read and write - no-one else can read or write and no-one has execution rights (e.g. your mailbox file).

Changing access rights

chmod (changing a file mode)

Only the owner of a file can use chmod to change the permissions of a file. The options of chmod are as follows:

Symbol Meaning
u user
g group
o other
a all
r read
w write (and delete)
x execute (and access directory)
+ add permission
- take away permission

For example, to remove read write and execute permissions on the file biglist for the group and others, type

% chmod go-rwx biglist

This will leave the other permissions unaffected. To give read and write permissions on the file biglist to all,

% chmod a+rw biglist

Exercise

Try changing access permissions on the file file1 and on the directory backups.

Use ls -l to check that the permissions have changed.

Command Summary

ls list files and directories
ls -a list all files and directories
mkdir make a directory
cd directory change to named directory
cd change to home-directory
cd ~ change to home-directory
cd .. change to parent directory
pwd display the path of the current directory
cp file1 file2 copy file1 and call it file2
mv file1 file2 move or rename file1 to file2
rm file remove a file
rmdir directory remove a directory
cat file display a file
more file display a file a page at a time
head file display the first few lines of a file
tail file display the last few lines of a file
wc file count number of lines/words/characters in file
command > file redirect standard output to a file
command >> file append standard output to a file
command < file redirect standard input from a file
command1 | command2 pipe the output of command1 to the input of command2
cat file1 file2 > file0 concatenate file1 and file2 to file0
sort sort data
who list users currently logged in
* match any number of characters
? match one character
man command read the online manual page for a command
whatis command brief description of a command

References