RH033
Redhat Enterprise Linux Essentials
(C) Mustafa Golam, 2007 1
Unit 1
Redhat Linux Essentials
Overview
(C) Mustafa Golam, 2007 2
Redhat Enterprise Linux
Certified with Leading OEM and ISV Products
ISV Independent Software Vendor
OEM Original Equipment Manufacturer
ABI/API Compatibility
Application Binary Interface
Application Programming Interface
ELF Executable and Linking Format
LSB Linux Standards Base
BEA BEA System Inc, San Jose, California. “Think Liquid”
HPC High Performance Computing
(C) Mustafa Golam, 2007 3
Unix Flavors
UNIX from AT&T, at Bell Labs, in 1969.
AIX from IBM
HP/UX from Hewlett-Packard
SunOS (Later Solaris) from Sun
IRIX from SGI (Silicon Graphics)
(C) Mustafa Golam, 2007 4
Unix Principles
Everything is a file.
Including hardware
Configuration data stored in text
Small, single-purpose programs
Avoid Captive User Interfaces
Ability to chain programs together to
perform complex tasks.
(C) Mustafa Golam, 2007 5
GNU Project / FSF
GNU Project started in 1984
Goal : Create “Free” UNIX clone
By, 1990, Nearly all required userspace application
created.
gcc, emacs etc.
Free Software Foundation
Non-profit Organization that manages the GNU
Project
Four Freedoms
http://www.gnu.org/philosophy/free-sw.html
(C) Mustafa Golam, 2007 6
GPL – GNU General Public License
Primary license for Open Source Software
Encourages free software
All enhancements and changes to GPL-software must
also be GPL
Often called “CopyLeft”
“All rights Reversed”
http://www.gnu.org/copyleft/gpl.html
X Window System, has it’s own T&C.
BSD code need to be abide the terms of Berkeley
Software Distribution. Visit http://www.bsd.com
(C) Mustafa Golam, 2007 7
Linux Origins
Linux Torvalds
Finish College Student in 1991
Created Linux Kernel
When Linux Kernel Combined with GNU
Applications, complete free UNIX-like OS
possible.
(C) Mustafa Golam, 2007 8
Why Linux?
Fresh Implementation of UNIX APIs.
Open Source Development model.
Supports wide variety of hardware.
Supports many networking protocols and
configurations.
Fully supported.
Linux is a UNIX-like OS.
Multi-user and Multi-tasking.
Wide hardware support. Checklist.
Fully Supported.
(C) Mustafa Golam, 2007 9
Redhat Enterprise Linux
A distribution of Linux
Custom version of a recent Linux Kernel.
Utilities and applications.
Installation and configuration software
Support available.
RHEL includes installation and configuration
software, a patched and rigorously tested recent
version of the Linux Kernel, and thousands of
utilities and applications.
gdm is the default display manager for RHEL.
(C) Mustafa Golam, 2007 10
Recommended Hardware Specifications
for RHEL
Pentium Pro or better with 256 MB RAM
Or
64-bit Intel/AMD with 512 MB RAM
2-6 GB Disk Space
Bootable CD
Other processor architectures supported
Itanium 2, IBM Power, IBM Mainframe.
(C) Mustafa Golam, 2007 11
Virtual Consoles
Virtual Consoles are available through CTRL – ALT –
F[1-6]
If X is running, it is available as CTRL – ALT – F7
The virtual consoles enable a user to have multiple
logins even when not using an X Window system.
You can scroll at the virtual consoles by using SHIFT-
PAGEUP and SHIFT-PAGEDOWN. The scroll buffer is
stored in video memory, so if you are running a
graphical program such as X, the scroll buffer is lost.
(C) Mustafa Golam, 2007 12
The Xorg GUI Framework
The goal of X.Org include a faster and more open development model
than Xfree86, support for a wide variety of video cards and input
devices and the development of a highly modular and flexible graphical
framework for Unix and Linux.
http://xorg.freedesktop.org
Or
http://www.x.org/wiki
X Client activity that spawns an X server event informing the web
browser to send an HTTP request to the link’s target ( or anchor). You
do not really see the X server, but X clients.
X provides the data I/O infrastructure for X clients, like a human
nervous system, it sends messages when touched by client activity.
An X Client running on one system can display on any X server running
on any operating system, if sufficient access is granted.
For each managed display, this socket is /tmp/.X11-unix/X# where # is
0 to the greatest number of permitted connections.
(C) Mustafa Golam, 2007 13
Xorg Graphical Environments
Collections of applications that provide
a graphical working environment with a
consistent look and feel.
GNOME (GNU Network Object Model
Environment) The default desktop
environment.
KDE ( K before L) Environment
based on the Qt toolkit.
(C) Mustafa Golam, 2007 14
Starting Xorg
Nothing needed if system boots to a
graphical login. Just authenticate.
If system boots to a virtual console
login, Xorg must be started manually.
Run startx to manually start Xorg.
Changing Password.
At least 6 but no more that 255 characters.
Other criteria depends on particular OS.
(C) Mustafa Golam, 2007 15
Unit 2
Running Commands and Getting Help
(C) Mustafa Golam, 2007 16
Getting Help
--help
whatis -- with makewhatis
man
info
/usr/share/doc/html/en/
Software source code
Documentations
(C) Mustafa Golam, 2007 17
Running Commands
Commands have the following syntax:
Command [ options] [arguments]
Each item is separated by a space.
Options modify the command’s behavior
Word options usually preceded by “--”
Single-letter options usually preceded by “-”
Can be passed as “-a –b –c “ or “-abc”
Arguments are filenames or other date needed
by the commands.
(C) Mustafa Golam, 2007 18
The –help option
Anything in straight braces ([]) is optional.
Anything followed by “…” represents an arbitrary-
length list of that thing.
If you see multiple options separated by pipes (|), it
means you can use any one of them.
Text in straight brackets(<>) represents variable
date. So <filename> means “insert the filename you
wish to use here”.
Sometimes, such variables are simply written in all
CAPS.
(C) Mustafa Golam, 2007 19
The man Command
The collection of all man pages on a system is called the Linux
Manual. The Linux is divided into sections, each of which covers
a particular topic, and every man page is associated with
exactly one of these sections.
The sections are:
Manual Sections.
1 User Commands
2 System Calls
3 Library Calls
4 Special Files
5 File Formats
6 Games
7 Miscellaneous
8 Administrative Commands
man and man –k commands
(C) Mustafa Golam, 2007 20
The info command
info pages are structured like a website.
Each page is divided into “nodes”.
Links to nodes are preceded by “*”.
info [<command>]
If you run info with no arguments you
will be presented with a list links to the
top nodes of every available info page.
(C) Mustafa Golam, 2007 21
Navigating info Pages
While viewing an info page
Navigate with arrows, pgUp, pgDown
Tab Move to next link
Enter Follow link the cursor is on
n/p/u View next/previous/up-one node
s[<text>] Search for text (default: last search)
q Quit Viewing page
If you prefer the navigation keys used by man, such
as using “/”, “n” and “N” to search, you can start info
with the –vi-keys arguments.
(C) Mustafa Golam, 2007 22
Extended docs
/usr/share/doc
http://www.redhat.com/docs
(C) Mustafa Golam, 2007 23
Miscellaneous
‘man –f foo’ is the same as ‘whatis foo’.
If your system has just been installed,
neither man –f nor whatis will work
until an administrator runs makewhatis.
ls –lh
(C) Mustafa Golam, 2007 24
Unit 3
Browsing the File system
(C) Mustafa Golam, 2007 25
Linux File Hierarchy Concepts
Files and directories are organized into a
single-rooted inverted tree structure.
Filesystem begins at the “root” directory,
represented by a lone “/” (Forward slash)
character.
Names are case-sensitive.
Paths are delimited by “/”.
A user’s path is a list of directories that are
searched for commands typed at the
command line.
(C) Mustafa Golam, 2007 26
Some Important Directories
The essential binaries necessary to boot and maintain the
system reside in /bin for regular binaries and /sbin for
system binaries.
Non-essential binaries, such as graphical environments,
web browsers, office tools and so forth, are installed in
/usr/bin and /usr/sbin.
On a newly installed system, there will also be /usr/local/bin
and /usr/local/sbin directories, but they will be empty.
CD-ROM is mounted under /media/cdrom folder.
Filesystems that are on non-removable media but are not
part of the RHEL, hierarchy are usually mounted under
/mnt.
Visit http://www.pathname.com/fhs
(C) Mustafa Golam, 2007 27
Other Important Directories
/etc -System Configuration Files
/tmp -Temporary files
/boot -Kernel and boot loader
/var and /svr -Server data
/proc and /sys -system Information
The lib directories
/lib, /usr/lib, /usr/local/lib
(C) Mustafa Golam, 2007 28
On Folders
Once a day the system automatically deletes
any files over seven days old in /tmp and it’s
subdirectories.
The /var directory contains regularly-changing
system files such as logs, print spools and
email spools.
In the future, server data may be moved to
the /srv directory so that /var only contains
logs, spools and so forth.
Each shell and system process has a current
working directory (cwd).
(C) Mustafa Golam, 2007 29
File and Directory Names
Names may be up to 255 characters.
All characters are valid, except the “/”.
It may be unwise to use certain special
characters in file or directory names.
Some characters should be protected with quotes when
referencing them.
Names are case sensitive.
Example: MAIL, Mail, mail and mAiL
Again, possible but may not be wise.
To access a file with name contains special
characters, enclose the filename in quotes.
(C) Mustafa Golam, 2007 30
Absolute Pathnames
Absolute pathnames begin with a slash (/)
Complete “road map” to file location.
Can be used anytime you wish to specify a file
name.
To change to your previous working directory
$ cd –
A dash (-) represents your previous working
directory. It’s a handy shortcut to use to switch
back and forth between two directories.
(C) Mustafa Golam, 2007 31
Copying Files and Directories
cp – copy files and directories
Usage:
cp [options] file destination
More than one file may be copied at a time if the
destination is a directory.
cp [options] file1 file2 dest
If final argument is an existing directory, a copy of
the source files are placed in that directory with the
same name as the source.
Otherwise, the destination is interpreted as a file
name, and a copy of the source file is created with
that.
(C) Mustafa Golam, 2007 32
On Copying
When copying a single file to a destination, cp
first checks to see if a directory exists with
the destination name.
If it does, a copy of the source file is placed
there with it’s original name.
If not, the destination is assumed to be a new
file name, and a copy of the source file is
made with the destination name.
(C) Mustafa Golam, 2007 33
Moving and Renaming Files
and Directories
mv – move and/or rename files and directories.
Usage:
mv [options] file destination
More than one file may be moved at a time if the
destination is a directory.
mv [options] file1 file2 dest
If the destination directory does not exit, the slash will
cause the command to fail with an error message.
If the destination directory does not exist (but the
destination pathname is valid), the source directory
will be moved to the destination directory with the
new name.
(C) Mustafa Golam, 2007 34
Creating and Removing Files
rm – remove files
Usage:
rm [options] filenames…
-i – interactive
-r – recursive
-f – force
touch – create empty files or update file timestamps
The –r option tells rm to remove files recursively and thus it will
delete directories and their contents.
There is no way to undo the effects of rm, except to restore
from a backup.
rmdir will only remove empty directories. To remove a directory
and it’s contents, use rm –r.
(C) Mustafa Golam, 2007 35
Using Nautilus
Gnome graphical Filesystem browser
Can run in “Spatial” or “Browser” mode
Accessed via …
Desktop icons
Home: Your home directory
Computer: Root Filesystem, network resources
and removable media.
“File Browser” option on Application menu.
Typing CTRL-SHIFT-w closes all parent
windows.
“Spatial” or “Browser Mode” can be edited.
(C) Mustafa Golam, 2007 36
Moving and Copying in Nautilus
Drag and Drop
Left-button: Move on same filesystem,
copy on different system.
Ctrl-Left-button : Always Copy
Ctrl-Left-button: Ask whether to copy,
move or create symbolic link (alias)
Context Menu
Right-click or rename, cut, copy or paste.
(C) Mustafa Golam, 2007 37
Determining File Content
Files can contain many types of data.
Check file type with file before opening to determine
appropriate command or application to use.
Syntax
file [options] filename(s)
file prints its best guess of the type of data contained
in a file whose name is given as an argument. It
bases it’s guess on a comparison of the contents of
the file and the patterns and offsets in it’s reference
file, /usr/share/magic.
(C) Mustafa Golam, 2007 38
Viewing an Entire Text File
Syntax:
cat [options] [files..]
Contents of the files are displayed sequentially
with no break.
Files display “concatenated”.
-A Show all characters, including control
characters and non-printing characters.
-s “Squeeze” multiple adjacent blank lines
into a single blank line.
-b Number each (non-blank) line of output.
(C) Mustafa Golam, 2007 39
Viewing Text Page by Page
Syntax:
less [options] [filename]
Scroll with arrows/pgUp/pgDown
Useful commands while viewing:
/text --search for text
n --next match
v -- open file in text editor.
less is the pager used by man
(C) Mustafa Golam, 2007 40
On less
Less navigation commands:
<space> move ahead one full screen
<return> move ahead one line
<Ctrl-d> move ahead (down) half a
screen
g move to the top of the file.
/text search forward for text
n repeat last search
q quit
b move back one full screen
k move back one line
<Ctrl-u> move back (up) half a screen
G move to the bottom of the file
?text search backward for text
N repeat last search, but in opposite direction.
v open file in a text editor (vi by default)
(C) Mustafa Golam, 2007 41
less options
-c Clear before displaying. Screen is
redrawn instead of scrolled between
screens.
-r Display raw control characters. (May
cause display problems)
-s “squeeze” multiple blank lines into a single
blank line
-e Exit the second time it reaches the end of
the line.
-m Verbose prompt, like more (by default it
uses a colon).
(C) Mustafa Golam, 2007 42
Unit 4
The bash Shell
(C) Mustafa Golam, 2007 43
bash introduction
“Bourne Again Shell”
Successor to sh, the original Unix shell
Developed for the GNU Project
The de factor standard Linux Shell
Backward-compatible with Bourne shell
(sh)- the original (Standard) UNIX shell
(C) Mustafa Golam, 2007 44
bash Heritage and Features
Bourne Again Shell (bash)
Implements many of the best features
from earlier shells: sh, csh, ksh , tcsh
Command line completion
Command line editing
Command line history
Sophisticated prompt control
(C) Mustafa Golam, 2007 45
Command Line Shortcuts:
File Globbing
Globbing is wildcard expansion:
* - matches zero or more characters
? – matches any single character
[a-z] – matches a range of characters
[^a-z] – matches all except the range.
The use of wildcards, or metacharacters, allows one
pattern to expand to multiple filenames by a process
called globbing.
RHEL uses UTF-8 encoding of characters, which
means that each capital letter comes directly after
the corresponding lower-case letter.
Use echo ?o* to find the effect of wild characters.
(C) Mustafa Golam, 2007 46
Some topics: Tab, history, ~
The TAB Key
history
!! – to repeat last command
!c – to repeat last command that started with c
!n – to repeat a command by its number in history output
!?abc – to repeat last command that contains (as opposed to
“started with”) abc
!-n – to repeat a command entered n commands back.
User ^old^new to repeat the last command with old changed to
new.
Tilde(~)
May refer to your home directory ( ~/.bash_profile)
May refer to another user’s home directory. (~gmustafa/.bashrc)
(C) Mustafa Golam, 2007 47
Command Line Expansion:
Variable and String
Parameter/Variable: ($)
Substitute the value of a variable in a command
line.
$ cd $HOME/public/html
To see a list of variables and their values, run the
set command.
Curly braces: { }
A string is created for every pattern inside the braces
regardless if any file exists.
$ rm hello.{c,o}
Curly braces are useful for generating patterned strings.
(C) Mustafa Golam, 2007 48
Command Line Expansion:
Command and Math
Command Output – ‘’ or $ ()
Substitute output from a command in a command
line.
$ echo “Hostname: `hostname`”
$ echo “Hostname: $(hostname)”
Arithmetic - $[]
Substitute result of arithmetic expression in a command
line.
$echo Area : $[$X * $Y]
$echo Area : `expr $X\*$Y`
(C) Mustafa Golam, 2007 49
Protecting form Expansion:
Backslash
Backslash (\) is the escape character
and makes the next character literal.
$echo Your Cost :\$5.00
Used as last character on line to
“Continue command on next line”
$find / -name foo*
$find / -name foo\*
(C) Mustafa Golam, 2007 50
Protecting From Expansion:
Quotes
Quotes prevents expansion
Single quotes (‘) inhibit all expansion
$echo ‘*** SALE ***`
Double quotes(“) inhibit all expansion except :
$ (dollar sign variable expansion
‘ (backquotes), command substitution
\ (backslash), single character inhibition
! (exclamation point), history substitution
Double quotes inhibit file name generation
expansion, but not other types.
(C) Mustafa Golam, 2007 51
History Tricks
Use the up and down arrow keys to scroll through
previous commands.
Type <CTRL-R> to search for a command in
command history.
{reverse-i-search)`’:
To recall last argument from pervious command:
<ESC>, (the escape key followed by a period)
<ALT -.> (hold down the alt key while pressing the period)
You can ignore repeated duplicate commands and
repeated lines that only differ in pre-pended spaces
by adding the following in your .bashrc
export HISTCONTROL=ignoreboth
(C) Mustafa Golam, 2007 52
Command Editing Tricks
<Ctrl-a> moves to beginning of
line.
<Ctrl-e> moves to the end of line.
<Ctrl-u> deletes to beginning of
line
<Ctrl-k> deletes to end of line.
<Ctrl-arrow> moves left or right by
word.
* Inherited from emacs.
(C) Mustafa Golam, 2007 53
Command Editing Tricks:
Editing Modes
By default, bash uses emacs-style
keybindings for command editing.
Type set –o vi to change keybinding to
vi-style.
Make vi the default by adding the
command above to $HOME/.inputrc
(C) Mustafa Golam, 2007 54
gnome-terminal
Applications/System Tools/Terminal
Graphical terminal emulator with
support for maintaining multiple
“tabbed” shells simultaneously.
<Ctrl-Shift-t> Open a new tab
<Ctrl-PgUp/PgDn> Next/Prev tab
<Ctrl-Shift-c/v> Copy/Paste
(C) Mustafa Golam, 2007 55
Unit 5
Standard I/O and Pipes
(C) Mustafa Golam, 2007 56
Standard Input and Output
Linux provides three I/O channels to
processes
Standard input – keyboard is default
Standard Output – Terminal windows is default
Standard Error – Terminal window is default.
Error messages could be saved in a file with
the normal output going to the monitor.
0,1,2 are called file descriptor.
(C) Mustafa Golam, 2007 57
Redirecting Input and Output
Standard Input, Output, and Error can be
reconnected to alternate locations.
Shell redirection operators allow standard I/O
channels to be redirected to/from a file.
Pipes allow standard I/O channels to connected to
the input or output of programs.
Although it is also possible to pipe standard
error into a file using some fairly complex
syntax, this is generally not done.
(C) Mustafa Golam, 2007 58
Redirecting and Piping
Redirecting
> command > file Directs standard output of command to file
>> command > file Appends standard output of command to file.
< command < file command receives it’s input from file.
2> command 2>file Error messages from command are directed to file.
2>> command 2>>file Error messages from command are appended to file.
Piping
| command1 | command 2 Pipes the standard output of command1 into the
standard input of command2.
(C) Mustafa Golam, 2007 59
Overwriting or Appending
If the target file of file redirection with >
already exists, the existing file will be
overwritten.
To append data to an existing file, use >> to
redirect instead of >.
Redirect standard output with >
Example: redirect standard output to a file.
Find /etc –name passwd > findresult.
Standard error is still displayed on the screen.
(C) Mustafa Golam, 2007 60
Redirecting Standard Error
Redirect Standard Error with 2>
Example: redirect standard error to a file:
find /etc –name passwd 2> finderrors
Standard output is displayed on the
screen.
Redirecting further standard error,
appending to the same file, with 2>>.
(C) Mustafa Golam, 2007 61
Redirecting Both Standard
Output and Error
Redirection of Standard Output and
Standard Error can be performed
simultaneously.
find / -name passwd 2> errs > results
Each I/O Channel can be redirected to
different files, or to the same file:
find / -name passwd > alloutput 2>&1
(or) find / -name passwd &> alloutput
(C) Mustafa Golam, 2007 62
Redirecting Input
Redirecting Standard Input with <
Some commands only operate on
Standard Input
tr doesn’t accept filenames as arguments-
it requires it’s input to be rediected from
somewhere.
tr ‘A-Z’ ‘a-z’ <.bash_profile
This command will translate the uppercase
characters in .bash_profile to lowercase.
(C) Mustafa Golam, 2007 63
Using Pipes To Connect Processes
Pipes ( the | character) let you redirect
output from one command to become
the input to another command.
$ ls /usr/lib | less
Can create pipelines –a powerful
feature of Linux.
$ cut –f1 –d: passwd | sort –r | less
(C) Mustafa Golam, 2007 64
On Pipes
Two of the basic tenets of UNIX philosophy are:
Make small programs that do one thing well.
And
Expect the output of every program to become the input to
another, as yet unknown, program.
And then Use pipes to do complex jobs on the fly.
Any command that writes to standard output can be used on
the left-hand side of a pipe.
Any command that reads from standard input can be used
on the right-hand side of a pipe.
Multiple commands can be chained together with pipes.
(C) Mustafa Golam, 2007 65
Useful Pipe Targets
ls –l | less
ls – l | mail –s “Files”
[email protected] ls –l | lpr
cat files_to_delete.txt | xargs rm –f
tee
Lets you tee a pipe: redirect output to a file while still piping
it to another program.
$set | tee set.out | less
In example, output from set is written to file set.out while
also being piped to less.
Command | tee stage1.out | sort | tee stage2.out | uniq –c | \
tee stage3.out | sort –r | tee stage4.out | less
(C) Mustafa Golam, 2007 66
Unit 6
Users, Groups and Permissions
(C) Mustafa Golam, 2007 67
The Linux Security Model
Users and groups are used to control access
to files and resources.
User log in to the system by supplying their
user name and password.
Every file on the system is owned by a user
and associated with a group.
Every process has a owner and group
affiliation, and can only access the resources
it’s owner or group can access.
(C) Mustafa Golam, 2007 68
Users
Every user of the system is assigned a unique
User ID number( the uid).
Users’ names and uids are stored in
/etc/passwd
Users are assigned a home directory and a
program that is run when they log on (Usually
a shell).
User cannot read, write or execute each
others’ files without permission.
(C) Mustafa Golam, 2007 69
Groups
Users are assigned to groups with unique
group ID numbers (the gid).
gids are stored in /etc/group.
Each user is given their own private group.
They can also be added to other groups to gain
additional access.
All users in a group can share files that
belong to the group.
The primary group can be changed using the
newgrp command.
(C) Mustafa Golam, 2007 70
The root user
The root user: a special administrative
account.
Sometimes called the superuser.
root has complete control over the system.
An ultimate capacity to damage the system.
You should not log in as root without a very
good reason.
Normal (“unprivileged”) users’ potential to do a
damage is limited.
(C) Mustafa Golam, 2007 71
Linux File Security
Every file and directory has permissions set
that determine who can access it.
Permission are set for:
The owner of the file (called the “user”, arguably
a misnomer)
The group members
All others
Permissions that are set are called read,
write, and execute permissions.
(C) Mustafa Golam, 2007 72
Permission Types
Four symbols are used when displaying permissions:
r permission to read a file or list a
directory’s content
wpermission to write to a file or
create of remove files from a
directory.
x permission to execute a program or
change into a directory and do a long
listing of the directory.
- no permission (in place of r, w, or x)
A file may be removed by anyone who has write permission to
the directory in which the file resides regardless of the
ownership or permissions on the file itself.
The first character of the long listing is the file type.
(C) Mustafa Golam, 2007 73
Linux Process Security
When a process accesses a file the user and
the group of the process are compared with
the user and group of the file.
If the user matches, the user permission apply.
If the group matches, but the user doesn’t, the
group permission apply.
If neither match, the other permissions apply.
Every process runs as a under the authority
of a particular user and with the authority of
one or more groups, this is called the
process’s security context.
(C) Mustafa Golam, 2007 74
Changing Permissions-
Symbolic Method
To change access modes:
chmod [-R] mode file…
Where mode is:
u,g or o (a) for user, group and other
+ or – (=) for grand or deny
r,w or x (s,t) for read, write and execute
Examples:
ugo+r Grant read access to all.
o-wx Deny write and execute to others.
Multiple comma separated operations can be give in
a single command
(C) Mustafa Golam, 2007 75
Changing permissions-
Numeric Method
Uses a three-digit mode number.
First digit specifies owner’s permissions.
Second digit specifies group permissions.
Third digit represents other’s permissions.
Permissions are calculated by adding:
4(for read)
2(for write)
1(for execute)
Example:
chmod 640 myfile
(C) Mustafa Golam, 2007 76
Unit 7
vi and vim Editor Basics and Printing
(C) Mustafa Golam, 2007 77
Overview of vi and vim
vi: the “visual editor”, standard Linux
and Unix editor
vim: the “vi improved” editor, standard
Red Hat editor
On Redhat operating systems, the vi
command invokes vim.
Derived from earlier Unix editors
ed ex vi vim
(C) Mustafa Golam, 2007 78
Starting vi and vim
To start vim: vi filename
If the file exists, the file is opened and the
contents are displayed.
If the file doesn’t exit, vi creates it when the
edits are saved for the first time.
To use vi instead:
unalias vi or
\vi
(C) Mustafa Golam, 2007 79
Starting vi with different options
vi -m myfile file is non-modifiable
vi –R myfile file is only modifiable by using the
exclamation point :w!
vi –n myfile do not use a swap file for backup
(useful for floppies)
vi –r myfile recover data from a swap file after a
crash
vi –x myfile encrypt file when saving, decrypt when
editing
(C) Mustafa Golam, 2007 80
Three modes of vi and vim
Command Mode
Cursor movement
Change, delete, yank, put, search
Insert mode
Type in new text
Return to command mode with <ESC>
ex mode
Configuring, exiting, saving
Search and replace
(C) Mustafa Golam, 2007 81
Cursor Movement
h left
j down
k up
l right
w word ahead
b word back
( Sentence Back
) Sentence forward
{ Paragraph above
} Paragraph below
Arrow keys (, , etc) also work.
A word is defined as a series of letters of the alphabet and numbers
uninterrupted by white space or punctuation.
If the cursor is on a punctuation character, the word is terminated by
white space or a letter of the alphabet or number.
(C) Mustafa Golam, 2007 82
Entering Insert Mode
a append after the cursor.
i insert before the cursor.
o open a line below.
A append to end of line.
I insert at beginning of line.
O Open a line above.
Pattern such as these permeate the vi and
vim commands.
(C) Mustafa Golam, 2007 83
Leaving Insert Mode: <Esc>
<Esc> Takes you from insert mode
back to command mode
Hint: When in trouble, press <Esc>
and then press <Esc> again.
By doing this, you can guarantee that,
regardless of the mode you were in,
you will be now in command mode.
(C) Mustafa Golam, 2007 84
Change, Delete and Yank
Change Delete Yank(Copy)
Line cc dd yy
Letter cl dl yl
Word cw dw yw
Sentence ahead c) d) y)
Sentence behind c( d( y(
Paragraph ahead c{ d{ y{
Paragraph behind c} d} y}
A line is yanked into a buffer, presumably to be put (or
pasted) back into the document at another location.
(C) Mustafa Golam, 2007 85
Put (Paste)
Use p or P to put (paste) copied or deleted data
For line oriented data:
p puts the data below the current line
P puts the data above the current line
For character oriented data:
p puts the data after the cursor.
P puts the data before the cursor.
Jus as in vi and vim, we “yank” instead of “copy”, we
“put” instead of “paste”; take data from a buffer and
place it in the documents.
(C) Mustafa Golam, 2007 86
Undoing Changes
u undo most recent change
U undo all changes to the
current line since the cursor
landed on the line.
<Ctrl-r> redo last “undone”
change.
The ‘u’ will not undo a previous u; that is, it will not toggle a
change, but rather undo several pervious changes.
To undo all successive changes to the current line, use the U
command.
To redo a change undone by a ‘u’ command, use the ‘<Ctrl-r>’
command.
(C) Mustafa Golam, 2007 87
Searching for Text
/text search downwards for “text”
?text Search upwards for “text”
n continue search in the same
direction.
N Continue search in the opposite
direction.
(C) Mustafa Golam, 2007 88
Command-Mode Tricks
dtc delete from cursor to the letter c
(does not span lines)
5dd delete five lines( a number can
precede any of the two character
change, delete, yank or put
commands).
x delete a character
rc replace a character with c
r replace character-for-character until
<Esc>
(C) Mustafa Golam, 2007 89
Saving and Exiting: ex mode
save changes Abandon changes
Exit :wq :q or :q!
Do not exit :w :e!
Forcing Changes:
Save changes
Exit :wq!
Do not Exit :w!
Abandon changes, staying in the editor, through :e! and force a
write through :w!. Finally, force a write through :w1!. Finally,
force a write and quit with :wq!. The last command will forcibly
write the file, but only quit if the forcible write was successful.
(C) Mustafa Golam, 2007 90
Printing in Linux
Printers may be local or networked.
Print requests are sent to queues.
Queued jobs are sent to the printer on a first
come first server basis.
Jobs may be canceled before or during
printing.
It support CUPS IPP (Common Unix Printing
Subsystem), lpd( Linux printing daemon),
windows, Netware and JetDirect printers.
(C) Mustafa Golam, 2007 91
Printing commands
lpr Send a job to the queue to be
printed
Accepts ASCII, PostScript, PDF, others.
lpq View the contens of the queue.
lprm Remove a job from the queue.
System V printing commands such as lp,
lpstat and cancel are also supported.
A user may only remove his own print jobs
from the queue.
(C) Mustafa Golam, 2007 92
Printing Utilities
enscript Converts text to PostScript
a2ps Converts text to PostScript
ggv(GNOME GhostView) PostScript and PDF Viewer
xpdf PDF viewer.
ps2pdf PostScript to PDF converter.
pdf2ps PDF to PostScript converter.
mpage Prints multiple pages per sheet.
(C) Mustafa Golam, 2007 93
Section 2
Red Hat Enterprise Linux:
A Deeper Look
(C) Mustafa Golam, 2007 94
Unit 8
The Linux Filesystem In-Depth
(C) Mustafa Golam, 2007 95
Partitions and Filesystems
Disk drives are divided into partitions.
Partitions are formatted with filesystems, allowing user to store
data.
Default filesystem: ext3, the Third Extended Linux Filesystem.
Other common filesystems:
Ext2 and msdos (typically used for floppies)
Iso9660 (typically used for CDs)
A filesystem is a data structure written to the media that allows
users to store and access files.
Ext2 at 1993, ext3 has Journaling to improve filesystem data
integrity.
EA Extended Attributes
ACLs POSIX Access Control Lists
(C) Mustafa Golam, 2007 96
Inodes
An inode table contains a list of all files in an
ext2 or ext3 filesystem.
An inode (index node) is an entry in the table,
containing information about a file (the
metadata), including:
File type, permissions, link count, UID, GID
The file’s size and various time stamps
Pointers to the file’s data blocks on disk.
Other metadata about the file
An individual entry in the inode table is called
an inode.
(C) Mustafa Golam, 2007 97
Directories
The computer’s reference for a file is the inode
number.
The human way to reference a file is by the file name.
A directory is a mapping between the human name for
the file and the computer’s inode number.
When a filename is referenced by a command or
application, Linux references the directory in which the
file resides, determines the inode number associated
with the file name, looks up the inode information in
the inode table, and , if the user has permission,
returns the contends of the file.
The ls –i command displays the inode number.
(C) Mustafa Golam, 2007 98
cp and inodes
The cp command:
Allocates a free inode number, placing a new
entry in the inode table.
Creates a directory entry, referencing the files
human file name to the inode number.
Copies data into the new file.
When a file is copied to a new name in the
same directory, the directory and the inode
table get a new entry.
(C) Mustafa Golam, 2007 99
mv and inodes
If the destination of the mv command is on
the same filesystem as the source, the mv
command:
Creates a new directory entry with the new file
name
Deletes the old directory entry with the old file
name
Has no impact on the inode table (except for a
time stamp) or the location of data on the disk.
No data is moved!
(C) Mustafa Golam, 2007 100
rm and inodes
The rm command:
Decrements the link count, thus freeing the
inode number to be reused.
Places data block on the free list.
Removes the directory entry.
Data is not actually removed, but will be
overwritten the data blocks are used by
another file.
(C) Mustafa Golam, 2007 101
Symbolic (or Soft) Links
A symbolic link points to another file
ls –l displays the link name and the
referenced file.
File type: l for symbolic link
The content of a symbolic link is the name of the
file that it references.
Syntax:
ln -s filename [linkname]
Removing a symbolic link removes the actual
link itself, not the underlying file
(C) Mustafa Golam, 2007 102
About Symbolic Links
Symbolic link is a separate file from the
original.
The first character of along listing for a
symlink is the letter l.
Symlink has permission of 777.
Permission of symlink are irrelevant; the
permissions set on the file pointed to by the
symlink control access rights.
The size of the symlink is always the number
of characters in the path name.
(C) Mustafa Golam, 2007 103
Hard Links
One physical file on the system.
Each link reference the file’s inode.
File is present in the filesystem as long as at
least one link remains.
Cannot span drives or partitions.
Syntax:
ln filename [linkname]
A hard link is a path name that references an
inode: that is , all files are hard linked at least
once.
(C) Mustafa Golam, 2007 104
On Hard Links
The file names must be on the same
filesystem: because they share an inode
number and an inode table is unique to
a file system, both must be on the same
file system.
It is not possible to use the ln command
to create additional hard links to
directories.
(C) Mustafa Golam, 2007 105
The Seven Fundamental
Filetypes
- regular files
d directory
l symbolic link
b block special file
c character special file
p named pipe
s socket
(C) Mustafa Golam, 2007 106
Extra Filetypes
c character special file:
Files referencing hardware are not regular files; they are one of the two
types of special files. Character special files are used to communicate with
hardware one character at a time.
b block special file:
Used to communicate with hardware a block of data at a time: 512 bytes,
1024 bytes, 2048 bytes: whatever is appropriate for that type of hardware.
Run the following command to see a list of block and character special files:
ls –l /dev | less
p named pipe:
A file that passes data between processes. It stores no data itself, but
passes between one process writing data into the named pipe and another
process reading data from the named pipe. A named pipe can be created
using the mknod command:
mknod mypipe p
s socket:
A stylized mechanism for interprocess communication. It is extremely rare
for a user or even a system administrator to explicitly create a socket.
(C) Mustafa Golam, 2007 107
Miscellaneous tools
df
du
mount
umount
eject
mtools(mdir,mcopy)
(C) Mustafa Golam, 2007 108
Unit 9
Configuring the Bash Shell
(C) Mustafa Golam, 2007 109
Configuring the Bash Shell
The Shell is configured through a
variety of mechanisms:
Local Variables
Aliases and functions
The set and shopt commands
The shell can also configure other
commands or applications through
environment variables.
(C) Mustafa Golam, 2007 110
Variables
A variable is a label that has a value.
Used to configure the shell or other programs.
Variables are resident in memory.
Two types: local and environment
Local Variables are used only by the shell.
Environment variables are passed onto other commands.
Display variables and values using:
set to display all variables.
env to display environment variables.
The set, env and echo commands can be used to display all
variables, environment variables, and a single variable value,
respectively.
set | less
env | less
echo $HOME
(C) Mustafa Golam, 2007 111
Configure the Shell: Local Variables
Data in Shell scripts and environment settings stored
in variables.
Conventionally all upper-case.
Setting variable value:
$FAV_COLOR=blue
To retrieve variable value, use $ before the variable
name
$echo $FAV_COLOR
$blue
For a list of variables that configure the shell, see the
Shell Variables section of the bash man page.
(C) Mustafa Golam, 2007 112
Common Local Variables
HISTFILESIZE
Determines how many commands to be saved in the history file
on logout.
COLUMNS
Sets the width of the terminal( xterm, gonme-terminal or
kterm)
LINES
Sets the height of the terminal ( xterm, gonme-terminal or
kterm)
HISTFILE
Specifies the file in which history commands are stored on
logout
HISTSIZE
Specifies the number of history commands to keep while
operating interactively.
(C) Mustafa Golam, 2007 113
The PS1 Local Variable
PS1 sets the prompt
Uses escape sequences to insert variable information in the
prompt.
\d the date
\h short hostname (not the FQDN)
\tthe current time
\u user name (useful if you have multiple
accounts)
\w the current working directory.
\!The history number of the current command.
\$ show $ if you are non-privileged user and a # if
you are a privileged user, useful if you sometimes
become superuser.
For a complete list of these prompting escape sequences, see
the PROMPTING section of the bash man page.
(C) Mustafa Golam, 2007 114
Aliases
Aliases let you create shortcuts to commands
$ alias dir=‘ls –laF’
Use alias by itself to see all set aliases.
Use alias follow by an alias name to see alias value.
$ alias dir
$alias dir=‘ls –laF’
In this case, if you ever want to use the rm
command itself, instead of your alias, you can
precede the command with a blackslash.
\rm –r Junk
(C) Mustafa Golam, 2007 115
Other Shell Configuration
Methods
Less common, but powerful commands to configure elements of the shell.
set
shopt
set –b report termination of
background commands
immediately, rather than
waiting for next prompt.
set –u unset variables generate an
error.
set –o noclobber do not clobber with > and
>& operators.
set –o vi use vi syntax on bash command
line instead of default emacs
syntax.
For a complete list of set values, see the set command under the SHELL
BUILTIN COMMANDS section of the bash man page. Also in this section is a list
of items configurable through the shopt command.
(C) Mustafa Golam, 2007 116
Configuring Commands:
Environment Variables
Shell variables exit only in current shell
instance.
Environment variables passed to subshells.
Shell variables can be exported into
environment
EDITOR=/usr/bin/vim; export EDITOR
Export EDITOR=/usr/bin/pico
To “blank” the value of an environment
variable, use the ‘unset’ built-in command.
$unset EDITOR
(C) Mustafa Golam, 2007 117
Common Environment Variables
HOME Path to user’s home directory
LANG Identification of default language programs should
use; example: en_US.UTF-8 for U.S. English.
PWD User’s current working directory.
EDITOR Default editor programs should invoke for text
editing.
LESS Options to pass to the less command.
The less command has many options; to force a set of options to
always be used, set the LESS variable. For example: LESS=“-emqs”
SHELL path to loging shell
USER Username of user
DISPLAY X Display Name
VISUAL Name of visual editor
(C) Mustafa Golam, 2007 118
The TERM Environment
Variable
TERM Environment variable setting the terminal
type.
reset Command (not variable) used to reset a
terminal should the screen become corrupted.
If a terminal is improperly set, the computer will not
be able to properly display data.
For more complex adjustments to your terminal
settings, set the stty command.
The stty command can perform a number of
terminal settings to your system.
(C) Mustafa Golam, 2007 119
The PATH Environment
variable
PATH colon separated list of locations where commands
can be found.
When a command is executed and the path is not specified,
then the shell will look in these directories in the given order,
stopping on first match, to find the command.
which command showing location in the PATH of an
executable
$which xterm
$ /usr/bin/xterm
Executable’s location may be specified.
$/bin/ls /etc
./myls /etc
Use the built-in shell type command to ask the shell what is
using to fulfill the following commands.
(C) Mustafa Golam, 2007 120
How the Shell Expands the
Command line
A simplification:
Split the line into words
Function and alias expansion
Curly brace string expansion
Tilde expansion
Parameter and variable expansion
Split the line into words again
File glob expansion
File redirection
Run the command!
(C) Mustafa Golam, 2007 121
Shell Startup Scripts
Scripts of commands executed at login
Uses include:
Configure the shell by setting local
variables or running the set and shopt
commands.
Configure other programs through
environment variables
Establish aliases
Run program on Startup.
(C) Mustafa Golam, 2007 122
Login Shells
Login shells are first shells started (i.e. when
you log in)
Shells launched from a login shell typically are
not login shells.
Login shells and non-login shells run different
startup scripts.
A login shell is a shell that someone started
by login onto the system. A non-login shell is
a shell started up in some other way, perhaps
by a user or a program issuing the bash
command.
(C) Mustafa Golam, 2007 123
Startup scripts: Order of
Execution
Login Shells
/etc/profile
/etc/profile.d
~/.bash_profile
~/.bashrc
/etc/bashrc
Non-login Shells
~/.bashrc
/etc/bashrc
/etc/profile.d
Typical sorts of commands placed in startup scripts include:
Local variable settings, particularly PS1
Environment variable settings, such as PATH or LESS.
Aliases, or perhaps unalias to remove undesired aliases set globally in
earlier scripts.
A umask can be setted.
(C) Mustafa Golam, 2007 124
/etc/profile
System-wide startup script for login shells.
Parsed by all users with Bourne-style shells, including
bash and sh.
Usually sets default PATH variable, user limits, and
other variables and settings.
Bash only sources /etc/profile if the shell is a login
shell.
This script will set a series of variables including
PATH, USER, LOGNAME, MAIL, HOSTNAME,
HISTSIZE and INPUTRC.
It will also run scripts in the /etc/profile.d directory.
(C) Mustafa Golam, 2007 125
/etc/profile.d
Some application-specific startup scripts in
this directory.
Scripts called by a for-loop in /etc/profile
Scripts set up variables and run initialization
procedures.
Two copies of each script are listed in this
directory, a Bourne Shell style with a .sh
suffix, and a C shell style with a .csh suffix.
(C) Mustafa Golam, 2007 126
~/.bash_profile and ~/.bashrc
For user-specific settings
Common to place variable settings, aliases
Commands that place output to the screen, such
as the date command, should go in
.bash_profile, not .bashrc.
The ~/.bash_profile file is only called by login
shells.
Never put any command that may echo something
to the screen in the ~/.bashrc file; such command
belong in the ~/.bash_profile file only.
(C) Mustafa Golam, 2007 127
~/.bash_logout
Resides in home directory
Executed when exiting a login shell.
Useful for running programs
automatically at logout
Example uses:
Make backup of files
Delete temporary files
Display Date and time of logout.
(C) Mustafa Golam, 2007 128
Mounting USB Media
Detected by the kernel as SCSI devices.
/dev/sdaX
Automatically mounted in GNOME/KDE.
Icon created in Computer window
Mounted under /media/<Device ID>
Device ID is build into device by vendor.
Most vendors give USB disks a label. fstab-sync
reads that label and automatically puts an entry in
the /etc/fstab for it, mounting it in /media/<label>
Like other disks, you can use fdisk and mke2fs to
create partitions and filesystems on USB disks.
(C) Mustafa Golam, 2007 129
Mounting Floppy Disks
Must be manually mounted and unmounted
mount /media/floppy
umount /media/floppy
DOS floppies can be accessed with mtools.
mount and unmounts device transparently.
Uses DOS naming conventions.
mdir a:
mcopy /home/file.txt a:
For a complete list of the mtools commands, run
mtools and/or consult the mtools info page.
(C) Mustafa Golam, 2007 130
Formatting Floppy Disks
Two types of format needed to prepare a floppy:
A low level format (rarely needed)
$fdformat /dev/fd0H1440
A filesystem, one of :
$mkfs –t ext2 /dev/fd0
$mke2fs /dev/fd0
$mkfs –t vfat /dev/fd0
mformat a:
Only the superuser or a non-privileged user logged
into the system console can run these commands.
Others do not have permission to do this.
(C) Mustafa Golam, 2007 131
Why archive Files?
Archiving places many files into one
target file.
Easier to backup, store and transfer.
tar – standard Linux archiving
command.
Originally, tar was used to create
archieves on tap devices, hence it’s
name- which stand for tape achieve.
(C) Mustafa Golam, 2007 132
Creating an Archive
Syntax:
tar cvf archive_name files…
c creates a new archive.
v produces verbose messages.
F archive_name is the name of the
new file.
(C) Mustafa Golam, 2007 133
Inspecting Archives
Syntax:
tar tf archive_name.tar
tar tvf archive_name.tar
First form displays a list of all files in the
archive.
The v causes a long listing (like ‘ls –l’)
of each file in the archive.
(C) Mustafa Golam, 2007 134
Extracting an Archive
Syntax
tar xvf archive_name.tar
The archive will be extracted in the
current directory.
Change to the target directory first.
Files maintain their hierarchy relative to
the current directory.
(C) Mustafa Golam, 2007 135
Why Use file Compression?
Results in smaller file size
Text files can be compressed over 75%
Binary files usually don’t compress much, if any.
tar archives are often compressed.
When text files often have patterns that lead to
compression ratios of up to 75%, binary files rarely
compress well with 0-25% being, typical.
In fact, it is possible for a “compressed” binary file to
actually be larger than the original.
(C) Mustafa Golam, 2007 136
Compression Utilites
gzip, gunzip
Standard Linux compression utility
Up to 75% compression for text files.
bzip2, bunzip2
Newer Linux compression utility.
Generally achieves better compression than gzip.
The gunzip command can also uncompress files,
compressed with the traditional Unix compress
command, making compress essentially obsolete.
zip
Compatible with DOS/Windows PKzip/Winzip utilites and can
compress more than one file into a single file.
(C) Mustafa Golam, 2007 137
On Compression
The traditional compress command can reduces the
text file by more than half. The Standard gzip
command does a better job, reducing the file to less
than 1/3 of the original size. Finally, the newer bzip2
command reduces the file to /14 of the original file.
The –c option to the gzip command leaves the
original compressed file alone, but sends and
uncompressed copy of the file to standard output.
The –d option decompresses a file, making ‘gzip –d
file.gz’ equivalent to ‘gunzip file.gz’
(C) Mustafa Golam, 2007 138
Compressing Archives
Often tar archives are compressed
tar can compress/uncompress archives.
Compression switches- use during
creation and extraction
z for gzip compression
j for bzip2 compression.
(C) Mustafa Golam, 2007 139
tar to Unformatted Floppies
Floppies can be used like tape drives
Low level format requied
File system not needed
Use tar to write to the floppy.
/dev/fd0 is the destination
Floppy cannot be mounted
Example:
tar czvf /dev/fd0 mydir
(C) Mustafa Golam, 2007 140
Unit 11
Advanced Topics in Users, Groups, and
Permissions
(C) Mustafa Golam, 2007 141
User and Group ID Numbers
User names map to user ID numbers.
Group names map to group ID numbers.
Data Stored on the hard disk is stored
numerically.
The user name and group affiliation of the
file are not stored; rather, the user ID
number and the group ID number are
stored.
(C) Mustafa Golam, 2007 142
/etc/passwd, /etc/shadow,
and /etc/group files
Authentication information is stored in plain is stored in plain text files:
/etc/passwd
/etc/shadow
/etc/group
/etc/gshadow
The /etc/passwd file contains seven fields: user name, password
placeholder (for historical reasons), uid number, gid number of the
user’s primary group, GECOS filed (usually containing the user’s real
name), home directory, and shell to be run when the user logs in.
The /etc/group file contains four fields: group password placeholder,
gid number, and a comma separated list of group members.
The /etc/shadow file is referenced when someone logs in: the file
contains a mapping of a user name to a password.
For a complete list of fields, see the man page
$man 5 shadow
(C) Mustafa Golam, 2007 143
System Users and Groups
Server programs such as web or print servers
typically run as unprivileged users, not as
root.
Examples: daemon, mail , lp, nobody.
Running programs in this way limits the
amount of damage any single program can
do the system.
These accounts exist primarily so that server
programs can run as non-privileged users or
as particular groups.
(C) Mustafa Golam, 2007 144
Changing Your Identity
To change your password, run passwd
Insecure passwords are rejected.
To start a new shell as a different user:
su
su –
su username
su - username
(C) Mustafa Golam, 2007 145
User Information Commands
Find out who you are
whoami
Find out what groups you belong to
groups, id
Find out who is logged in
users, who, w
Login/reboot history
last
(C) Mustafa Golam, 2007 146
Default Permissions
Default permission for files is 666
Default permission for directories is 777
umask is subtracted from default to determine new
file/directory permissions.
Non-privileged users’ umask is 002
Files will have permission of 664.
Directories will have permission of 775.
Root’s umask is 022.
Executive privilege is always denied a newly-create file,
regardless of the umask in effect. Execute privilege always
has to be explicitly granted to a file. Execute permission is
given to a directory upon creation, unless explicitly denied
by the umask.
(C) Mustafa Golam, 2007 147
Special Permissions
Special permissions: a fourth permission set
(in addition to user/group/other)
Applicable in four cases:
suid for an executable (4)
sgid for an executable (2)
sgid for a directory (2)
sticky bit for all directory (1)
Set with chmod or Nautilus.
chmod 3775 groupdir
(C) Mustafa Golam, 2007 148
Special Permission for
Executiables
Special Permission for Executables:
suid: Command run with permission of the owner of the
command, not the executor of the command. Like passwd.
sgid: command runs with group affiliation of the group
of the command.
In a long listing, the suid permission is displayed as a
lower case “s” where the “x” would otherwise be
located for the user permission (an upper case “S”
would be present if the underlying executable
permission is not set.)
Commands running with the sgid permission run
with the group affiliation of the group of the
command.
(C) Mustafa Golam, 2007 149
Special Permission for Directories
Special permissions for directories:
Sticky bit: files in directories with the sticky
bit set can only be removed by the owner
and root, regardless of the write
permissions of the directory.
Sgid : files created in directories with the
sgid bit set have group affiliations of the
group of the directory.
(C) Mustafa Golam, 2007 150
On Sticky Bits
The sticky bit for a directory sets a special
restriction on deletion of files: with the sticky
bit set, only the owner of the file, and the
superuser, can delete files within the
directory.
The sgid permission for a directory means
that files created in the directory will inherit
its group affiliation from the directory, rather
than inheiriting it from the user.
Often both the sticky bit and the sgid
permission will be set on a group directory.
(C) Mustafa Golam, 2007 151
Section 3
RHEL Power Tools
(C) Mustafa Golam, 2007 152
Unit 13
Introduction to String Processing
(C) Mustafa Golam, 2007 153
head
Displays first few lines (default: 10
lines) of the text in a file.
$head /tmp/output.txt
Use –n or --lines displayed
$head –n 20 /tmp/output.txt
(C) Mustafa Golam, 2007 154
tail
Displays last few lines (default: 10 lines) of text in a
file
$tail /etc/passwd
Use –n or –-lines to change number of lines
displayed.
$tail –n 5 /etc/passwd
tail is often used by the system administrator to
read the most recent entries in the log files.
Use -f to follow the end of a text file as it changes.
$tail –f make.out
Used to “watch” log files.
(C) Mustafa Golam, 2007 155
wc
“word count” – also counts lines and
characters
$wc story.txt
Use –l for only line count
Use –w for only word count
Use –c for only character count
(C) Mustafa Golam, 2007 156
sort
Sorts text to stout – original file unchanged
$ sort [options] files…
Common options
-r Reverses sort to sort descending
-n Numeric sort
-f ignore (fold) case of characters in strings.
-u unique (remove duplicate lines in output).
-t‘ x’ – use x as field separator.
-k POS1 – sort from field POS1
-k POS1.POS2 – Sort using fields POS1 ending at POS2.
The argument to the –k can be two numbers separated by a
dot. In this case, the number before the dot is the field number
and the number after the dot is the character within that field
with which to begin the sort.
(C) Mustafa Golam, 2007 157
uniq
Removes successive, duplicate lines in a file.
Can use in conjunction with sort to remove all
duplicates ( or use sort –u)
Use –c to count number of occurrences of duplicate
data.
To print only unique line occurrence in a file
(“removing” all duplicate lines), input to uniq must
first be sorted.
Since uniq can be given fields or columns on which to
base it’s decisions, these are the fields or columns
upon which it’s input must be sorted.
(C) Mustafa Golam, 2007 158
On uniq
Use –u to output only the lines that are truly unique
– only occurring once in the input.
Use –d to output only print one copy of the lies that
are repeated in the input.
Use –c to produce a frequency listing. Each line will
be prepended with a number indicating how many
times it appears in the input.
Use –fn or -sn to avoid comparing the first n fields or
characters in each line respectively.
$cut –d: -f7 /etc/passwd | sort | uniq
(C) Mustafa Golam, 2007 159
cut
Display specific columns of file data
$cut –f4 results.data
-f specifies field or column
-d specifies field delimiter (default is
TAB)
$cut –f3 –d: /etc/passwd
-c cuts by characters
$cut –c2-5 /user/share/dict/words
(C) Mustafa Golam, 2007 160
Other String processing Tools
paste – paste files together
tr – character translator
paste combines files “horizontally”. It takes a line
from each file and “pastes” them together to
standard output, separated by a tab. Use –d option
to change the output of the output delimeter.
$paste –d: ids.txt data.txt > merged.txt
tr is used to translate characters; that is, given two
ranges of characters, any time a character in range 1
is found, it is translated into the equivalent character
in range 2. This command is commonly used in shell
scripts to ensure that data is in an expected case.
(C) Mustafa Golam, 2007 161
Version Comparison with diff
Compares two files for differences
$diff area.c /tmp/area.c
33c33
< x = y +2;
----
> x = y+4;
33c33 indicates line where files differ
< indicates line in first file.
> indicates line in second file.
(C) Mustafa Golam, 2007 162
Spell checking with aspell
Interactive spell-checker
Easy way to check spelling in a file.
$aspell check letter.txt
Can create personal dictionary
look – quick spell check.
$look must
$aspell –l will non-interactively list the
misspelled words in a file read from standard
input.
Visit http://aspell.sourceforge.net
(C) Mustafa Golam, 2007 163
Formatting Tools
expand – expand tabs into spaces
fmt - reformat text into paragraph.
fmt formats its input paragraphs of the line width you
specify with –wn. You can also request uniform spacing
(with two spaces after each sentence) with u. fmt interprets
blank line in its input as paragraph delimiters.
pr -reformat text for printing
By default, it outputs 66-line pages including 56 lines of text
and a header (which can be suppressed)
$ pr –f /usr/share/dict/words | lpr
(C) Mustafa Golam, 2007 164
Awk , tcl, perl
Advanced!
More advanced.
Simple
(C) Mustafa Golam, 2007 165
Unit 12
Advanced Uses of the vi and vim
Editors
(C) Mustafa Golam, 2007 166
File Repositioning
G go to last line in the file
1G go to first line in file (any
number can be given and curser
will jump to that line)
<Ctrl-f> go forward one full screen
<Ctrl-b>go back one full screen
<Ctrl-d>go down half a screen
<Ctrl-u>go up half a screen
This is useful when an error message tells you that
an error exists on the particular line of a file. You can
use the G command preceded by that number to
jump right to the offending line.
(C) Mustafa Golam, 2007 167
Screen Repositioning
H go to first line on screen(high)
M go to middle line on screen
(middle)
L go to last line on screen(low)
z<Enter> Make current line first line on
screen.
z- make current line last line on
screen.
(C) Mustafa Golam, 2007 168
Filtering
The output of a command can be placed in the file.
The data in the file can be used as input of a
command.
Examples:
!!date
Replace current line with the output of date command.
!}sort
The paragraph will then be replaced with the output of the sort
command.
!}fmt -66
Will replace the paragraph with a paragraph formatted to be
less than 66 characters wide.
(C) Mustafa Golam, 2007 169
ex mode: Search and Replace
sed style search and replace
Different default addressing rule.
No address current line only.
1,12 Change lines 1 through 12
1,$ or % for changes in the entire file.
.,.+10 from current line(“.”) to
current line plus 10 lines (“.+10”)
Example: :%s/Ohiho/Iowa/g
:%s’/dev/had’ /dev/sda’g
:%s/\/dev\/had/\/dev/\sda/g
(C) Mustafa Golam, 2007 170
Visual Mode
Allow selection of blocks of text
v character-oriented highlighting
Vline-orienting highlighting
<Ctrl-v> block orienting highlighting
Visual Keys can be used in conjunction with movement keys:
w, ), }, arrows, etc.
Highlighted text can be deleted, yanked, changed, filtered,
search/replaced, etc.
c change d delete y yank(copy)
gg format to ‘textwidth’ columns
> indent
< unindent
(C) Mustafa Golam, 2007 171
Advanced Reading and Saving
:r newfile
:r !date
:1,20w xfile
:.,$w yfile
:1,20w >>zfile
:n otherfile
:n! otherfile
:n#
(C) Mustafa Golam, 2007 172
Configuring vi and vim
Configuring on the fly
:set or :set all
Configure permanetly
~/.vimrc or ~/.exrc
A few common configuration items
:set showmatch or :se sm or :se nosm
:set autoindent or :se ai or :se noai
:set textwidth=65 (vim only)
:set wrapmargin=15 :set wrapmargin=0
:se wm=15 and :se wm=0
:set ignorecare or :se ic or :se noic
:set number or :se nu or :se nonu
(C) Mustafa Golam, 2007 173
Expanding your vocabulary
Learn more cursor movements
Expanding change, delete, yank, and put
vocabulary
Add the advanced material from the
appendix to your skill base.
Learn more configuration features.
Play with filters.
:help
(C) Mustafa Golam, 2007 174
Cursor Movements
0 start of current line
$ end of current line
^ first nonblank character of current line.
e end of next word
gg top of current file
n% go to a line n percent through the current file
n| go to column n of the current line
Read the material in :help. Learn to maneuver around
the online help. Place the cursor over one of the |
tags| and go to that with <Ctrl-]> keystroke,
returning to the previous screen with :n#. This is a
rich resource well worth being mined extensively.
(C) Mustafa Golam, 2007 175
Unit 14
String Processing with Regular
Expressions
(C) Mustafa Golam, 2007 176
Pattern Matching with Regular
Expressions
Regular expressions are a pattern matching
engine
Used by many tools, including: grep, sed,
less , vi, awk
Values:
Power over ease of use
Greed!
Two types:
Basic
Extended
(C) Mustafa Golam, 2007 177
Tools with Regular Expressions
grep : the general regular expression
processor, which analyzes the contents of files a line
at a time, returning line that match a pattern.
Sed : the stream editor, returns the contents of a file
(or stream of data), performing a specified search
and replace instruction.
less : which uses regular expression in search
commands.
vi : which uses regular expression for
searches (like less) or search and replace (like sed).
awk : a data oriented programming language.
(C) Mustafa Golam, 2007 178
More on Regular Expression
Regular expressions are greedy. That is, if a
regular expression can match a smaller string
or a bigger string, it will always match the
largest string possible.
Types:
wildcard characters that stand for some other
character
Modifiers, that modify the preceding character.
Anchors that anchor a character sequence to the
beginning or end of a line or the beginning or end
of a word.
(C) Mustafa Golam, 2007 179
Wildcard characters
Wildcard characters stands for another single
characters:
. Any single character
[abc] any single character in the set
[a-c] any single character in the range.
[^abc] any single character not in the set
[^a-c] any single character not in the
range.
(C) Mustafa Golam, 2007 180
Modifiers
Modifiers determine the number of previous character
* zero or more of the previous char
\+ one or more of the previous char
\? Zero or one of the previous character
\{i\} exactly ‘i’ of the previous character
\{i,\} i or more of the previous char
\{i,\j\} i to j of the previous character.
The \+ and \? Sequences also modify the previous
character as described. Be warned that this syntax
may not be portable across all Unix-like operating
systems.
(C) Mustafa Golam, 2007 181
Modifiers Example
a*
ab*c
[a-z][0-9]*[a-z]
r\{6\}
#\{3,5\}
[0-9]\{9,\}
(C) Mustafa Golam, 2007 182
Anchors
Anchors match the beginning or end of a line
or word.
^ line begins with
$ line ends with
\< word beings with
\> word ends with
Absent the preceding backslashes, the <and>
characters are ordinary characters with no
special meaning.
(C) Mustafa Golam, 2007 183
Example Anchors
^Hi
^Hello$
\<cat
can\>
\<cat\>
<cat>
(C) Mustafa Golam, 2007 184
regex combinations
Regular expression are most useful in combination
with each other.
.* zero or more of any character.
[a-z]* zero or more character
\<cat\> the word ‘cat’
ab..ef ab and ef separated by two chars.
.\{32\} 32 of any character
\* a literal asterisk
A special character in regular expression preceded by
a backslash, \, is said to be quoted and so had no
special meaning, but rather stands for itself only.
(C) Mustafa Golam, 2007 185
Quote your regex’s!
On the command line, quote regular expressions.
$grep ‘mail.*tar’ *.txt
File name generation characters must remain
unquoted.
Do not user quotes in regular expressions within
commands.
Command using regular expressions outside
command line do not require quotes. For example,
using regular expressions to search for text search
for text while in the less command do not require
quotes.
(C) Mustafa Golam, 2007 186
grep
Prints lines of files where a pattern is
matched
$grep gmustafa /etc/passwd
Also used as filter in pipelines.
$ls | grep .c
Uses regular expressions
$grep ‘[0-9][A-Z]\{3\}[0-9]\{3\}’ cars
(C) Mustafa Golam, 2007 187
Common options of grep
-v return lines that do not contain
pattern
-n precede returned lines with line numbers.
-c only return a count of line with the
matching pattern.
-l only return the names of the files that have
at least one line containing the pattern.
-r perform a recursive search of files,
starting with the named directory.
-i perform a case-sensitive search.
(C) Mustafa Golam, 2007 188
On grep
“ One afternoon I asked Ken Thompson if he
could lift the regular expression recognizer
out of the editor and make a one-pass
program to do it. He said yes. The next
morning I found a note on my mail
announcing a program named grep. It
worked like a charm. When asked what that
funny name meant. Ken said it was obvious.
It stood for the editor command that it
simulated. g/re/p (global regular expression
print).” – Doug McIlroy
(C) Mustafa Golam, 2007 189
Using cron
Must edit and install your cron table file
Cron table file cannot be edited directly
Edit the file and then install with crontab or
Edit the file through crontab
Syntax:
contab [-u user] file
crontab [-l|-r|-e]
Cron table files( crontabs ) are stored in
/var/spool/cron, which is not accessible by
non-privileged users.
(C) Mustafa Golam, 2007 190
Using sed
Quote search and replace instructions
Sed addresses
$ sed ‘s/dog/cat/g’ pets
$ sed ‘1,50s/dog/cat/g’ pets
$ sed ‘/digby/,/duncan/s/dog/cat/g’ pets
Multiple sed instructions
$ sed –e ‘s/dog/cat/’ –e ‘/s/hi/lo’ pets
$ sed –f myedits pets
(C) Mustafa Golam, 2007 191
less and slocate
Searches in less use regular expressions
/h[aeiou]t
Searches in slocate can use regular
expression.
$ slocate –r ‘tig.*png’
With the –r option, place the regular
expression in quotes and all files matching
this pattern will be returned.
(C) Mustafa Golam, 2007 192
Regular Expressions in vi and vim
Regular expressions operate in less-like search
operations.
Example:
/1RKZ[68][0-9]3
?\<[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}\>
This would search upward in the file for social security
numbers.
And in sed-like search and replace commands
Example:
:1,$s/\<[Cc]at\>\& and dog/g
(C) Mustafa Golam, 2007 193
Extended Regular Expressions
An extended of the regular expression
set
Tools that use extended regex’s
egrep
grep –E ( same as egrep)
awk
(C) Mustafa Golam, 2007 194
Extended regex Syntax
Most basic regular expression are
supported.
Basic regular expressions requiring a
preceding backslash no longer require
backslash
a{10,12} counter: 10,11, or 12 letter “a”s
Exception: word anchors (“\<“ and “\>”)
still require backslashes.
(C) Mustafa Golam, 2007 195
awk
Programming language for editing text
Searches a file for lines matching a
pattern or patterns.
Performs specified actions on matching
lines.
Search patterns are extended regular
expressions.
$ awk ‘/tcsh/ {print}’ /etc/passwd
(C) Mustafa Golam, 2007 196
Using awk
awk programs are data-driven.
awk rules contain a pattern and an action in curly braces
Pattern {action}
The action is taken on any line matching the pattern.
$awk ‘/[2-5]+/ {print}’ /etc/inittab
$awk ‘{print}’ /etc/fstab
$awk ‘{print $2,$1}’ /etc/fstab
$awk ‘{print $2” “$1} /etc/fstab
All extended regular expression should work with awk, except
for the curly brace counters. To use these, you must use the –
posix or –re-interval options to awk.
(C) Mustafa Golam, 2007 197
Unit 15
Finding and processing Files
(C) Mustafa Golam, 2007 198
slocate
Can be invoked as slocate or locate
Queries a pre-built database of paths to files on the system.
Database is updated nightly with cron.
Full path is searched, not just filename.
May only search directories where the uses has read and
execute permission.
slocate is the “Security Enhanced Version of Locate”.
If you look at the /usr/bin/locate command you will see that it is
really a symbolic link to the slocate command.
locate and slocate can use regular expressions when searching
this database to provide information to perform a “fuzzy”
search.
(C) Mustafa Golam, 2007 199
slocate examples
$slocate foo
Search for files with foo in the name or path.
$slocate -r ‘\.foo$’
Regex search for files ending in .foo
Useful options
i: case-insensitive
-n X: show only the first X matches.
slocate only accepts basic regular expressions not
extended regular expressions.
If you decide to use –n to limit the number of results
printed by slocate, slocate will only print the first –n
<num> results.
(C) Mustafa Golam, 2007 200
find
$ find [directory…] [criteria…]
Searches directory trees in real-time
Slower but more accurate than slocate
CWD is used if no starting directory given
All files are matched if no criteria given.
Can execute commands on found files
May only search directories where the user has read and
execute permission.
Find has a huge amount of options that can be provided to
describe exactly what kind of file should be found.
You can search bases on file name, file size, last modified time
stamp, inode number, and many, many more.
find allows you to perform arbitrary actions on arbitrary files.
(C) Mustafa Golam, 2007 201
Basic find examples
$ find –name show.png
Search for files named show.png
$find / -name “*.png”
$find -iname show.png
Case-insensitive search for files named show.png, Snow.png,
SNOW.PNG, etc.
$find –user gmustafa –group gmustafa
Search for files owned by the user gmustafa and the group
gmustafa
The –regex option in find does not work quite the way one
would expect. –regex applies the regular expression to the
name of the file, including the absolute path to the file.
(C) Mustafa Golam, 2007 202
find and Logical operators
Criteria and ANDed together by default.
Can be OR’d or negated with –o and –not
Parentheses can be used to determine logic order, but must be
escaped in bash.
$ find –user gmustafa –not –group root
$ find -user gmustafa –o –user root
$ find –not\(-user gmustafa –o –user root \)
$ find / -name “*.png” –user gmustafa –mtime+12
$ find / -name “*.png” –not –user abcd
Logical ANDs have a higher priority than a logical OR, and a
logical NOT has a higher priority than an AND or an OR.
To force precedence of an expression, you can enclose options
that should be grouped together in parentheses.
(C) Mustafa Golam, 2007 203
find and Permissions
Can match ownership by name or id (-user, -group, -uid, -gid)
$ find / -owner gmustafa –gid 500
Can match octal or symbolic permissions
$ find –perm 755 # if anyone can write
$ find –perm +2 #if anyone can write
$ find –perm -2 #if everyone can write
$ find –perm +o+w# if other can write
Use –not to test denied permissions.
A numeric permission preceded by – will match files that have at least one
bit (user, group or other) for that permission set.
Symbolic permission by a + will match any file where all criteria are met.
To match files where someone has been denied a permission, simply write
a search for files where the permission is granted and then negate it. In
other words, to find files where neither the user nor the group have read
access you could use something like: find –not –perm +ug+r
(C) Mustafa Golam, 2007 204
find and Numeric Criteria
Many find criteria take numeric values
$ find –size 10M
Files with a size of exactly 10 Megabytes.
$ find –size +10M
Files with a size over 10 Megabytes
$ find -size -10M
Files with a size less than 10 Megabytes
$ find / -atime 5
The date of the last time the file was read (-atime)
$ find / -mtime +5
The date of the last change to the file’s data (-mtime)
$ find / -ctime -5
The date of the last changes to the file’s metadata. (-ctime)
(C) Mustafa Golam, 2007 205
find execution examples
$find –name “*.conf” –exec cp {} {}.orig \;
Create backup copies of configuration files, adding a .orig
extension
$find /tmp –ctime +3 –user gmustafa –ok rm {} \;
Prompt to remove gmustafa’s tmp files over 3 days old.
$find ~ -perm +2 –exec chmod o-w {} \;
Fix world-writable files in your home directory.
If your –exec command does not include {}, find will
still execute the command once for each file that is
found.
(C) Mustafa Golam, 2007 206
Find and Access times
find can match by inode timestamps
-atime : when file was last read
-mtime : when file data last changed
-ctime : when file metadata last
changed
Value given is in days
$find –mtime -10
Files modified less than 10 days ago.
(C) Mustafa Golam, 2007 207
More of *time
While the values passed to –atime, -ctime and –mtime are
measured in days, there are also corresponding criteria that
perform searches in minutes: -amin, -cmin and –mmin.
You can match access times relative to the timestamps of other
files using -anewer, -cnewer and –newer, which test
mtimes.
$ find –newer recent_file.txt
Would list all files with mtimes more recent that that of recent_file.txt.
To match files older than recent_file.txt you would simply
negate the –mnewer criteria.
$ find –not –newer recent_file.txt
The metadata, including all three timestamps, for a file can be
manually examined using the stat command.
(C) Mustafa Golam, 2007 208
Executing commands with find
Commands can be executed on found files.
Command must be proceeded with -exec or –ok
-ok prompts before acting on each file.
Command must end with <space>\;
Can use {} as a filename placeholder
$ find –size +100M –ok gzip {} \;
-ok options, which caused find to ask for each file.
When a character is prepended with a backslash (\),
bash is instructed to treat it literally, so typing \; at
bash’s command prompt will send; to find after bash
has done it’s interpretations.
(C) Mustafa Golam, 2007 209
Unit 16
Investigating and Managing processes
(C) Mustafa Golam, 2007 210
What is a process?
A process is an executing program which has
many components and properties
Exec thread
PID
Priority
Memory context
Environment
File descriptors
Security credentials
(C) Mustafa Golam, 2007 211
How Processes are created?
Once process “forks” a child, pointing to the
same pages of memory, and marking the
area as read-only.
Then the child “execs” the new command,
causing a copy-on-write fault, thus copying to
a new area of memory.
A process can exec, without forking
The child maintains the process ID of the parent.
(C) Mustafa Golam, 2007 212
Process Ancestry
init is the first process started at boot
time –always has PID 1
Except init, every process has a parent.
Processes can be both a parent and a
child at the same time.
pstree shows the process ancestry for
all process running on the system.
(C) Mustafa Golam, 2007 213
Process States
A process can be on of the many states:
-R Runnable (on the run queue)
process in the run queue. It is waiting for it’s turn to run or it is executing.
-S Sleeping
Process is not executing, not it is ready to run. It is waiting for an event to occur
or a signal to arrive to wake it up.
-T stopped
Process is not executing because it has been stopped.
-D Uninterruptible sleep
Process is sleeping and can not be woken up until an even occurs. It can be
woken by a signal. Typically, the result of an I/O Operations.
-Z Defunct (Zombie) process
Just before a process dies, it sends a signal to it’s parent and waits for an
acknowledgement before terminating. Even if the parent process does not
immediately acknowledge the signal, all resources except for the process identity
number (PID) are released. Zombie process are cleared from the system during
the next system reboot and do not adversely affect system performance.
(C) Mustafa Golam, 2007 214
Viewing process
ps
Displays processes information
Syntax: ps [options]
Useful options:
a Processes by all users
x process from all terminal
u show process owner
w include command arguments
f show process ancestry.
The options described above are based on output conforming to the UNIX98
Standard.
-l long listing. Includes more information such as the process owner’s UID.
$ ps –alx | grep ‘lpd’
$ pgrep lpd
The above two command are identical.
(C) Mustafa Golam, 2007 215
Sending Signals to Processes
Syntax:
$ kill [-signal ] pid(s)
$ kill [-signal] %jobID
Sends the specific signal to a process
Defult signal is TERM
$kill –l lists all available signals
$killall
Read man 7 signal.
kill can send many signals, but processes only respond to the
signals they have been programmed to recognize.
Signals may be sent to processes interactively using top and
gnome-system-monitor.
(C) Mustafa Golam, 2007 216
Terminating Processes
Most desirable way to end a process is
to let it end normally
Commands finish, applications are exited.
Can attempt to interrupt with <Ctrl-c>
(INT) or send a TERM signal
If all else fails, send a KILL signal.
Using KILL signals on a route basis may
cause zombie process and lost data.
(C) Mustafa Golam, 2007 217
Interactive Process
Management Tools
Display real-time process information
Allow sorting, killing and renicing
Command-line: top
GUI: gnome-system-monitor
Read: man top and help of gnome-
system-monitor
(C) Mustafa Golam, 2007 218
Running a Process in the
Foreground
When a command is entered, the shell
will not process further input until the
process is complete and the shell
prompt is redisplayed. i.e. job control.
The typeahead buffer allows you to
type other command, but they will not
be processed until the pending process
completes, or “returns”.
(C) Mustafa Golam, 2007 219
Altering process scheduling
priority
At process invocation time
Syntax:
$ nice [-n adjustment] command
Processes are scheduled with a Defult priority of 0
Priority value can range from -20 (highest priority) to
19 (lowest).
$ nice myprog
$ nice –n 15 myprog
Non-privileged users may not set niceness value to
less than zero; that is , they may not request a
higher than normal priority for their processes. This
is a function reserved for the superuser.
(C) Mustafa Golam, 2007 220
Altering Process scheduling
Priority – (continued)
renice changes the priority of a running process.
$ renice # [ [ -p | g] PID] [[-u]user]
# is the priority value.
Once a priority value is raised, a non-privileged user can not
lower it.
$ renice 15 –p < PID of user’s offending, greedy process>
$ renice -15 –p <PID of some user’s critical process>
The –p options is not necessary.
If you wish to change the priority of an entire process group,
use the –g option.
Use –u option, to modify the priority of all the process of a
particular user.
$ renice 15 –u gmustafa
(C) Mustafa Golam, 2007 221
Running a Process in the
Background
Running a command in the background allows another
process to run concurrently on the same terminal.
Launch a program as a background process by appending
an ampersand (&) to the end of a command:
$firefox &
When a process is started in the background, a new bash
“sub-shell” is created.
The bash program is then replaced with the command
being executed (the fork then exec procedure).
Background processes can be managed like any other
process.
(C) Mustafa Golam, 2007 222
Suspending a Process
Foreground jobs can be suspended:
temporarily halted without being killed
Suspend a foreground process with <Ctrl-z>
Suspended jobs can be:
Resumed in the background (bg)
Resumed in the foreground (fg)
When the job resumes, it will continue
executing from the point at which it was
suspended, it will not have to start over from
the beginning.
(C) Mustafa Golam, 2007 223
Listing Background and
Suspended Jobs
jobs displays all process running in the
background or that suspended.
The number in brackets is a job
number, used to kill jobs or bring them
back to the foreground.
Job numbers are referenced with %.
(C) Mustafa Golam, 2007 224
Resuming Suspended Jobs
When a command is suspended or
backgrounded, it can be brought back
to the foreground with fg.
Suspended jobs can be resumed in the
background with bg.
Syntax:
$ fg [%job_number]
$ bg [%job_number]
(C) Mustafa Golam, 2007 225
Compound Commands
List of commands separated by semi-colons
List inside () to run inside a subshell
$ (cd /usr; du ) &
A subshell group will combine the commands
so they are treated them as one unit.
When a group of commands is placed inside
parentheses, a new subshell is spawned and
output can be redirected as if it were one
command.
$ (date; who | wc –l) >> logfile
(C) Mustafa Golam, 2007 226
Scheduling a Process to
Execute Later
Syntax:
$ at time
<commands>
$ atq [user]
$ atrm [user|atJobID]
Commands will be executed at the time indicated
Non-redirected output is mailed to the user.
$ at 8:00pm December 7
$ at 7 am Thursday
$ at now + 5 minutes
$ at midnight +23 minutes
(C) Mustafa Golam, 2007 227
Scheduling Periodic Processes
The cron mechanism allows processes to be invoked
periodically
User need not to be logged on
Cron jobs are listed in a crontab file
The cron mechanism is controlled by a daemon called
crond. This daemon wakes up every minute or so
and determines if any entry user’s cron tables need
to be executed. If the time passed for an entry to be
started, it is started. A cron job can be scheduled as
often as once a minute or as infrequently as once a
year.
(C) Mustafa Golam, 2007 228
Using cron
Must edit and install your cron table file
Cron table file cannot be edited directly
Edit the file and the install with crontab
Or
Edit the file through crontab
Syntax
$ crontab [-u user] file
$ crontab [-l | -r | -e]
(C) Mustafa Golam, 2007 229
Crontab File Format
Entry consists of five space delimited
field followed by a command line
One entry per line, no limit to the line
length.
Fields are minute, hour, day of month,
month, and day of week.
Comment line begin with #
(C) Mustafa Golam, 2007 230
Unit 17
Network Clients
(C) Mustafa Golam, 2007 231
Other GUI Web Browsers
Epiphany
Uses same rendering engine as Firefox
Fully Gnome-complaint, but fewer features.
Konqueror
KDE web browser/file manager
Supports tabs, popup-blocking, etc.
Uses khtml rendering engine.
(C) Mustafa Golam, 2007 232
Non GUI Web browsers
links
Provided by the elinks rpm
Full support from frames and ssl
Examples
$ links http://www.redhat.com
$ links –dump http://www.redhat.com
$ links –source http://www.redhat.com
(C) Mustafa Golam, 2007 233
wget
Retrieves files via HTTP and FTP
Non-interactive –useful in shell scripts.
Can follow links and traverse directly
trees on the remote server –useful for
mirroring web and FTP sites.
Read info wget and wget –help.
(C) Mustafa Golam, 2007 234
Remote access and file
transfer
ssh/scp
telnet and the ‘r’ services
rsync
FTP clients
Nautilus
(C) Mustafa Golam, 2007 235
ssh : Secure shell
Public-key encryption technology replacement
for various unsecured services.
Allows secure access to remote systems
$ ssh gmustafa@oss2
-or-
$ ssh -l gmustafa oss2
gmustafa@oss2’s password:
$ ssh oss2 ls /tmp
$ ssh oss1 df -h
(C) Mustafa Golam, 2007 236
scp : Secure copy
Secure replacement for rcp
Layered on top of ssh
$ scp source destination
Remote files can be specified using:
user@host:/path/to/file
-r optoin enables recursion
-p preserves time and permissions
-c compresses datastream
Also available is sftp, an interactive file-transfer program
similar to a simple ftp client. The remote host’s sshd needs
to have support for sftp in order for the sftp client to work.
scp requires that the destination be a directory if the
source is a directory or consists of more than one file.
(C) Mustafa Golam, 2007 237
telnet and the ‘r’ services
Insecure protocols mostly replaced by ssh
telnet : login names and password pass over the
network in clear text
“r” services (rsh, rlogin, rcp): generally insecure
authentication mechanism.
telnet client can be used to connect to services
running on arbitrary ports
Examples: testing your mail server
$ telnet localhost 25
These tools should never be used.
(C) Mustafa Golam, 2007 238
rsync
A drop-in replacement for rcp copying to or from remote systems.
Can use ssh for transport
$ rsync –e ssh mysong.mp2 172.27.12.13:/home/gmustafa
Faster than scp – copies difference in like files.
Useful options to rsync
-e Use an external, rsh-compatible program to connect
(usually ssh)
-a Recurse subdirectories, preserving permissions,
ownership, etc.
-r Recurse subdirectories without preserving permissions,
etc.
--partial continue partially downloaded files
--progress print a progress bar while transferring
-p Same as –partial –progress
Read man 1 rsync
(C) Mustafa Golam, 2007 239
lftp
Versatile command line FTP client
Anonymous or real-user sessions
$ lftp ftp.cdrom.com
$ lftp –u gmustafa ftp.myserver.com
Automated transfer with non-interactive
lftpget
Graphical tools gftp.
(C) Mustafa Golam, 2007 240
gFTP
Applications/Internet/gFTP
Graphical FTP Client
Allows Drag-and-Drop transfers
Anonymous or Authenticated access
Optional secure transfer via ssh (sftp).
(C) Mustafa Golam, 2007 241
smbclient
FTP-like client to access SMB/CIFS resources
Examples:
View shares on a server
$smbclient –L server1
Access a share
$smbclient –U student //server1/homes
CIFS is most commonly implemented as Microsoft
Windows Network Neighborhood.
Useful options include:
-w workgroup or domain
-U Username
-N Suppress password prompt (otherwise you will be
asked for a password).
(C) Mustafa Golam, 2007 242
Network Diagnostic Tools
ping
Detects if it is possible to communicate with another system. Many systems no longer
respond to pings.
tracroute
Displays the computers through which a packet must pass to reach another system.
mrt
The mtr command is a repetitive version of traceroute, giving continually updated
connection time statistics.
host
Performs hostname to IP address translation, as well as the reverse.
dig
Performs a service similar to host in greater datail.
netstat
Provides a service similar to host in greater detail.
gnome-nettool (GUI)
A graphical front end of tools listed above (as well as some others) in a single, simple
interface. gnome-nettool can be run from the command line or by selecting it’s icon
from the internet section of the Application Menu. Note that, this tool may no be
installed by default.
(C) Mustafa Golam, 2007 243
Unit 18
Bash Shell Scripting
(C) Mustafa Golam, 2007 244
Scripting Basics
Shell scripts are text files that contains a
series of commands or statements to be
executed.
Shell scripts are useful for:
Automating commonly used commands.
Performing system administration and
troubleshooting
Creating simple applications
Manipulations of text or files.
Application prototyping
(C) Mustafa Golam, 2007 245
Creating Shell Scripts
Step 1: Use a text editor such as vi to create a text
file containing commnds.
First line contains the magic “shbang” sequence : #!
#!/bin/bash
Comment your scripts
Comments start with a #
Create shell script which is self documenting.
If you enter this by pressing \ key followed by the
Enter key on the most keyboards. This will enable
you to enter one command that spans multiple lines.
(C) Mustafa Golam, 2007 246
Creating Shell Scripts cont.
Step 2: Make the script executable
$ chmod a+x myscript.sh
To execute the new script:
Place the script file in a directory in the
executable path –OR-
Specify the absolute path or relative path
to the script on the command line.
(C) Mustafa Golam, 2007 247
Generating Output
Use echo to generate simple output
$ echo ‘Welcome to Redhat Linux paradise!’
$ echo –n “please enter the file name: “
Use printf to generate formatted output.
$ printf “the result is %0.2f\n” $RESULT
Syntax similar to C printf() function
Does not automatically put a newline at the end of
the output.
(C) Mustafa Golam, 2007 248
Handling Input
Use read to assign an input value to a shell variable:
echo –n “Enter the filename: “
read FILENAME
read reads from standard input and assigns one
word to each variable.
Any leftover words are assigned to the last variable.
A word is defined as a character string surrounding
with white space such as spaces and tabs.
Can be changed by IFS. IFS=‘:’
If there are more words than variables, the last
variable is assigned all the remaining words.
(C) Mustafa Golam, 2007 249
bash scripts example
#!/bin/bash
echo –n ‘Enter name (First Last):’
read FIRST LAST
printf “Your First name is %s and your last name is %s \n” \ $FIRST
$LAST
The (-p) option is used to display a prompt string. Place quotes around
the string if you need to prompt the user with a multiple-word
command
#!/bin/bash
read –p “Enter Several Values:” value1 value2 value3
echo “value 1 is $value1”
echo “value 2 is $value2”
echo “value 3 is $value3”
(C) Mustafa Golam, 2007 250
Exit Status
Commands exit with an exit status
0 for success, 1 to 255 for failure
Exit status of most recently executed
command is kept in the $? Variable just like
return values form shell functions.
Shell scripts may set an exit status with
the exit command.
exit 1 # indicated an error
(C) Mustafa Golam, 2007 251
Control Structures
The three types in shell programming:
Sequential structures – the program flows one line
after another
Selection structures – code execution based on a
logical decision.
if, if/else, if/elif/else and conditional operators.
Repetition structures (loops) – code execution is
repeated based on a logical decision
for, while and until
(C) Mustafa Golam, 2007 252
Conditional Execution
Commands may be executed conditionally,
based on the exit stats of the previous
command.
&& logical AND
|| logical OR
Examples:
$ grep gmustafa passwd || echo ‘No gmustafa!’
$ cp –a /tmp/*.o . && echo ‘Done!’
This structures can be used in the command
line as well.
(C) Mustafa Golam, 2007 253
Selection Structures: Using
the if statement
if selection structures execute the body
of the structure only if the condition
tested is true.
if [condition]; then
do something
fi
(C) Mustafa Golam, 2007 254
File Tests
File tests:
-f tests to see if file exist and is a
regular file
-d tests to see if a file exists and is a
directory
-x tests to see if a file exits and is
executable
if [-f $HOME/lib/functions ]; then
source ~/lib/functions
fi
(C) Mustafa Golam, 2007 255
More on File Tests
-d FILE True if file is a directory
-e FILE True if file exists.
-f FILE True if file exits and is a regular file
-h FILE True if file is a symbolic link.
-L FILE True if file is a symbolic link.
-r FILE True if the file is readable by you.
-s FILE True if file exists and is not empty.
-w FILE True if the file is writable to you.
-x FILE True if the file is executable by you.
-O FILE True if the file is effectively owned by you.
-G FILE True if the file is effectively owned by your
group.
Type ‘help test’ at the shell to see a complete list.
(C) Mustafa Golam, 2007 256
String Tests
Strings may be tested as well
-z returns true if the string is empty
-n returns true if the string is not empty.
Operators such as =, !=, < and > may be used to
compare strings as well.
if [ $(id –u) =“0”]; then
echo “You are logged in as root”
fi
(C) Mustafa Golam, 2007 257
More on String Tests
-z STRING True if string is empty.
-n STRING True if string is not empty.
STRING1=STRING2 True if the strings are equal.
STRING1!=STRING2 True if the strings are not
equal.
STRING1<STRING2 True if STRING1 sorts before
STRING2 lexicographically.
STRING1>STRING2 True if STRING1 sorts after
STRING2 lexicographically.
-o OPTION True if the shell option
OPTIOIN is enabled.
! EXPR True if EXPR is false.
EXPR1 –a EXPR2 True if both EXPR1 AND EXPR2 is true.
EXPR1 –o EXPR2 True if either EXPR1 OR EXPR2 is true.
ARG1 OP ARG2 Arithmetic tests. OP is one of the –eq, -ne,
-lt, -le, -gt, or –ge.
(C) Mustafa Golam, 2007 258
Selection Structures: Using
if/else Statements
if/else selection structures execute the
body of the if structure only if the
condition tested is true, otherwise the
else is executed.
if [ condition] ; then
Do something
else
Do something else
fi
(C) Mustafa Golam, 2007 259
Selection Structures: Using
the case Statement
The case statement provides an alternative
method for performing selections that may be
cleaner than multiple if/elif/else tests.
case variable in
pattern1)
do something ;;
pattern2)
do another thing ;;
esac
(C) Mustafa Golam, 2007 260
Repetition Structures:
The for-loop
The for repetition structure provides a
method for iterating, or looping,
through a list of values and executing
commands on each of these values.
for variable in list-of-values
do
commands …
done
(C) Mustafa Golam, 2007 261
for and sequences
For loops are useful for iterating
through numeric sequences
Use bash notation for simple sequences
for I in {0..10}
Will use : 0,1,2,3,4,5,6,7,8,9,10
Use seq command for arbitrary increments
For I in $(seq 0 2 10)
Will count by twos: 0, 2, 4, 6, 8, 10
(C) Mustafa Golam, 2007 262
Selection Structures:
The while-loop
The while loop structure provides a useful method for
performing a set of commands while a condition remains true.
The syntax is:
while condition
do
commands
done
While loops are known as sentinel repetition structures.
An until loop works in exactly the same way, except that it
continues to execute as long as the command following the until
statement executes successfully; that is, it will stop the loop
when the command succeeds.
(C) Mustafa Golam, 2007 263
continue and break
While loops can be disrupted during
execution
continue stops the current execution of the loop
and reexamines the initial condition, possibly
restarting the loop.
break stops the processing the loop entirely,
jumping past the done statement.
exit exits from the shell scripts entirely
You may provide an exit status.
Without an explicit exit status, the exit command
will exit with at status of zero, indicating success.
(C) Mustafa Golam, 2007 264
Using Positional Parameters
Positional parameters are special variables that hold
the command-line arguments to the script.
The positional parameters are available are $1, $2,
$3, etc. These are normally assigned to more
meaningful variable names to improve clarity.
$* holds all command-line arguments.
The variable $0 is reserved and specifies the program
name as it was executed on the command line.
Variables above $9 require special handling and so
they must be enclosed in curly braces, e.g. ${11}.
All positional parameters are read only variables.
(C) Mustafa Golam, 2007 265
Handling Parameters with
Spaces
Bash expects space-delimited parameters
Causes problem when parameters have spaces
Example : script.sh “arg 1” “arg 2”
$* contains “arg” “1” “arg” “2”
Solution: for VAR; do …; done
Automatically assigns VAR with $1, $2, etc..
Handles spaces in parameters gracefully.
VAR would be set to “arg 1”, then “arg 2”
(C) Mustafa Golam, 2007 266
Handling Filenames with
Spaces
Bash mis-interprets spaces in filenames
Space treated as filename-delimiter by default
“my file” interpreted as two files: my and file
IFS environment variable can be used to
specify an alternate (or no) delimiter.
Always reset after changing
OLD_IFS=$IFS #Backup old delimiter
IFS=$(echo) #Set empty delimiter
<code> #Do Stuff
IFS=$OLD_IFS #Restore IFS
(C) Mustafa Golam, 2007 267
Using functions in shell scripts
Shell scripts may include shell functions.
Shell functions may improve program readability. They also help to
remove repetitious code from the scripts.
Shell functions must be declared before they are used.
If there is a section of shell code that is trying to accomplish the same
task and it is five or more lines long then it is good candidate for being
put in a separate function. This is known as functional
decomposition and will improve the program readability.
If there are a piece of shell code that you are likely to use again then it
is also good candidate for being put in a separate function. This is
called code reuse.
Shell functions must be declared in the shell script before they may be
used. This can be done by including the function before it gets called in
the script or by sourcing another shell script using the keyword
‘source’ or the (.) operator.
(C) Mustafa Golam, 2007 268
Using functions, continued
Arguments may be passed to a shell function by using their own
set of positional parameters ($1, $2 etc.)
myFunction $filename
The value of $filename will be available as $1 inside the body of
myFunction.
Functions may return the values by using the ‘return’ keyword
which sets the value of the special variable $?.
local make the variables unavailable outside the function. This
is called information hiding and is useful for keeping function
compartmentalized. In addition it will prevent us from
accidentally overwriting a global variables.
Functions may return vales using the return keyword which
sets the value of the special variable $?.
(C) Mustafa Golam, 2007 269
Scripting at the command line
Scripts can be typed at the bash prompt.
If/else/for/while statements cause extended
prompt
Can also separate lines with semicolon (;)
Define functions for code reuse
Store common functions in ~/.bashrc
Bash built-in “fc” for long commands
Default editor is vi or $EDITOR
(C) Mustafa Golam, 2007 270
Shell Script debugging
In order to debug a shell script invoke
the shell interpreter with debug options
or change the shebang to include the
debug options.
$bash –x script_name
$bash –v script_name
#!/bin/bash –x
#!/bin/bash -v
(C) Mustafa Golam, 2007 271
Appendix:
Advanced Features of the vi and vim
Editors
(C) Mustafa Golam, 2007 272
Advanced uses of vi and vim
Special features of vim
File and screen repositioning
Setting marks in text
Mapping complex commands to
keystrokes (:map)
Advanced read and save
(C) Mustafa Golam, 2007 273
Special Features of vim:
Multiple Buffers
vim -o fn1 fn2 start with multiple windows
<Ctrl-w>s split file horizontally
<Ctrl-w>v split file vertically
<Ctrl-w>n open new window, new file
<Ctrl-w>q quit out of a window
<Ctrl-w>[hjkl] move to different
window.
<Ctrl-w>[+-] resize window
:help windows show help for windows.
(C) Mustafa Golam, 2007 274
Special Features of vim:
Reformatting Text Width
gq reformats text width.
Usage:
Make sure textwidth is set:
:set textwidth=65
Issue the gq command followed by the
paragraph indicator:
gq}
1GgqG
(C) Mustafa Golam, 2007 275
Other Special Features of vim
Recording keystrokes and playback.
qc record keystrokes saving to buffer c
q stop recording keystrokes
@c play back keystrokes from buffer c
Other configuration options
:syntax <LANGUAGE | off>
:nohl
(C) Mustafa Golam, 2007 276
Setting a mark in text
Set a mark in text with a single quote
followed by a letter of the alphabet specifying
the mark name.
Example: delete some lines
Move the curson to the first line to delete
Set mark a: ‘a
Move the cursor to the last line to delete.
Set mark b: ‘b
Delete from marks a to b: :’a,bd
(C) Mustafa Golam, 2007 277
Creating Commands with
:map
You can create your own commands with the :map command
Place: map commands in ~/.vimrc or ~/.exrc file
Example: mapping <Ctrl-x> to : syntax off
: map <Ctrl-x> : syntax off
:map <Ctrl-x> :syntax off
:map <Ctrl-l> :w!<CR>:!aspell check %<CR>:e!%<CR>
The <Ctrl-l> keystroke may not be entered into the file. To
force it to be read, run <Ctrl-v> before the <Ctrl-l>: <Ctrl-
v><Ctrl-l>
The <Ctrl-v> forces the <Ctrl-l> to be taken literally.
The sequence <CR>, meaning “carriage return” should be
entered just as shown, with the four separate characters.
(C) Mustafa Golam, 2007 278
Miscellaneous Commands
‘‘ Returns to your previous
location
J Join with the next file.
~ Change character case
>> Indent
<< Unindent.
(C) Mustafa Golam, 2007 279
What next?
LUGs (bdlug)
http://www.redhat.com/training
http://www.redhat.com/training/developter/courses/
http://www.redhat.com/training/architect/courses
http://www.redhat.com/training/developter/courses/
http://www.idiom.com/free-compilers
http://www.redhat.com/apps/support/knowldedgeba
se
http://www.tldp.org
http://www.linuxquestions.org
(C) Mustafa Golam, 2007 280
Motto
It is worth working hard today to learn
a thing and so be lazy tomorrow!!
(C) Mustafa Golam, 2007 281