<<

tutorial

Introduction to Unix

http://linux.oreilly.com/

VI, October 2006 Page 1

Unix tutorial

Outline

• Basic background in Unix structure

• Directories and files I (listing, navigation, filenames)

• Directories and files II (working with files & their content)

• Redirection (output, input, pipes)

• Editors

• Access rights, processes

• Other useful Unix commands

VI, October 2006 Page 2 Unix - basic background (oversimplified)

UNIX is a common (suite of programs the computer work) made up of three parts; the kernel, the shell and the programs.

The kernel The kernel of UNIX is the hub of the operating system: it allocates and memory to programs and handles the filestore and communications in response to system calls.

The shell The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a line interpreter (CLI).

The commands are themselves programs: when they terminate, the shell gives the user another prompt (“$” on some systems).

VI, October 2006 Page 3

Unix - basic background (oversimplified)

VI, October 2006 Page 4 Unix - basic background (oversimplified)

Filename Completion

By typing part of the name of a command, filename or directory and pressing the [Tab] key, the shell will complete the rest of the name automatically. If the shell finds than one name beginning with those letters you have typed, it will beep, prompting you to a few more letters before pressing the tab key again.

History

The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys [arrows] to scroll up and down the list or type "" for a list of previous commands.

VI, October 2006 Page 5

Unix - basic background (oversimplified)

VI, October 2006 Page 6 Unix - basic background (oversimplified)

Files and processes

"Everything in UNIX is either a or a process." A process is an executing program identified by a unique PID (process identifier).

A file is a collection of data. They are created by users using text editors, running compilers etc. It is identified by a name and an logical address (or path).

Examples of files: • a document (report, essay etc.) • the text of a program written in some high-level programming language • instructions comprehensible directly to the machine and incomprehensible to a casual user, for example, a collection of binary digits (an executable or binary file) • etc…

VI, October 2006 Page 7

Unix - basic background (oversimplified)

The directory structure Starting an terminal session (CLI) /

/usr /home 1)

local phil john 2) An terminal window will appear bin lib with a Unix prompt, waiting for you to start entering commands. emacs X11

VI, October 2006 Page 8 Unix - Directories and files I

lists files in a directory /

ls -la lists all (a) files in long (l) format /usr /home

creates a new subdirectory local phil john

bin lib file.txt dir_1 removes an EMPTY directory

file2.txt file3.txt print working directory (path, current location) (/home/john) 1.3 Changing to a different directory changes current directory Note: typing cd with no argument or typing cd ~ always returns you to your home directory.

. in UNIX means the current directory . . in UNIX means the parent of the current directory

VI, October 2006 Page 9

Unix - Directories and files II

copies files / copy orig.file copy.file (pathname may be required) /usr /home moves/renames a file mv file1 file2 local phil john

bin lib file.txt dir_1 removes/deletes a file rm -i file2 (-i : interactive) file2.txt file3.txt clears all text

searches the directory for a file find . -name lostfile -print

* matches any number of characters in a file (or directory) name

? matches only one character.

VI, October 2006 Page 10 Unix - Directories and files II

concatenates/displays all the content of a file on the screen

displays the content of a file one screenful a time less file.txt then press spacebar to display the next screen or use arrow keys to move up or down row by row. (Type / followed by a word to find it in the file. Press Q to quit).

more (see less)

prints the first lines of a file on the screen (10 by default or specify the desired number of lines with "-") head -20 file.txt diplays the first 20 lines from the file "file.txt"

prints the last lines of a file on the screen (10 by default or specify the desired number of lines with "-") tail -20 file.txt diplays the last 20 lines from the file "file.txt"

VI, October 2006 Page 11

Unix - Directories and files II

compares two files and prints how they differ diff file1 file2

displays a count of lines, words and characters in a file (word count)

searches a file for a string and displays all the lines containing this string

grep word file.txt displays all lines containing "word" grep "following words" file.txt displays all lines containing "following words"

grep -c word file.txt displays the number of lines containing the string

grep -e "one" -e "two" -e "three" file.txt displays all lines containing "one" or "two" or "three"

grep -v "word" displays all lines that do not contain "word"

VI, October 2006 Page 12 Unix - Directories and files II od The od command copies sequentially each input file to standard output and transforms the input data according to the specified options.

od -c file.txt displays some non-graphic characters as C-language escapes (see more options: man od)

null \0 backspace \b form-feed \f new-line \n return \r tab \t

Important: Unix & all Unix flavors: \n Mac OS : \r Windows:\r\n

VI, October 2006 Page 13

Unix - Directories and files II

