You are on page 1of 8

Essentials for Scientic Computing: Introduction to UNIX and Linux Day 1

Ershaad Ahamed TUE-CMS, JNCASR

May 2012

Operating Systems

Computers consist of one or more CPUs, memory and peripheral devices. The CPU fetches instructions from memory and executes them. When a computer boots up, the rst instructions that it fetches and executes is from the BIOS. These instructions direct the CPU in various stages to eventually load the operating system and execute it. From that point onwards the operating system (or specically the kernel) runs at all times and has the following functions. 1. Hardware abstraction 2. Resource management and multitasking 3. Standardised interfaces

1.1

Hardware Abstraction

The manner in which hardware is instructed to perform their specic functions is highly dependent on the hardware itself. This in turn depends on the manufacturer, design and even the specic revision of the hardware in question. In case we didnt have an OS, every programmer would have to know the exact details of how to interact with the specic hardware on which their program will run whenever they write a program.

1.2

Resource Management and Multitasking

On a computer which is running an operating system that supports multitasking (which includes most modern OSes), several programs appear to be running simultaneously although there may be only one or a few processors present. This is called multitasking and is achieved by running each program for a short amount of time on the CPU and quickly switching between them, giving the user the illusion that all programs are executing concurrently. Multiple programs running also means that they are sharing common resources such as memory and even devices such as the video display. It is the operating system that manages this sharing of resources and prevents rogue programs from disrupting 1

the functioning of other programs especially by protecting each programs share of memory from other running programs.

1.3

Standardised Interfaces

Programs or applications interact with the operating system by invoking functions provided by the operating system called system calls. For example when an application needs to open a le, it invokes a specic system call with appropriate parameters. The operating system then performs hardware specic tasks that will eventually make the data of the le available to the application program. The specic names, parameters that need to be passed and return values of these system calls constitutes the Application Programming Interface (API) presented by the operating system to an application programmer. By standardising these APIs, an application programs source code can be compiled unmodied on all operating systems that present the same API regardless of the specic vendor or version of the operating system and independent of the hardware it is running on.

UNIX, Linux and the GNU Project

In the early 1970s, Ken Thompson, Dennis Ritchie and others began to design and develop the UNIX operating system at AT&T Bell Labs. Work on UNIX development continued into the 80s and by the 1990s several variants of the UNIX operating system were developed by various entities, a notable one being the Berkeley Software Distribution (BSD). The large number of variants eventually led to the creation of the IEEE POSIX standard which initially dened the API, and eventually all aspects of the behaviour of a UNIX-like system including commands, utilities and standard libraries. POSIX then evolved into what is now the Single Unix Specication (SUS). In 1983, Richard Stallman initiated the GNU project aiming to create a UNIX compatible software system consisting of free software. Free software can be modied, copied and redistributed without restriction. Several licenses qualify for designation as free software. The GPL (GNU General Public License) is the license adopted by the GNU project. In essence it dictates that the source code of any software released under the GPL should be available without restriction and that any modied or derivative software will also be governed by the GPL. Linux began as a hobby kernel by Linus Torvalds in 1991. It aimed to be POSIX compliant and was licensed under the GPL. The combination of Linux kernel and GNU project software is what is now popularly known as Linux, although it is more accurate to refer to it as GNU/Linux. Fedora, Ubuntu, SUSE etc. are all distributions of the GNU/Linux operating system. Software from the GNU project is extensive and includes everything from compilers for a large number of languages to editors to complete desktop graphical user interfaces (GUIs).

Users and Groups

UNIX was designed around the concept of multiple users. Each user on a system owns a set of les. The owner can dene whether other users may view, modify or execute (if they are scripts or executables) their les. Users may be divided into sets called groups. A user may be a member of several groups. Groups allow users to allow specic permissions to members of a group to which they belong. In UNIX, users and groups are identied by names which are mapped to unique user and group ids respectively. Any le on disk has a user and group id associated with it, and this denes its ownership. As special user exists on UNIX systems called the root user. The root user has access to all le and functions on the system. A person logs in as the root user in order to perform system administration tasks such as conguring a network or changing operating system policy and settings. The root user can edit les critical to the functioning of the system and can delete all les on the system, while normal users can only edit les that are owned by them or for which they are given write permissions.

Processes

Once the programmer has written the source code in a programming language (like C or FORTRAN), it is compiled into machine code that is ready to be executed. This machine code executable when stored on disk is called a program. When a program is executed, the operating system reads it from disk and loads all or parts of the program into main memory. From there it initiates the execution of these instructions by the CPU and the program becomes a process. Each process in a UNIX system is identied by a process ID. A process may have a limited lifetime or may run for the entire duration the system is up. processes may be in a running, stopped or sleeping state.

Files (from the point of view of processes)

A process may open a number of les for input or output. In the UNIX design les need not necessarily need to be data stored on disk. They may be a network device or even a sound device. This is one of the abstractions built into the UNIX design. The operating system can present any device (real or virtual) to an application program as a regular le. Three les are always opened whenever a process is started. They are the standard input, standard output and standard error (stdin, stdout and stderr). these les do not exist on disk.

