Upload
waqas-ch
View
219
Download
0
Embed Size (px)
Citation preview
7/31/2019 UNIX Fundamentals 2010
1/75
UNIX Fundamentals
CTO/SDE
2009-5
7/31/2019 UNIX Fundamentals 2010
2/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 20092 | Corporate Overview | January 2000
Agenda
UNIX Overview
UNIX File System
Process Control
Introduction to vi
Customizing Your Shell Environment
Other Basic Commands
7/31/2019 UNIX Fundamentals 2010
3/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 20093 | Corporate Overview | January 2000
UNIX Overview
7/31/2019 UNIX Fundamentals 2010
4/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 20094 | Corporate Overview | January 2000
What Is UNIX?
The UNIX Operating System (OS) is a large program (mostly coded in C) that turns
the computer into a useable machine.
UNIX is a multi-user, multi-tasking operating system.
UNIX is a machine independent operating system.
UNIX is a software development environment.
7/31/2019 UNIX Fundamentals 2010
5/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 20095 | Corporate Overview | January 2000
A Brief History of UNIX
UNICS(1969)
Fourth Edition(1973)
Sixth Edition(1975)
Seventh Edition
(1979)
SYSV(1983)
BSD(1979)
Solaris/SunOs 5.x(SUN)AIX(IBM)IRIX(SGI)
HP-UN(HP)Digital UNIX(DEC)SCO Unix(SCO)UnixWare(SCO)
SunOs 4.x(SUN)ULTRIX(DEC)
NextStep(NeXT)
FreeBSD (Open Source)NetBSD (OPen Source)
OpenBSD (Open Source)
SVR4(1993)
*Linux (Open Source) (1991)
7/31/2019 UNIX Fundamentals 2010
6/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 20096 | Corporate Overview | January 2000
Why Use UNIX?
multi-tasking / multi-user
Distributed processing
rich set of tools
powerful networking capabilities
stabile
portable (Hardware independence )
free! (FreeBSD, GNU)
profitable
1996 Sales: US$34.5 Billion, up 12%
active community
7/31/2019 UNIX Fundamentals 2010
7/75All Rights Reserved Alcatel-Lucent Shanghai Bell 20097 | Corporate Overview | January 2000
Architecture of UNIX
Kernel
includes device driver
implements most BSD and SYSV system calls
Shells and GUIs
command line shell
GUIs: KDE, GNOME
Utilities & Application
Utilities : ls, cp, grep, awk, sed, bc, wc, more
Application programs: emacs editor, gcc
compiler
7/31/2019 UNIX Fundamentals 2010
8/75All Rights Reserved Alcatel-Lucent Shanghai Bell 20098 | Corporate Overview | January 2000
Login to UNIX systems
login: ad Enter your ID and RETURN.
Password: Enter your password and RETURN. It does not appear.
$ The UNIX prompt (or similar). You can now enter commands.
If you login with a graphical terminal, you can look for menus or icons whichmention the words "shell", "xterm", "console" or "terminal to open a shellprompt.
7/31/2019 UNIX Fundamentals 2010
9/75All Rights Reserved Alcatel-Lucent Shanghai Bell 20099 | Corporate Overview | January 2000
Logout from your account
logout
or
^D Press CONTROL and D together
Or
exit
7/31/2019 UNIX Fundamentals 2010
10/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200910 | Corporate Overview | January 2000
Change your password
The command is
$ passwd
It will ask you for the new password twice.
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 andlowercase letters, numbers, and symbols(e.g. #, %).
7/31/2019 UNIX Fundamentals 2010
11/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200911 | Corporate Overview | January 2000
General Command Syntax
The general syntax for a UNIX command is
$ command [-options] targets
Example:
ls -l /home/jones/dir1
Note: UNIX commands, options, and arguments are all Case sensitive!
7/31/2019 UNIX Fundamentals 2010
12/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200912 | Corporate Overview | January 2000
UNIX Help
man cmd Manual pages
Spacebar to go on; ^C to stopman ls
man man
whatis cmd One-line description
which cmd Location of command locate keyword List files with keyword in their name (or path)
7/31/2019 UNIX Fundamentals 2010
13/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200913 | Corporate Overview | January 2000
UNIX File System
7/31/2019 UNIX Fundamentals 2010
14/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200914 | Corporate Overview | January 2000
File types
The UNIX file system contains several different types of files
Regular file
Directory
Device
Link Socket
Named Pipe
7/31/2019 UNIX Fundamentals 2010
15/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200915 | Corporate Overview | January 2000
A simplified UNIX Directory Structure
. . .
. . . . . . .
. . .
. . .
/
etc bin dev tmp
date calhome
. . . . . .
will joan
play work proj1hobby.c. . .. . .
7/31/2019 UNIX Fundamentals 2010
16/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200916 | Corporate Overview | January 2000
Some Typical UNIX directories
/ The "root" directory
/bin Essential low-level system utilities
/usr/bin Higher-level system utilities and application programs
/sbin Superuser system utilities (for performing system administration
tasks)
/lib Program libraries (collections of system calls that can be included
in programs by a compiler) for low-level system utilities
/usr/lib Program libraries for higher-level user programs
/tmp Temporary file storage space (can be used by any user)
/etc UNIX system configuration and information files
7/31/2019 UNIX Fundamentals 2010
17/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200917 | Corporate Overview | January 2000
Some Typical UNIX directories (cont.)
/home User home directories containing personal file space for each user.
or /homes Each directory is named after the login of the user.
/dev Hardware devices
/proc A pseudo-filesystem which is used as an interface to the kernel.
Includes a sub-directory for each active program (or process).
7/31/2019 UNIX Fundamentals 2010
18/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200918 | Corporate Overview | January 2000
The home directory
When you log into UNIX, your current working directory is your user home
directory.
You can refer to your home directory at any time as "~" and the home directory
of other users as "~".
Willspecify the directory proj1?/
home
joan
proj1hobby.c. . .
will
play work
Will shome dir
. . .. . .
/ home /joan /proj1
~joan/proj1
~/play
7/31/2019 UNIX Fundamentals 2010
19/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200919 | Corporate Overview | January 2000
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 Relative Pathnames Comments
/bin bin if you are in //etc/terminfo terminfo if you are in etc
/export/user/home/ad ../user/home/ad if you are in /export/tma
. the current directory
.. the parent directory
7/31/2019 UNIX Fundamentals 2010
20/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200920 | Corporate Overview | January 2000
File Permissions
There are three types of permissions supported by UNIX:
r (4) read, view the contents of a file or a directory
w(2) write, edit file/directory contents
x (1) execute, run executable file
There are three levels of permissions:
User (owner) the person who owns the file.
Group the group owns the file.
Other the rest of the world
7/31/2019 UNIX Fundamentals 2010
21/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200921 | Corporate Overview | January 2000
File Permissions
Heres an example
Suppose you type in ls -l and the result is
-rwxr-xr-- 1 hansdoc 858 Aug 22 22:28hw1
owner
group
size Modificationdate/time
Grouppermissions
OtherPermissions
links
type
Userpermissions
File name
7/31/2019 UNIX Fundamentals 2010
22/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200922 | Corporate Overview | January 2000
File Permissions
read=>4; write=>2; execute=>1
111
4 + 2 + 1
7 5 4
101
4 + 0 + 1
100
4 + 0 + 0
rwx r-x r--
7/31/2019 UNIX Fundamentals 2010
23/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200923 | Corporate Overview | January 2000
Changing Permissions
chmod [options]modefilename change file permissions with numerical mode
example:
chmod 641 hw1
6 4 1
110 100 001
rw- r-- --x
the owner of hw1 has rw-(6) permission, the grouphas r--(4) permission, others have --xpermission.
7/31/2019 UNIX Fundamentals 2010
24/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200924 | Corporate Overview | January 2000
Changing Permissions
chmod [options] who ops permissionfilename change file permissions with
symbolic mode
options
R recursively change permissions
who can be any combination of:u (user) g (group)
o (other/world) a (all or ugo)
ops adds or takes away permission, and can be:
+ (add permission) (remove permission)
permission can be any combination of:
r (read) w (write) x (execute)
7/31/2019 UNIX Fundamentals 2010
25/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200925 | Corporate Overview | January 2000
Changing Permissions (cont.)
examples:
chmod u+w test report add write permission on the files test and report for
their owners
chmod u-x abc.c take away execute permission on abc.c from owner
chmod g+rwx myfile1 add read, write and execute permissions on myfile1for the group
chmod ugo+rwx myfile2 add read,write and execute permissions on myfile2
for everyone
7/31/2019 UNIX Fundamentals 2010
26/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200926 | Corporate Overview | January 2000
Permission Mask
umask [-S] [mode] User file-creation mode mask
Option:
-S Produce symbolic output
Example: user group other
file default permissions: rw- rw- rw-
directory default permissions: rwx rwx rwx
$ umask u=rwx,g=rwx,o= (or $ umask 007)
$ 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
7/31/2019 UNIX Fundamentals 2010
27/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200927 | Corporate Overview | January 2000
Changing owner and group
chown user file change file ownership to another user
Must have the write permission on the file!
chgrp group file change file group to another user
Only owner or supervisor can change the group!
examples:chown joan test1
chgrp testteam test1
7/31/2019 UNIX Fundamentals 2010
28/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200928 | Corporate Overview | January 2000
File Management
ls (list) list files and directories
pwd (print working directory display full pathname of current directory
mkdir (make directory create new directories
cd (change directory) change to the directory
mv (move) move a file or directorycp (copy) copy a file or directory
rm (remove) remove a file and directory
touch update Timestamp on File
cat (concatenate) display files, or concatenate files
more control the display of files
ln (Link) create link file or directory
http://localhost/var/www/apps/conversion/releases/20121024195125/tmp/scratch_2/ahttp://localhost/var/www/apps/conversion/releases/20121024195125/tmp/scratch_2/fhttp://localhost/var/www/apps/conversion/releases/20121024195125/tmp/scratch_2/fhttp://localhost/var/www/apps/conversion/releases/20121024195125/tmp/scratch_2/fhttp://localhost/var/www/apps/conversion/releases/20121024195125/tmp/scratch_2/ahttp://localhost/var/www/apps/conversion/releases/20121024195125/tmp/scratch_2/a7/31/2019 UNIX Fundamentals 2010
29/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200929 | Corporate Overview | January 2000
Listing Contents
ls [options] dirname list the contents of dirname
options:
-a list all files including hidden files
[hidden files are preceded by a .; eg .cshrc]
-l long listing showing ownership, permissions and links
examples:
ls /home/user/temp view the contents of a directory with absolute
pathname /home/user/temp
ls ../../temp list the contents of a directory using a relativepath.
pwd display full pathname of current directory
7/31/2019 UNIX Fundamentals 2010
30/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200930 | Corporate Overview | January 2000
Make Directory
mkdir dirname create the directory dirname
examples:
mkdir work create the directory work/ in the current working
directorymkdir work/proj1 create the directory proj1/ in the work/ directory
mkdir /wrk/user2 create the directory user2/ in the /wrk/ directory
7/31/2019 UNIX Fundamentals 2010
31/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200931 | Corporate Overview | January 2000
Change Directory
cd change to your home directory
cd dirname change to the directory dirname
examples:
cd ~tom change to toms home directory
cd /wrk/user2 change to the directory /wrk/user2cd .. change to the parent directory
cd ../.. Get up two levels
7/31/2019 UNIX Fundamentals 2010
32/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200932 | Corporate Overview | January 2000
Moving or Renaming
mv [options]presname newname rename a file
mv [options] srcfile destdir move a file to another directory
options:
-i confirm overwrites
example:
mv ~user1/file ./outputfile moves file from user1s home directory to
the current working directory and renames it
output file
Note: Be careful when overwriting files!
7/31/2019 UNIX Fundamentals 2010
33/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200933 | Corporate Overview | January 2000
Copying
cp [options] srcfile destfile copy a file to another filename
cp [options] srcfile destdir copy a file to another directory
options:
-i confirm overwrites
-R recursively copy a directory and its contents,
copies symbolic links
example:
cp -R proj1 proj2 copy the directory proj1/ and name it proj2/
Note: Be careful when overwriting files!
7/31/2019 UNIX Fundamentals 2010
34/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200934 | Corporate Overview | January 2000
Removing
rm [options]filename remove a file
options:
-f remove without prompting
-i confirm removal
-r recursively remove a directory and its contents
example:
rm -rf /scratch/user2/dir1 remove the directory /scratch2/user2/dir1
and its contents
!!!WARNING!!! This will DELETE EVERYTHING in that directory!!!
You can not recover your files after you removed them (unlike Windows OS).
7/31/2019 UNIX Fundamentals 2010
35/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200935 | Corporate Overview | January 2000
Update Timestamp on File
touch [options]file update access and/or modification times of file
Options:
-a Changes the access time of the file
-m Changes the modification time of the file
-c Does not create the file if it does not already exist
Examples:ls -l-rw-r--r-- 1 user1 users 25936 Apr 24 09:53 firstfile-rw-r--r-- 1 user1 users 10245 Apr 24 09:53 secondfiletouch newfilels -l-rw-r--r-- 1 user1 users 25936 Apr 24 09:53 firstfile
-rw-r--r-- 1 user1 users 0 Apr 25 10:02 newfile-rw-r--r-- 1 user1 users 10245 Apr 24 09:53 secondfiletouch secondfilels l-rw-r--r-- 1 user1 users 25936 Apr 24 09:53 firstfile-rw-r--r-- 1 user1 users 0 Apr 25 10:02 newfile-rw-r--r-- 1 user1 users 10245 Apr 25 10:05 secondfile
7/31/2019 UNIX Fundamentals 2010
36/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200936 | Corporate Overview | January 2000
Catenate / Type
catfilename 1) displays the contents of file on the screen, one after
the other .
2) You can also use it to create files from keyboard input
as follows (> is the output redirection operator)
example:cat > hello.txt
hello world!
[ctrl-d]
ls hello.txt
hello.txtcat hello.txt
hello world!
7/31/2019 UNIX Fundamentals 2010
37/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200937 | Corporate Overview | January 2000
Catenate with pause
morefilename 1) displays the contents of file on the screen, pausing at the
end of each screenful and asking the user to press a key
2)You can also use more to break up the output of commands
that produce more than one screenful of output
example:more output.c
ls -l | more
(| is the pipe operator)
7/31/2019 UNIX Fundamentals 2010
38/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200938 | Corporate Overview | January 2000
Making links file
lnfilename linkname Make hard links from one file or directory to another
ln -sfilename linkname Make soft (or symbolic) links from one file or
directory to another
example:
ln -s hello.txt bye.txtls -l bye.txt
lrwxrwxrwx 1 will finance 13 bye.txt -> hello.txt
7/31/2019 UNIX Fundamentals 2010
39/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200939 | Corporate Overview | January 2000
Process Control
7/31/2019 UNIX Fundamentals 2010
40/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200940 | Corporate Overview | January 2000
Processes
A process is a program in execution identified by a unique PID (process
identifier).
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.
A process may be in the foreground, in the background, or be suspended.
7/31/2019 UNIX Fundamentals 2010
41/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200941 | Corporate Overview | January 2000
Listing Process
ps [options] display the status of the current processes and the process
id-number
options:
-e all processes now running
-u display processes owned by a particular user
jobs shows any jobs that are currently running in the background
or suspended
7/31/2019 UNIX Fundamentals 2010
42/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200942 | Corporate Overview | January 2000
Process Control
-C cancel a foreground job
-Z stop (interrupt) a foreground job
bg run stopped job in the background
fg run stopped job in the foreground
& appended to the end of a command will place that job in the
background
examples:
$ sleep 1000 &
$ man ls-Z
$ jobs
$ fg %2
$ jobs[2] + Stopped (SIGTSTP) man ls
[1] - Running sleep 1000 &
?
7/31/2019 UNIX Fundamentals 2010
43/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200943 | Corporate Overview | January 2000
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
27501 pts/10 00:00:01 find
27502 pts/10 00:00:00 ps
kill 27501
kill -9 27501 If a process refuses to be killed, uses the -9 option
7/31/2019 UNIX Fundamentals 2010
44/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200944 | Corporate Overview | January 2000
STDINT,STDOUT and STDERR
Reset
BreakStop
f1 f2 f 3 f4 f5 f6 f7 f8
Back
Space
ReturnCTRL
User
System
. / + -
7 8 9
4 5 6 ,
1 2 3
0 .
Menu
TabNextSelect
Prev
Insert
line
Delete
line
Insert Delete
char char
|
\
}
]
{
[
=
+
_
-
"
'
:
;
Shift
Extend
char
?
/
>
.
command output redirection (create new, overwrites existing file)
>> command output redirection (append)
Examples:
Create/Overwrite Create/Append
$ ls > filelist.out $ ls >> filelist.out
$ cat seq1 seq2 > seq $ cat seq1 >> seq2
(combine individual sequence files into one file)
7/31/2019 UNIX Fundamentals 2010
47/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200947 | Corporate Overview | January 2000
Error Redirection
2> error output redirection (create new, overwrites existing file)
2>> error output redirection (append)
Examples:
$ cp 2> cp.err Create/Overwrite
$ cp 2>> cp.err 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_filecp [-f|-i] [-p] source_file ... target_directory
cp [-f|-i] [-p] -R|-r source_directory...target_directory
7/31/2019 UNIX Fundamentals 2010
48/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200948 | Corporate Overview | January 2000
pipes
command1 | command2 | directs standard output of one command into
standard input for the next command
examples:
ls -al | more look at the ls output one page at a time
cat hello.txt | sort | uniq creates three processes (corresponding to cat,
sort and uniq) which execute concurrently. As
they execute, the output of the who process is
passed on to the sort process which is in turn
passed on to the uniq process. uniq displays its
output on the screen (a sorted list of users
with duplicate lines removed).
7/31/2019 UNIX Fundamentals 2010
49/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200949 | Corporate Overview | January 2000
Introduction to vi
7/31/2019 UNIX Fundamentals 2010
50/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200950 | Corporate Overview | January 2000
Introduction to vi
vi is the standard UNIX text editor
very powerful
useful simple subset of commands
portable (PCs, mainframes, etc.)
designed for slow networks
full-screen
Starting vi
vifilename
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.
7/31/2019 UNIX Fundamentals 2010
51/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200951 | Corporate Overview | January 2000
Command mode
move cursor, save, delete text, quit vi, etc.
Last-line mode
Initiated from command mode by entering advanced editing
commands like :, /, ?, !.
Commands are shown on the status line (Bottom line )
Input mode
for inserting text
start by typing i; finish with ESC
cannot quit, delete, etc. in this mode
If in doubt, press ESCa few times. This will put you back in
command mode.
Three Modes
7/31/2019 UNIX Fundamentals 2010
52/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200952 | Corporate Overview | January 2000
Cursor movement
h left
j downk up
l right
^ beginning of line
$ end of line
1 G top of document
G end of document
G go to line
^F page forward
^B page backward
w word forwards
b word backwards
l
7/31/2019 UNIX Fundamentals 2010
53/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200953 | Corporate Overview | January 2000
Inserting and typing text
i insert text (and enter input mode)
a append text (and enter input mode)o start a new line (and enter input mode)
Move to insertion point
Switch to input mode: i
Start typing; BACKSPACE or DELETE for deletion
ESC finish; back in command mode
No RETURN
7/31/2019 UNIX Fundamentals 2010
54/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200954 | Corporate Overview | January 2000
Deleting
Backspace Delete character before cursor (only works in insert mode)
Must be in command mode
x Delete character that cursor is on.
dd Delete current line.
dd Delete n lines
D Delete from cursor position to end of line
:i,jd Delete lines i to j
:23,29d Delete lines 23 to 29
:.,$d Delete from current line to the end of file.
u Undo last command
7/31/2019 UNIX Fundamentals 2010
55/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200955 | Corporate Overview | January 2000
Deleting and moving
Cut & Paste with Deleted Text
dd or dd or D delete from screen and store text in a buffer
move cursor to new location
p paste contents of buffer to right of cursor position
Copy & Paste
yy or yy yank/copy lines and store text in a buffer
move cursor to new location
p paste contents of buffer to right of cursor position
7/31/2019 UNIX Fundamentals 2010
56/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200956 | Corporate Overview | January 2000
Searching for Text
There is one here
and one more here
and yet one more
but not this ONE
nor this One
/one
next
previous
n
N
a56686
type / and then a regular expression and pressEnter
7/31/2019 UNIX Fundamentals 2010
57/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200957 | Corporate Overview | January 2000
Global Search and replace
:s/old/new/g replace every occurrence of old by new
:i,js/old/new/g replace every occurrence of old by new between lines i and j
:s/Hat/Head/g
:2,200s/Andy/Andrew/g
:1,.s/fc/function/g from line 1 to current
7/31/2019 UNIX Fundamentals 2010
58/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200958 | Corporate Overview | January 2000
Finishing a vi Session
Get to command mode (press ESC)
:w save file (not quit):wq save file and quit
ZZ save file and quit
:q! quit without saving
:w! newfile save as the file 'newfile', overwriting any existing newfile
7/31/2019 UNIX Fundamentals 2010
59/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200959 | Corporate Overview | January 2000
Customizing Your Shell
Environment
7/31/2019 UNIX Fundamentals 2010
60/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200960 | Corporate Overview | January 2000
Shells and Shell Scripts
A shell is a program that takes your commands from the keyboard and gives them
to the operating system to perform.Shells also usually provide features such job control, input and output redirection
and a command language for writing shell scripts.
A shell script is simply an ordinary text file containing a series of commands in a
shell command language (just like a "batch file" under MS-DOS).
There are many different shells available on UNIX systems (e.g. sh, bash, csh, ksh
etc.), and they each support a different command language.
7/31/2019 UNIX Fundamentals 2010
61/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200961 | Corporate Overview | January 2000
View your login shell
Determine your login shell:
echo $SHELL
Change to another shell, just type the shell name:
sh (or csh or ksh or bash)
Or
exec sh (close the original shell, then open another shell sh)
Go back to your normal shell
exitorpress CTRL-D
7/31/2019 UNIX Fundamentals 2010
62/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200962 | Corporate Overview | January 2000
Changing your Login Shell
To change your Unix shell, you will need to be at a Unix shell prompt.
type the command
$ 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:
enter the full name of your new shell.
Old shell: /usr/local/bin/bashNew shell:
7/31/2019 UNIX Fundamentals 2010
63/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200963 | Corporate Overview | January 2000
Initialization files
Each time you login to a UNIX host, the system looks in your home directory forinitialization files. You can change your environment by changing initialization
files.
Shell System initializationfiles First
User initializationfiles secondly
Template/etc/skel
sh(Bourne
shell)
/etc/profile $HOME/.profile local.profile
ksh(Kornshell)
/etc/profile $HOME/.profile$HOME/.kshrc
local.profile
csh(C shell) /etc/.login $HOME/.cshrc$HOME/.login
local.cshrclocal.login
bash(BourneAgain shell )
/etc/profile $HOME/.bash_profile$HOME/.bashrc
local.profile
$HOME/.cshrc
$HOME/.login
7/31/2019 UNIX Fundamentals 2010
64/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200964 | Corporate Overview | January 2000
Setting shell variables
At login the C shell first reads .cshrc followed by .login
.login is to set conditions which will apply to the whole session and to performactions 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 SHELLvariables in the .cshrc file.
Setting shell variables in the .cshrc file
$ vi ~/.cshrcAdd the following line AFTER the list of other commands
set history = 200Save the file$ source .cshrc (force the shell to reread its .cshrc file )$ echo $history (Check this has worked)
7/31/2019 UNIX Fundamentals 2010
65/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200965 | Corporate Overview | January 2000
Setting Environment Variables
View all system variables by the command env
By convention system variables are capitalized
PATH a list of directories that the shell uses to locate executable files for commands
HOME Name of your home (login) directory
PWD Current directory
OLDPWD 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
7/31/2019 UNIX Fundamentals 2010
66/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200966 | Corporate Overview | January 2000
Alias
alias new=original defines new to be an alias for original (ksh or bash)
alias new original (csh)
example:
alias ct=cleartool
alias ll='ls l'
alias logout='. ~/.ksh_logout; exit'
7/31/2019 UNIX Fundamentals 2010
67/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200967 | Corporate Overview | January 2000
Other Basic Commands
h d
7/31/2019 UNIX Fundamentals 2010
68/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200968 | Corporate Overview | January 2000
Other Basic Commands
tar (tape archive) ; create archives
compress compress files
gzip compress files
find find files and directories with a wealth of search criteria
grep (general regular expression parser); text search
sort sort information provided on standard inputssh (secure shell) ; remote login
A hi d S
7/31/2019 UNIX Fundamentals 2010
69/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200969 | Corporate Overview | January 2000
Archives and Storage
tar create archives, and read/write from tapes and diskettes
tar -cvf archivenamefilenames create a disk file tar archive
tar -tvf archivename list the contents of a tar archive
tar -xvf archivename restore files from a tar archive
compress /gzip compress files
compressfile compressfile and rename itfile.Z
uncompressfile.Z uncompressfile.Zand rename itfile
gzipfile compressfile and rename itfile.gz
gunzipfile.gz uncompressfile.gzand rename itfile
Fi di Fil
7/31/2019 UNIX Fundamentals 2010
70/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200970 | Corporate Overview | January 2000
Finding Files
find Find files and directories with a wealth of search criteria
Example:
find directory-name targetfileprint
look for a file called targetfile in any part of the directory tree rooted at
directory.
find /home -name "*.txt" -print 2>/dev/null
search all user directories for any file ending in ".txt" and output any matching
files. The 2>/dev/null suppresses error messages.
find . -name "*.txt" -exec wc -l '{}' ';'
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.
S hi T t
7/31/2019 UNIX Fundamentals 2010
71/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200971 | Corporate Overview | January 2000
Searching Text
grep options patternfiles 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)
S hi T t ( t )
7/31/2019 UNIX Fundamentals 2010
72/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200972 | Corporate Overview | January 2000
Searching Text (cont.)
Examples:
grep -vi hello *.txt searches all text files in the current
directory for lines that do not contain
any form of the word hello
(e.g. Hello, HELLO, or hELlO).
grep hello `find . -name "*.txt" -print` search all text files in the directory tree
rooted at the current directory for lines
containing the word "hello".
grep ^..[l-z]$ hello.txt matches any line in hello.txt thatcontains a three character sequence that
ends with a lowercase letter from l to z.
S ti g fil
7/31/2019 UNIX Fundamentals 2010
73/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200973 | Corporate Overview | January 2000
Sorting files
sort [options]filenames sort information provided on standard input
Options:
-r Reverses the sense of comparisons
Examples:
sort input1.txt input2.txt > output.txt
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
7/31/2019 UNIX Fundamentals 2010
74/75
All Rights Reserved Alcatel-Lucent Shanghai Bell 200974 | Corporate Overview | January 2000
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 sets environment variables for porting X-display
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
7/31/2019 UNIX Fundamentals 2010
75/75
www.alcatel-sbell.com.cn
Thanks!