Tekstmanipulatie, week 9

General Introduction

Introduction to the course. Please, read this page and this page, too.

What is a computer in fact?

Main characteristics of a computer:

Software: feeding the computer with a software is as easy as feeding the computer with the data, and yet it determines the task performed by the computer. Earlier, you had had to rebuild the whole machine, when you wanted to solve a new problem.

John v. Neumann - Herman Goldstine (1948): binary system + controlled by a software program

Several levels between the machine and the user:

Why using PC's at post offices or for typing simple letters? The advantages of this kind of modularity are: ... therefore wider market to sell them, so they are cheaper.


Unix as an operating system. And also a culture, a way of thinking. Starting at AT&T, in 1969... standards and plenty of variations (Linux).

(Miles Osborne's Unix slides .)

Elements of the Unix philosophy:

Unix shells

A shell is really the envelope around the computer: it receives the commands and executes them. How does it work?

Different types of shells [different kinds of prompts]: e.g. Korn shell (ksh) [%], C-shell (Csh) [$], Bourne shell [$], Bourne Again shell (bash) [>]

We most often use the Bash-shell. In order to save typing a lot:

- cursor up: previous commands
- TAB: fills in the file names, if there is only one possibility
- TAB TAB: if there are more possibilities, you can get a list of them by typing TAB a second time

The User

Logging in, login name, password, changing password ('passwd'), logging out ('logout', 'exit', ^d)
A user is given:

- a user name (login name)
- a password
- a home directory

When you log in (login name + password), a shell is launched. This shell looks after you, waits for your commands, and executes them. Finally, you can exit the shell ('logout', 'exit', ^d), but then who takes care of you? If nobody, then you are logged out.

Root privileges (only the system administrator) vs. 'regular users'. 

The UNIX file system

What is a file (in Unix: "everything is a file")?

Finding the needed file out of thousands ones is usually hard. Therefore, operational systems use a hierarchical tree with a root, branches (= directories) and leaves (= files).

In Unix, the root directory is referred to as /. For instance, to list the content of the root directory, type ls /.

The symbol / refers also to a subdirectory. Thus, if blue is a file in the directory colours, then we may refer to it as colours/blue. If the directory bin is a subdirectory of the root (/), and if within bin there is a file named bash (it is in fact the program that usually runs as the shell), then we can refer to this file as /bin/bash.

To refer to your home directory, use ~. When the system administrator created your account, he also created your home directory (usually within the directory /home or /users, therefore your home directory is most probably /home/your_user_name or /users/your_user_name). When you log in under some user name, then the symbol ~ refers to the home directory of that user.

It is important to remember that the home directory is a crucial border in the file tree between the system and the user: bellow it, you are free to do anything (create sub-directories, files, remove them, etc.). However, you cannot do anything outside of your own home directory. Only the superuser (the system administrator), who has "root privileges", is able to make changes in the whole file system (including the root directory, hence the expression "root privileges").

In any moment, there an actual working directory. This is the point in the file system tree where you are now. You can refer to it as .. Relative to some point in the tree, the symbol .. refers to the parent directory (looking at one level upper in the tree).

Paths: a path is the way you identify the place of a given file in the file structure (don't forget that files at different points of the file structure may have the same name)

If you are in the directory tekstmanipul within your home directory, and you have logged in under the user name s12345, then the file /home/s12345/tekstmanipul/colours/blue can be reached as ./colours/blue or simply as colours/blue. If you are, however, in the directory ~/algorithm, then you have first to walk one level up, before moving down the tree: ../tekstmanipul/colours/blue. Furthermore, if your working directory is ~/algorithm/week1, then you have to work up two levels to reach the required file blue: ../../tekstmanipul/colours/blue.


/ : the root directory
___/___ : subdirectories (cf. \ in DOS!)
~ : home directory
. : the actual working directory
.. : parent directory
../.. : grand-parent, etc.

/abc : a file in the root directory
~/abc : a file in my home directory
./abc : a file in the actual working directory
../abc : a file in the parent directory of the actual working directory.

Important commands relating to the file structure:

'pwd' : print working directory (to file)
'ls' : list
mkdir : make directory
cd : change (working) directory
cat: catenate (concatenate) (lat. catena = 'chain') : use now this to create the simplest files.

A few important remarks about file names:

Different types of files:
- data files: not executable, but programs can read and / or write (re-write) them.
- program files: executable files (see also permissions )
- directories (yes, even directories are a special type of file)
- links , and other special types of links (e.g. the peripherals, like the floppy disc, the keyboard or the screen, are also files).

An important principle in Unix: EVERYTHING IS A FILE!

Directories are files. (This principle will be very important when we will learn about pipe lines.)
The "screen" is also a file (e.g. /dev/tty1 or /dev/pts/1): you can write something to a terminal (or virtual terminal) by writing into that file. (We will come back to this when we will learn about pipe lines.)
Drives are also files (directories) within the same hierarchy (e.g. /media/floppy/) (unlike DOS, like Windows).

The command mount virtually connects an external device (for instance a floppy disk) to the file system, and the command umount removes is. Thus, if you want to use a floppy disk, first you have to mount if with mount /media/floppy (the exact path to be used depends on the system). Then, the files on your floppy are under /media/floppy (or something slightly different, depending on the system). For instance, if your floppy contains a directory assignments, and within it, there is a file assignment1, then this file is /media/floppy/assignments/assignment1 for Unix. At the end, you have to unmount the floppy by using the command umount /media/floppy (or something slightly different, depending on your system's configuration). Unmounting the floppy before removing it from the drive and / or before logging out is necessary for three reasons, at least. First, maybe this is only when the new information is put physically on the disk (before that, the system kept track of what should have been on the disk). Second, if you do not unmount your floppy, you are not able to use another one. Last, but far not least, if log out without unmounting your floppy, other people cannot unmount yours, and they will not be able to use their own floppy disks.

Unix commands

Commands are either functionalities of the running shell, or program files that you can simply launch from the shell.

The general syntax of them is:

command [-options] [arguments]

Remark: [...] always means that this part is optional. What means e.g. abc [de [fg]] ?

Getting help / information about a given command:

man <command's_name>
<command's_name> - -help | less

The second option uses the online manual, a very useful functionality of Unix. If you do not know the command's name that you are looking for, you can use man -k keyword to look for a keyword in the online manual (probably, you will use man -k keyword | less).

Furthermore, it is important to know that the online manual has many chapters (usually referred to as numbers in brackets, e.g. in the "see also" section). If you type simply man command, you will receive the first description appearing in the online manual. So, for instance, if you type man time, Unix shows you an entry from section 1. However, if you type man 2 time, you are returned a very different description, from section 2. (The latter is actually a C function, because the online manual also includes information about the C language, a programing language closely related to Unix.)

Remark: the command man uses the program less in order to visualize the online manual. To scroll down in either `man' or `less', just press <SPACE> or the cursor buttons. To leave these programs, press Q.

Basic commands

Getting help / information about a given command:

man <command's_name>
<command's_name> - -help | more

We will use the command 'cat' to create and to read files, although we will discuss it only next week:

cat > file_name puts the text we are typing into the file, and we finish the file by typing ^d (where ^ refers to CTRL)
cat file_name gives us the content of that file.

passwd : change your password

logout : logging out (on Linux graphical terminals: use the logging out button instead)

exit : quit the shell (practically speaking, it means very often logging out)

cd : change (working) directory
Going home with 'cd' or 'cd ~'

pwd : print working directory

who : who are logged in?
'whoami' or 'who am i'

date : gives the actual date and time

cal [month] year : gives the calendar of that given month / year. Try 'cal 9 1752', and remember what you know about the Julian and the Gregorian calendar!

ls : list

ls lists the actual working directory
ls -l gets long list
ls <file_names> lists the given files or directories
ls -R lists recursively the subdirectories

Always list the directory you have just done something! Check yourself after every step!

mkdir : make directory

mkdir <dir_name> [<dir_name>...]

rm, rmdir : remove files and directories (watch out!)

rm -r : removes the subdirectories recursively
rm -i : asks for affirmation

cp : copy
mv : move (copy + delete the original one)

cp / mv [options] <origin> <destination>
The destination can be both a file name or a directory. If the two arguments of cp or mv are regular files, then the file to which the first argument points is copied or moved to the file to which the second argument points. Previously existing files are overwritten. However, if the last argument is an already existing directory, then the source file is copied or moved into that directory, and its name is kept. Furthermore, if the last argument is a directory, you can have more than two arguments: all the files which are referred to by the first arguments (all arguments except the last one) are moved into that directory. If the last argument is not a directory, the action does not make sense and you receive an error message: which of the first files should be copied or moved to the last one?

How to rename a file? Renaming a file is nothing but moving file x to file y: you delete file x, and you create a new file, with the name y and with the same content. If you do it within the same directory, then it looks as if you have renamed the original file. Consequently, you the command `mv' to rename a file.

Examples (try to understand them! observe how relative and absolute paths are used!):

A very very important rule : if a file has been removed, there is no possibility forever to recover it, unlike in the case of DOS!!! (Remember, Unix is designed for multiple users working parallel in the same file structure.)

Bíró Tamás:
English web site
Magyar honlap

Last modified: Thu Jul 3 11:39:17 METDST 2003