Directory Layout, Pathnames and Symbolic Links

In UNIX all les are stored within directories (folders). UNIX uses a hierarchical directory structure similar to other operating systems. In UNIX all directories are subdirectories of the uppermost level root directory represented by /. 3

The / is also the symbol for separating elements in a pathname. An absolute pathname species how to reach a particular le or directory from the root directory. For example,. /home/user/thesis.txt

6.1

Relative and Absolute Pathnames

If a pathname is specied without a leading /, then it is a relative path. Relative pathnames are interpreted as relative to the current working directory. Picture it as an absolute pathname that is constructed by joining the current working directory and the relative pathname. Every directory has two special subdirectories named . and ... . is equivalent to the current working directory and .. is equivalent to the parent of the current working directory.

6.2

Symbolic Links

Symbolic links are les on a disk that point to other les (shortcuts to Windows users). Deleting a symbolic link does not delete the le that it points to. Symbolic links take up very little actual space on the disk. One use of symbolic links is the make a direct link to a le that has a very long pathname. Symbolic links are created with the ln command.

7
7.1

Basic Commands
The Bash Shell

In operating system, the shell is the interactive interface presented to the user. The user interacts with the shell in order to achieve specic tasks, and the shell in turn communicates with the operating system kernel. The shell is thus a layer of abstraction that hides details of the underlying operating system and presents a uniform and convenient way for the user to interact with the OS. The most common text mode shell for Linux based operating systems is the Bash shell. The Bash shell presents the user with a programmable interface along with several built-in commands. If you need to get a shell on a Linux system running a graphical front end, you usually need to open the terminal menu item. The Bash shell presents a prompt to the user when it is ready to accept commands. A user prompt may look like the following. ershaad@strobe:~$ or [ershaad@n138 ~]$ And a root prompt will be similar to root@strobe:~# or [root@n138 ~]# 4

Notice the # at the end of the prompt for the root user. As we mentioned above, the root user can aect the working of the system and therefore, this account should be used with caution.

7.2

ls Command and the Current Working Directory

The ls command lists the contents of the current working directory. The current working directory for a session of the shell can be found by entering the pwd command. Files that begin with a period . are hidden les and do not appear in the output of the ls command. In order to view all les including hidden les pass the -a command line option to ls ls -a. Command line options may have a long and a short form. The corresponding long option for -a is --all, so that you would type ls --all to see the full listing of les. Short options can be strung together or supplied separately. Like ls -l -t or ls -lt. The same cannot be done with long options. Both long and short options can be used in the same command ls -t --all. Most commands usually print a usage summary when passed either of the -h or --help options. In order to get detailed usage of a command, the man page can be referred. It is an online documentation that can be viewed from the shell. To view the man page of the cp command simply type man cp at the shell prompt. The rst few lines of the cp man page looks like. CP(1) NAME cp - copy files and directories SYNOPSIS cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY SOURCE... DESCRIPTION Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. The words in CAPS or in the form <word> or in italics means that they need to be substituted with an appropriate word which may be a lename or keyword. In the above example SOURCE needs to be replaced with the path of the source le. Words or options within square brackets [] are optional. If a | symbol separates one or more words or options like -r|-s, it means only one of them may be specied at a time. An ellipsis . . . after a word means that more can be specied. In the above man page SOURCE... means one or more source les can be specied separated by spaces. User Commands

7.3

Essential Commands

pwd Print the present working directory cd Change the current working directory 5

cd [dir] cd /home/ershaad/workdir cd workdir rm Delete le(s) rm draftfile.txt rm -r tempdir cp Copy les cp thesis.tex thesiscopy.tex cp -r datadir datadircopy mv Move or rename les mv oldfilename.txt newfilename.txt mv /home/ershaad/olddirname /home/backup/newdirname mv myfile.txt .. mkdir Create a directory mkdir newdir mkdir -p newdir/newsubdir/subsubdir cat Write the concatenation of contents to stdout cat file1.txt cat file1.txt file2.txt less View the contents of les less readme.txt file Information on the type of a le file image01.jpg find Search for les based on criteria find sourcedir -name "*.f90" locate Search for lename within index of les locate workreport.doc 6

chmod Manage permissions for a le chmod u+x file.sh chmod go-rwx file.txt chmod o-w file.txt gzip Compress a le gzip bigfile.dat gunzip Uncompress a le gunzip bigfile.dat.gz tar Create/Extract an archive tar -cvf homebackup.tar /home/ershaad tar -czvf homebackup.tar.gz /home/ershaad tar -xvf homebackup.tar tar -xzvf homebackup.tar.gz df Summary of space on lesystems df -h exit Exit the shell head, tail Print the rst/last few lines of a le head bankstatement.txt head -n 15 bankstatement.txt tail bankstatement.txt tail -n 12 bankstatement.txt du Find how much disk space a directory is using du -sh Downloads date Print or set the system date/time grep Print lines in le that match some pattern 7

grep orange fruits.txt

You might also like