"Alves","Pedro","1005","[email protected]"^M"Brehelin","Claire","2007","claire.brehelin@unine .ch"^M"Budirahardja","Yemima","1066","[email protected]"^M"Burki","Fabien","1211","Fab [email protected]"^M"Charles","Roch-Philippe","1005","[email protected]"^M"Estev ez-Labori","Francisco","CH1066","[email protected]"^M"Flores Urushima","Sandra Yuk ie","1005","[email protected]"^M"Frateschi","Simona","1005","Simona.Frateschi@un il.ch"^M"Garcion","Christophe","1700","[email protected]"^M"Glauser","Dominique","1211" ,"[email protected]"^M"Gutacker","Michaela","4056","[email protected] "^M"Guy","Lionel","1015","[email protected]"^M"Hampp","Gabriele","1700","[email protected] "^M"Harris","Michael","1005","[email protected]"^M"Henrichsen","Charlotte","1015","charlotte []henrichsen[at]unil[dot]ch"^M"Kulkarni","Subhash","1015","[email protected]"^M"Kundzew icz","Adam","CH-1004","[email protected]"^M"Leidel","Sebastian","1066","Sebastian.Leidel@is rec.unil.ch"^M"Nguyen-Ngoc","Tu","1066","[email protected]"^M"Nikolaev","Sergey","1211 ","[email protected]"^M"Oshota","Olusegun","SP4 0JG","[email protected]"^M"Parker","Joel"," CH-1015","[email protected]"^M"Paszkowski","Uta","1211","[email protected]"^M"Praz ","Viviane","1066","[email protected]"^M"Rybtsov","Stanislav","1006","Stanislav.Rybtsov@uni l.ch"^M"Viatte","S<8E>bastien","1005","[email protected]" participants_2005.cvs (END)

Important: Unix & all Unix flavors: \n Mac OS : \r Windows:\r\n

VI, October 2006 Page 14 Unix - Directories and files II

[embnet02@bc2-linux3 ~]$ od -c participants_2005.cvs | head 0000000 " A l v e s " , " P e d r o " , 0000020 " 1 0 0 5 " , " p e d r o . a l 0000040 v e s @ h o s p v d . c h " \r " 0000060 B r e h e l i n " , " C l a i r 0000100 e " , " 2 0 0 7 " , " c l a i r 0000120 e . b r e h e l i n @ u n i n e 0000140 . c h " \r " B u d i r a h a r d 0000160 j a " , " Y e m i m a " , " 1 0 0000200 6 6 " , " Y e m i m a . B u d i 0000220 r a h a r d j a @ i s r e c . c

Important: Unix & all Unix flavors: \n Mac OS : \r Windows:\r\n

VI, October 2006 Page 15

Unix - Redirection (output, input, pipes)

Many processes initiated by UNIX commands: - to the standard output (the terminal screen) - and take their input from the standard input (the keyboard)

The > symbol is used to redirect the output of a command in a file. If the file exists, its content is replaced, if not, it is created. cat file1 file2 > file3 redirects the concatenated files into file3

The >> symbol is used to redirect the output of a command in a file. If the file exists, the output is appended, if not, the file is created. cat file4 file5 >> file3 appends the concatenated files into file3

The | symbol (pipe) uses the output of the 1st command as the input of the 2nd grep word file.txt | wc

VI, October 2006 Page 16 Unix - Editors pico A Unix text editor (http://www.indiana.edu/~ucspubs/b103/) emacs Emacs is the extensible, customizable, self-documenting real-time display editor. (http://www.gnu.org/software/emacs/emacs.html) vi The Unix text editor (http://www.unix-manuals.com/refs/vi-ref/vi-ref.htm) vim, gvim, etc…

VI, October 2006 Page 17

Unix - Access rights, processes

Each file (and directory) has associated access rights, which may be found by typing ls -l :

Owner of the file Group owning the file Date of creation

[embnet02@bc2-linux3 ~]$ ls -l total 8 -rwxr-xr-x 1 embnet02 guest 43 Sep 20 11:53 hello.pl* drwxr-x--x 3 embnet02 guest 4096 Sep 7 16:03 www/

9 letters code indicating right access Size Name d indicates a directory

VI, October 2006 Page 18 Unix - Access rights, processes

Access rights on files. • r (or -), indicates the presence (or absence) of permission to read and copy the file • (or -), indicates write permission (the permission to change a file) • x (or -), indicates execution permission (the permission to execute a file)

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 execute files in the directory.

owner (user) group others r w x r w x r w x

VI, October 2006 Page 19

Unix - Access rights, processes

Changing access rights on files:

u USER chmod a+x file.pl g GROUP o OTHERS chmod g+w file.pl r READ w WRITE x EXECUTE - TAKE AWAY PERMISSION + ADD PERMISSION a ALL

owner (user) group others r w x r w x r w x

VI, October 2006 Page 20 Unix - Access rights, processes

displays information about all processes with their associated PID and status, type

bg moves the current process to the backround (can be done automatically by adding & after the command)

fg moves the current process to the foreground

jobs lists backround and suspended processes

stops a process (use ps or jobs to find the process id) kill 1 (kills the job number 1)

ctrl-C stops the current process

date displays current date and time

to modify the users's password

VI, October 2006 Page 21

Other useful Unix commands

tar creates (or extracts) a tarball from (to) a list of files tar -cvf tarball.tar subdir/* tar -xvf tarball.tar

the -z option compacts the files by gzip. when -z is used it is advised to use the "gz" tar -zcvf tarball.gz subdir/* tar -zxvf tarball.gz

List all files in archive.gz verbosely tar -ztvf archive.gz

logout logs current user out

logs current user out and closes the shell

VI, October 2006 Page 22 The useful Unix command

man The man command (short for "manual") displays the manual page for the specified command at your terminal. To page through the output, press the Return key. To exit, type Ctrl-c.

man grep (Lists information on the "grep" command.)

If you don't know which command you want? Well … An alternative is to use the "-k" option to tell man to do a keyword lookup for manual pages containing the keyword.

man -k route (This command displays a one-line summary from each manual page matching the keyword "" Look through the matches for a likely candidate manual page.)

VI, October 2006 Page 23