Post on 17-Jun-2019
Page 1VI, October 2006
Introduction to Unix
Unix tutorial
http://linux.oreilly.com/
Page 2VI, October 2006
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
Page 3VI, October 2006
Unix - basic background (oversimplified)
UNIX is a common operating system (suite of programs which make the
computer work) made up of three parts; the kernel, the shell and the programs.
The kernelThe kernel of UNIX is the hub of the operating system: it allocates time and
memory to programs and handles the filestore and communications in response
to system calls.
The shellThe 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 command line
interpreter (CLI).
The commands are themselves programs: when they terminate, the shell gives
the user another prompt (“$” on some systems).
Page 4VI, October 2006
Unix - basic background (oversimplified)
Page 5VI, October 2006
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 more than one name beginning with those letters you have
typed, it will beep, prompting you to type 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
"history" for a list of previous commands.
Page 6VI, October 2006
Unix - basic background (oversimplified)
Page 7VI, October 2006
Unix - basic background (oversimplified)
Files and processes
"Everything in UNIX is either a file 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…
Page 8VI, October 2006
Unix - basic background (oversimplified)
The directory structure
/
/usr /home
phil johnlocal
bin lib
emacs X11
Starting an terminal session (CLI)
1)
2) An terminal window will appear
with a Unix prompt, waiting for you
to start entering commands.
Page 9VI, October 2006
Unix - Directories and files I
ls lists files in a directory
ls -la lists all (a) files in long (l) format
mkdir creates a new subdirectory
rmdir removes an EMPTY directory
pwd print working directory (path, current location)
(/home/john)
1.3 Changing to a different directory
cd 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
/
/usr /home
phil johnlocal
bin lib file.txt dir_1
file2.txt file3.txt
Page 10VI, October 2006
Unix - Directories and files II
cp copies files
copy orig.file copy.file (pathname may be required)
mv moves/renames a file
mv file1 file2
rm removes/deletes a file
rm -i file2 (-i : interactive)
clear clears all text
find searches the directory tree for a file
find . -name lostfile -print
* matches any number of characters in a file (or directory) name
? matches only one character.
/
/usr /home
phil johnlocal
bin lib file.txt dir_1
file2.txt file3.txt
Page 11VI, October 2006
Unix - Directories and files II
cat concatenates/displays all the content of a file on the screen
less displays the content of a file one screenful at 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)
head 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"
tail 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"
Page 12VI, October 2006
Unix - Directories and files II
diff compares two files and prints how they differ
diff file1 file2
wc displays a count of lines, words and characters in a file (word count)
grep 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"
Page 13VI, October 2006
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
Page 14VI, October 2006
Unix - Directories and files II
Important:
Unix & all Unix flavors: \n
Mac OS : \r
Windows:\r\n
"Alves","Pedro","1005","pedro.alves@hospvd.ch"^M"Brehelin","Claire","2007","claire.brehelin@unine
.ch"^M"Budirahardja","Yemima","1066","Yemima.Budirahardja@isrec.ch"^M"Burki","Fabien","1211","Fab
ien.Burki@zoo.unige.ch"^M"Charles","Roch-Philippe","1005","Roch-Philippe.Charles@unil.ch"^M"Estev
ez-Labori","Francisco","CH1066","Francisco.Estevez-Labori@unil.ch"^M"Flores Urushima","Sandra Yuk
ie","1005","sandrayukie.floresurushima@unil.ch"^M"Frateschi","Simona","1005","Simona.Frateschi@un
il.ch"^M"Garcion","Christophe","1700","christophe.garcion@unifr.ch"^M"Glauser","Dominique","1211"
,"dominique.glauser@medecine.unige.ch"^M"Gutacker","Michaela","4056","michaela.gutacker@unibas.ch
"^M"Guy","Lionel","1015","lionel.guy@unil.ch"^M"Hampp","Gabriele","1700","gabriele.hampp@unifr.ch
"^M"Harris","Michael","1005","michael.harris@unil.ch"^M"Henrichsen","Charlotte","1015","charlotte
[dot]henrichsen[at]unil[dot]ch"^M"Kulkarni","Subhash","1015","Subhash.Kulkarni@unil.ch"^M"Kundzew
icz","Adam","CH-1004","Adam.Kundzewicz@unil.ch"^M"Leidel","Sebastian","1066","Sebastian.Leidel@is
rec.unil.ch"^M"Nguyen-Ngoc","Tu","1066","Tu.Nguyen-Ngoc@isrec.unil.ch"^M"Nikolaev","Sergey","1211
","s-nikol@yandex.ru"^M"Oshota","Olusegun","SP4 0JG","James.Oshota@hpa.org.uk"^M"Parker","Joel","
CH-1015","joel.parker@unil.ch"^M"Paszkowski","Uta","1211","uta.paszkowski@bioveg.unige.ch"^M"Praz
","Viviane","1066","Viviane.Praz@isb-sib.ch"^M"Rybtsov","Stanislav","1006","Stanislav.Rybtsov@uni
l.ch"^M"Viatte","S<8E>bastien","1005","sviatte@hotmail.com"
participants_2005.cvs (END)
Page 15VI, October 2006
Unix - Directories and files II
Important:
Unix & all Unix flavors: \n
Mac OS : \r
Windows:\r\n
[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
Page 16VI, October 2006
Unix - Redirection (output, input, pipes)
Many processes initiated by UNIX commands:
- write 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
Page 17VI, October 2006
Unix - Editors
picoA Unix text editor
(http://www.indiana.edu/~ucspubs/b103/)
emacsEmacs is the extensible,
customizable, self-documenting
real-time display editor.
(http://www.gnu.org/software/emacs/emacs.html)
viThe Unix text editor
(http://www.unix-manuals.com/refs/vi-ref/vi-ref.htm)
vim, gvim, etc…
Page 18VI, October 2006
Unix - Access rights, processes
[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/
Each file (and directory) has associated access rights, which may be found
by typing ls -l :
d indicates a directory
9 letters code indicating right access
Owner of the file Group owning the file
Size
Date of creation
Name
Page 19VI, October 2006
Unix - Access rights, processes
owner (user) group others
r w x r w x r w x
Access rights on files.
• r (or -), indicates the presence (or absence) of permission to read and copy the
file
• w (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.
Page 20VI, October 2006
Unix - Access rights, processes
Changing access rights on files: chmod
u USER
g GROUP
o OTHERS
r READ
w WRITE
x EXECUTE
- TAKE AWAY PERMISSION
+ ADD PERMISSION
a ALL
chmod a+x file.pl
chmod g+w file.pl
owner (user) group others
r w x r w x r w x
Page 21VI, October 2006
Unix - Access rights, processes
ps displays information about all processeswith 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
kill 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
passwd to modify the users's password
Page 22VI, October 2006
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
exit logs current user out and closes the shell
Page 23VI, October 2006
The most 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 help 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 "traceroute"
Look through the matches for a likely candidate manual page.)