Professional Documents
Culture Documents
UNIX Fundamentals 2010
UNIX Fundamentals 2010
CTO/SDE
2009-5
Agenda
UNIX Overview UNIX File System Process Control Introduction to vi Customizing Your Shell Environment Other Basic Commands
UNIX Overview
What Is UNIX?
The UNIX Operating System (OS) is a large program (mostly coded in C) that turns the computer into a useable machine.
active community
Architecture of UNIX
Kernel
Utilities : ls, cp, grep, awk, sed, bc, wc, more Application programs: emacs editor, gcc
compiler
If you login with a graphical terminal, you can look for menus or icons which mention the words "shell", "xterm", "console" or "terminal to open a shell prompt.
^D
Or
exit
Password Tips NEVER tell anyone your password. Dont write it down. A good password is: - 8 (or more) characters long - uses a mix of uppercase and lowercase letters, numbers, and symbols (e.g. #, %).
Example: ls -l /home/jones/dir1
Note: UNIX commands, options, and arguments are all Case sensitive!
UNIX Help
man cmd
man ls man man
File types
The UNIX file system contains several different types of files
Regular file
Directory
Device Link
Socket
Named Pipe
...
dev
tmp
...
...
.......
joan
play
...
work
...
hobby.c
proj1
...
/lib
/usr/lib
/tmp
/etc
/dev
/proc
Hardware devices
A pseudo-filesystem which is used as an interface to the kernel. Includes a sub-directory for each active program (or process).
You can refer to your home directory at any time as "~" and the home directory of other users as "~<login>".
Will specify the directory proj1? / Wills home dir will / home /joan /proj1 home joan
~joan/proj1
play
...
work
...
~/play
hobby.c
proj1
...
Pathnames
Absolute Pathnames
The sequence of directory names between the top of the tree (the root) and the directory of interest.
Relative Pathnames
The sequence of directory names below the directory where you are now to the directory of interest.
examples: Absolute Pathnames /bin /etc/terminfo Relative Pathnames bin terminfo Comments if you are in / if you are in etc
/export/user/home/ad
../user/home/ad
. ..
File Permissions
There are three types of permissions supported by UNIX:
r (4)
w (2)
x (1)
File Permissions
Heres an example
type
User permissions
Group permissions
owner
size group
Modification date/time
File name
Other Permissions
File Permissions
rwx r-x
r--
111
4 +2 +1
101
4 +0+1
100
4 +0+0
Changing Permissions
chmod [options] mode filename example: change file permissions with numerical mode
6
110
4
100
1
001
rw-
r--
--x
the owner of hw1 has rw-(6) permission, the group has r--(4) permission, others have --x permission.
23 | Corporate Overview | January 2000 All Rights Reserved Alcatel-Lucent Shanghai Bell 2009
Changing Permissions
chmod [options] who ops permission filename change file permissions with symbolic mode
options
R recursively change permissions who can be any combination of:
u (user)
o (other/world)
g (group)
a (all or ugo)
ops adds or takes away permission, and can be: + (add permission) (remove permission)
w (write)
x (execute)
All Rights Reserved Alcatel-Lucent Shanghai Bell 2009
their owners
chmod u-x abc.c chmod g+rwx myfile1 take away execute permission on abc.c from owner add read, write and execute permissions on myfile1
Permission Mask
umask [-S] [mode] Option: -S Example: file default permissions: directory default permissions: $ umask u=rwx,g=rwx,o= $ mkdir foo $ touch bar $ ls -l drwxrwx--- 2 user test 512 May 1 20:59 foo -rw-rw---- 1 user test 0 May 1 20:59 bar
26 | Corporate Overview | January 2000 All Rights Reserved Alcatel-Lucent Shanghai Bell 2009
User file-creation mode mask Produce symbolic output user group other rwrwrwrwx
rwx rwx
examples:
File Management
ls pwd (list) list files and directories
mkdir
cd mv
(make directory
(change directory) (move)
cp
rm touch cat more ln
(copy)
(remove)
(concatenate)
(Link)
Listing Contents
ls [options] dirname options: list the contents of dirname
-a
-l
examples: ls /home/user/temp
ls ../../temp
pwd
Make Directory
mkdir dirname create the directory dirname
examples:
mkdir work create the directory work/ in the current working directory
mkdir work/proj1
mkdir /wrk/user2
Change Directory
cd cd dirname change to your home directory change to the directory dirname
examples:
cd ~tom cd /wrk/user2 change to toms home directory change to the directory /wrk/user2
cd ..
cd ../..
Moving or Renaming
mv [options] presname newname mv [options] srcfile destdir rename a file move a file to another directory
options:
-i
example: mv ~user1/file ./outputfile
confirm overwrites
moves file from user1s home directory to the current working directory and renames it output file
Copying
cp [options] srcfile destfile cp [options] srcfile destdir copy a file to another filename copy a file to another directory
options:
-i -R
example: cp -R proj1 proj2
confirm overwrites recursively copy a directory and its contents, copies symbolic links
Removing
rm [options] filename options: remove a file
-f
-i -r
example: rm -rf /scratch/user2/dir1
Examples:
ls -l -rw-r--r-- 1 user1 -rw-r--r-- 1 user1 touch newfile ls -l -rw-r--r-- 1 user1 -rw-r--r-- 1 user1 -rw-r--r-- 1 user1 touch secondfile ls l -rw-r--r-- 1 user1 -rw-r--r-- 1 user1 -rw-r--r-- 1 user1
35 | Corporate Overview | January 2000
users 25936 Apr 24 09:53 firstfile users 10245 Apr 24 09:53 secondfile users 25936 Apr 24 09:53 firstfile users 0 Apr 25 10:02 newfile users 10245 Apr 24 09:53 secondfile users 25936 Apr 24 09:53 firstfile users 0 Apr 25 10:02 newfile users 10245 Apr 25 10:05 secondfile
All Rights Reserved Alcatel-Lucent Shanghai Bell 2009
Catenate / Type
cat filename 1) displays the contents of file on the screen, one after the other .
cat > hello.txt hello world! [ctrl-d] ls hello.txt hello.txt cat hello.txt hello world!
more output.c
ls -l | more (| is the pipe operator)
directory to another
example: ln -s hello.txt bye.txt ls -l bye.txt lrwxrwxrwx 1 will finance 13 bye.txt -> hello.txt
Process Control
Processes
Every time you invoke a system utility or an application program from a shell,
one or more "child" processes are created by the shell in response to your command.
An important process that is always present is the init process. This is the first
process to be created when a UNIX system starts up and usually has a PID of 1. All other processes are said to be "descendants" of init.
Listing Process
ps [options] display the status of the current processes and the process id-number
options:
-e -u all processes now running display processes owned by a particular user
jobs
shows any jobs that are currently running in the background or suspended
Process Control
<Ctrl>-C <Ctrl>-Z cancel a foreground job stop (interrupt) a foreground job
bg
fg &
background
examples: $ sleep 1000 & $ man ls <Ctrl>-Z $ jobs $ fg %2
$ jobs [2] + Stopped (SIGTSTP) man ls [1] - Running sleep 1000 &
?
All Rights Reserved Alcatel-Lucent Shanghai Bell 2009
Killing a process
kill id-number terminate a process owned by you; id-number (process id) can be found with the ps command
or
kill %job-number terminate a process owned by you; job-number can be found with the jobs command
Example:
ps
PID TTY TIME CMD 17717 pts/10 00:00:00 bash
File
Rese t Br e k a f1 Sp to f2
Device
f3 f4 Men u Use r S m yste f5 f6 f7 f8
File Descriptor
Inse t r line Dele te line
stdin
~ `
! 1 Q
@ 2 W
# 3 E
$ 4 R
% 5 T
^ 6 Y
& 7 U
* 8 I
( 9 O
) 0 P
_ { [ : ;
= + } ] " '
Back S ce pa
. 7 4 1 0
/ 8 5 2
+ 9 6 3 .
T ab
| \
Ins r t e
cha r
Dele te cha r
Cap s
CTRL
Re rn tu
Pe r v
,
T ab
DE L ESC
S hift
< ,
> .
? /
E xten d cha r
S hift
S elect
Next
Exten d cha r
a566137
Input Redirection
< command input redirection (from file)
Examples:
how to email a file to a collaborator mail -s results collab@alcatel-sbell.com.cn < new_blast_results
Output Redirection
> >> command output redirection (create new, overwrites existing file) command output redirection (append)
$ ls > filelist.out
$ cat seq1 seq2 > seq
$ ls >> filelist.out
$ cat seq1 >> seq2
Error Redirection
2> 2>> error output redirection (create new, overwrites existing file) error output redirection (append)
Examples:
$ cp 2> cp.err $ cp 2>> cp.err Create/Overwrite Create/Append
$ more cp.err
Usage: cp [-f|-i] [-p] source_file target_file cp [-f|-i] [-p] source_file ...target_directory cp [-f|-i] [-p] -R|-rsource_directory...target_directory Usage: cp [-f|-i] [-p] source_file target_file
pipes
command1 | command2 | directs standard output of one command into standard input for the next command
examples:
ls -al | more cat hello.txt | sort | uniq look at the ls output one page at a time creates three processes (corresponding to cat,
Introduction to vi
Introduction to vi
vi is the standard UNIX text editor very powerful
full-screen
Starting vi vi filename Changes are stored in a buffer, so you must save to change the file !If the file doesn't exist, vi will create it for you.
50 | Corporate Overview | January 2000 All Rights Reserved Alcatel-Lucent Shanghai Bell 2009
Three Modes
Command mode
for inserting text start by typing i; finish with ESC cannot quit, delete, etc. in this mode If in doubt, press ESC a few times. This will put you back in
command mode.
51 | Corporate Overview | January 2000 All Rights Reserved Alcatel-Lucent Shanghai Bell 2009
Cursor movement
h j k left down up
right
^ $
1G G ^F ^B w b
top of document end of document page forward page backward word forwards word backwards
No RETURN
Move to insertion point
deletion
Deleting
Backspace Delete character before cursor (only works in insert mode) Must be in command mode
x
dd <n>dd
Delete lines i to j Delete lines 23 to 29 Delete from current line to the end of file.
dd or <n>dd or D p
Copy & Paste
yy or <n>yy
Searching for Text type / and then a regular expression and press Enter
There is one here and one more here and yet one more but not this ONE nor this One
a56686
:s/Hat/Head/g
:2,200s/Andy/Andrew/g
:1,.s/fc/function/g
Finishing a vi Session
Get to :w :wq ZZ :q! command mode (press ESC) save file (not quit) save file and quit save file and quit quit without saving save as the file 'newfile', overwriting any existing newfile
:w! newfile
There are many different shells available on UNIX systems (e.g. sh, bash, csh, ksh etc.), and they each support a different command language.
exec sh
$ passwd -r nis -e
Enter login(NIS) password: Type your Unix password and press return. You will then see screen output similar to the following:
Initialization files
Each time you login to a UNIX host, the system looks in your home directory for initialization files. You can change your environment by changing initialization files. Shell System initialization filesFirst /etc/profile /etc/profile User initialization filessecondly $HOME/.profile $HOME/.profile $HOME/.kshrc $HOME/.cshrc $HOME/.login $HOME/.bash_profile $HOME/.bashrc Template /etc/skel local.profile local.profile
/etc/.login
/etc/profile
.login is to set conditions which will apply to the whole session and to perform
actions that are relevant only at login.
.cshrc is used to set conditions and perform actions specific to the shell and to
each invocation of it. The guidelines are to set ENVIRONMENT variables in the .login file and SHELL variables in the .cshrc file. Setting shell variables in the .cshrc file
$ vi ~/.cshrc Add the following line AFTER the list of other commands set history = 200 Save the file $ source .cshrc (force the shell to reread its .cshrc file ) $ echo $history (Check this has worked)
PATH
HOME PWD OLDPWD
a list of directories that the shell uses to locate executable files for commands
Name of your home (login) directory Current directory Previous directory before the last cd command
Setting system variables differs by shell. bash uses export, csh uses setenv
varname=value command export varnames Example: PATH=$PATH:/sbin:/usr/sbin:/usr/bin:/etc:/usr/ucb:/local/bin EDITOR=/usr/local/bin/emacs export EDITOR PATH
Alias
alias new=original alias new original defines new to be an alias for original (ksh or bash) (csh)
gzip
find grep sort
compress files
find files and directories with a wealth of search criteria (general regular expression parser); text search sort information provided on standard input
ssh
compress file
uncompress file.Z gzip file gunzip file.gz
Finding Files
find Example: Find files and directories with a wealth of search criteria
counts the number of lines in every text file in and below the current directory. The '{}' is replaced by the name of each file found and the ';' ends the -exec clause.
Searching Text
grep options pattern files searches the named files (or standard input if no files are named) for lines that match a given pattern
Options:
-c (print a count of the number of lines that match) -i (ignore case) -v (print out the lines that don't match the pattern) -n (printout the line number before printing the matching line)
Sorting files
sort [options] filenames Options: sort information provided on standard input
-r
Examples:
outputs the sorted concentenation of files input1.txt and input2.txt to the file output.txt. sort input.txt | uniq > output.txt
Uniq removes duplicate adjacent lines from a file input.txt. It is most useful when combined with sort
Remote login
ssh [options] host secure shell; a program for logging into a remote host providing encrypted communications
between hosts
options: -l login name
-X
example: ssh -l fengy sbardy12 open a secure connection for the user fengy on the host sbardy12 ssh load2 open a secure connection to host load2 with current username
74 | Corporate Overview | January 2000 All Rights Reserved Alcatel-Lucent Shanghai Bell 2009
Thanks!
www.alcatel-sbell.com.cn