You are on page 1of 264

Redhat Enterprise Linux Essentials

Redhat Linux Essentials


Overview

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

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)

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.

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.ht
ml
6

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 its own T&C.
BSD code need to be abide the terms of
Berkeley Software Distribution. Visit
http://www.bsd.com

Linux Torvalds
Finish College Student in 1991
Created Linux Kernel

When Linux Kernel Combined with GNU


Applications, complete free UNIX-like OS
possible.

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.

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.

10

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.

11

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.

12

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 links 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.

13

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.

14

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.

15

Running Commands and Getting Help

16

--help
whatis -- with makewhatis
man
info
/usr/share/doc/html/en/
Software source code
Documentations

17

Commands have the following syntax:

Command [ options] [arguments]

Each item is separated by a space.


Options modify the commands 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.

18

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.

19

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
2
3
4
5
6
7
8

User Commands
System Calls
Library Calls
Special Files
File Formats
Games
Miscellaneous
Administrative Commands

man and man k commands

20

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.

21

While viewing an info page


Navigate with arrows, pgUp, pgDown
Tab
Enter
n/p/u

node
s[<text>]

Move to next link


Follow link the cursor is on
View next/previous/up-one

Search for text (default: last


search)
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.

22

/usr/share/doc
http://www.redhat.com/docs

23

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

24

Browsing the File system

25

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 users path is a list of directories that
are searched for commands typed at the
command line.

26

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
27

/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

28

Once a day the system automatically


deletes any files over seven days old in
/tmp and its subdirectories.
The /var directory contains regularlychanging 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).

29

Names may be up to 255 characters.


All characters are valid, except the /.

Names are case sensitive.

It may be unwise to use certain special


characters in file or directory names.
Some characters should be protected with quotes when
referencing them.
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.

30

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. Its a handy shortcut to use to
switch back and forth between two
directories.
31

cp copy files and directories


Usage:

More than one file may be copied at a time if


the destination is a directory.

cp [options] file destination

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.
32

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 its 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.

33

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.
34

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 its contents, use rm r.

35

Gnome graphical Filesystem browser


Can run in Spatial or Browser mode
Accessed via

Desktop icons
Home:
Computer:

Your home directory


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.

36

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.

37

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 its guess on a comparison of
the contents of the file and the patterns and
offsets in its reference file, /usr/share/magic.

38

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.
-bNumber each (non-blank) line of
output.
39

Syntax:

less [options] [filename]

Scroll with arrows/pgUp/pgDown


Useful commands while viewing:
/text
n
v

--search for text


--next match
-- open file in text editor.

less is the pager used by man

40

Less navigation commands:

<space>
<return>
<Ctrl-d>

g
/text
n
q
b
k
<Ctrl-u>
G
?text
N
v

move ahead one full screen


move ahead one line
move ahead (down) half a
screen
move to the top of the file.
search forward for text
repeat last search
quit
move back one full screen
move back one line
move back (up) half a screen
move to the bottom of the file
search backward for text
repeat last search, but in opposite direction.
open file in a text editor (vi by default)

41

-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).

42

The bash Shell

43

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

44

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

45

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.

46

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 users home directory.
(~gmustafa/.bashrc)

47

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.

48

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`

49

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\*

50

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.
51

Use the up and down arrow keys to scroll


through previous commands.
Type <CTRL-R> to search for a command in
command history.

To recall last argument from pervious command:

{reverse-i-search)`:
<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

52

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.

<Ctrl-a>

53

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

54

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

55

Standard I/O and Pipes

56

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.

57

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.
58

Redirecting
> command > file Directs standard output of command to file
>>
command > file Appends standard output of command to
file.
< command < file command receives its 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
command1 into the
command2.

Pipes the standard output of


standard input of

59

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.
60

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>>.

61

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

62

Redirecting Standard Input with <


Some commands only operate on
Standard Input
tr doesnt accept filenames as arguments- it
requires its 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.

63

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

64

Two of the basic tenets of UNIX philosophy are:

And

Make small programs that do one thing well.


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.

65

ls l | less
ls l | mail s Files abc@def.com
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

66

Users, Groups and Permissions

67

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 its owner or group can access.

68

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.

69

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.
70

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.

71

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.

72

Four symbols are used when displaying permissions:


permission to read a file or list a
directorys content
w
permission 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
of the directory.
no permission (in place of r, w, or x)

listing

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.

73

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 doesnt, 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 processs security context.
74

To change access modes:

Where mode is:

u,g or o (a)
+ or (=)
r,w or x

for user, group and other


for grand or deny
(s,t)
for read, write and execute

Examples:

chmod [-R] mode file

ugo+r
o-wx

Grant read access to all.


Deny write and execute to others.

Multiple comma separated operations can be


give in a single command

75

Uses a three-digit mode number.

Permissions are calculated by adding:

First digit specifies owners permissions.


Second digit specifies group permissions.
Third digit represents others permissions.
4
2
1

(for read)
(for write)
(for execute)

Example:

chmod 640 myfile

76

vi and vim Editor Basics and Printing

77

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

78

To start vim: vi filename


If the file exists, the file is opened and the
contents are displayed.
If the file doesnt exit, vi creates it when
the edits are saved for the first time.
To use vi instead:
unalias vi or
\vi

79

vi -m myfile
vi R myfile
the
vi n myfile
backup
vi r myfile
after a
vi x myfile
decrypt when

file is non-modifiable
file is only modifiable by using
exclamation point :w!
do not use a swap file for
(useful for floppies)
recover data from a swap file
crash
encrypt file when saving,
editing

80

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

81

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.

82

a
i
o

append after the cursor.


insert before the cursor.
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.

83

<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.

84

Yank(Copy)
Line
Letter
yl
Word
Sentence ahead
Sentence behind
Paragraph ahead
Paragraph behind

Change
cc

Delete
dd

cl
cw
c)
c(
c{
c}

yy
dl

dw
d)
d(
d{
d}

yw
y)
y(
y{
y}

A line is yanked into a buffer, presumably to be put (or


pasted) back into the document at another location.
85

Use p or P to put (paste) copied or deleted data


For line oriented data:

For character oriented data:

p puts the data below the current line


P puts the data above the current line
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.

86

u
U

undo most recent change


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.

87

/text
?text
n

search downwards for text


Search upwards for text
continue search in the same
direction.
Continue search in the opposite
direction.

88

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>
89

Exit
Do not exit

save changes
:wq
:w

Abandon changes
:q or :q!
: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.

90

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.
91

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.
92

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.

93

Red Hat Enterprise Linux:


A Deeper Look

94

The Linux Filesystem In-Depth

95

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

96

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 files size and various time stamps
Pointers to the files data blocks on disk.
Other metadata about the file

An individual entry in the inode table is


called an inode.
97

The computers 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 computers 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.
98

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.

99

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!

100

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.

101

A symbolic link points to another file


ls l displays the link name and the
referenced file.

Syntax:

File type: l for symbolic link


The content of a symbolic link is the name of
the file that it references.
ln -s filename [linkname]

Removing a symbolic link removes the


actual link itself, not the underlying file

102

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.

103

One physical file on the system.


Each link reference the files 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.

104

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.

105

d
l
b
c
p
s

regular files
directory
symbolic link
block special file
character special file
named pipe
socket

106

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

socket:

A stylized mechanism for interprocess communication. It is extremely


rare for a user or even a system administrator to explicitly create a
socket.

107

df
du
mount
umount
eject
mtools(mdir,mcopy)

108

Configuring the Bash Shell

109

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.

110

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
111

Data in Shell scripts and environment settings


stored in variables.
Conventionally all upper-case.
Setting variable value:

To retrieve variable value, use $ before the


variable name

$FAV_COLOR=blue

$echo $FAV_COLOR
$blue

For a list of variables that configure the shell,


see the Shell Variables section of the bash
man page.
112

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.

113

PS1 sets the prompt


Uses escape sequences to insert variable information in
the prompt.
the date
short hostname (not the FQDN)
the current time
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.

\d
\h
\t
\u

For a complete list of these prompting escape sequences,


see the PROMPTING section of the bash man page.

114

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

115

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.

116

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
117

HOME
LANG
should
English.
PWD
EDITOR

LESS

Path to users home directory


Identification of default language programs
use; example: en_US.UTF-8 for U.S.
Users current working directory.
Default editor programs should invoke for text
editing.
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
USER
DISPLAY
VISUAL

path to loging shell


Username of user
X Display Name
Name of visual editor

118

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.

119

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

Executables

$which xterm
$ /usr/bin/xterm

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.

120

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!

121

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.

122

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 nonlogin shell is a shell started up in some
other way, perhaps by a user or a
program issuing the bash command.

123

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.

124

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.
125

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.

126

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.
127

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.

128

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.
129

Must be manually mounted and unmounted

DOS floppies can be accessed with mtools.


mount and unmounts device transparently.
Uses DOS naming conventions.

mount /media/floppy
umount /media/floppy

mdir a:
mcopy /home/file.txt a:

For a complete list of the mtools commands, run


mtools and/or consult the mtools info page.

130

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.

131

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 its
name- which stand for tape achieve.

132

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.

133

Syntax:
tar tf archive_name.tar
tar tvfarchive_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.

134

Syntax

archive_name.tar

The archive will be extracted in the


current directory.

tar xvf

Change to the target directory first.

Files maintain their hierarchy relative to


the current directory.

135

Results in smaller file size


Text files can be compressed over 75%
Binary files usually dont 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.

136

gzip, gunzip

bzip2, bunzip2

Standard Linux compression utility


Up to 75% compression for text files.
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.

137

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

138

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.

139

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

140

Advanced Topics in Users, Groups, and


Permissions

141

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.
142

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 users primary group, GECOS filed (usually containing the
users 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

143

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 nonprivileged users or as particular groups.

144

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

145

Find out who you are

Find out what groups you belong to

groups, id

Find out who is logged in

whoami

users, who, w

Login/reboot history

last

146

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.
Roots 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.

147

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

148

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.

149

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.

150

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.
151

RHEL Power Tools

152

Introduction to String Processing

153

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

154

Displays last few lines (default: 10 lines) of text


in a file

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 /etc/passwd

$tail f make.out

Used to watch log files.


155

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

156

Sorts text to stout original file unchanged

Common options

$ sort [options] files


-r
-n
-f
strings.
-u
-t
x
-k POS1
-k POS1.POS2

Reverses sort to sort descending


Numeric sort
ignore (fold) case of characters in
unique (remove duplicate lines in output).
use x as field separator.
sort from field POS1
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.

157

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 its decisions, these are the fields
or columns upon which its input must be sorted.

158

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

159

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

160

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.
161

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.

162

Interactive spell-checker
Easy way to check spelling in a file.

Can create personal dictionary


look quick spell check.

$aspell check letter.txt

$look must

$aspell l will non-interactively list the


misspelled words in a file read from
standard input.
Visit http://aspell.sourceforge.net

163

expand expand tabs into spaces


fmt
- reformat text into paragraph.

pr

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.

-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

164

Advanced!
More advanced.
Simple

165

Advanced Uses of the vi and vim Editors

166

G
1G

go to last line in the file


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.

167

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.
zmake current line last line on
screen.
168

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.

169

sed style search and replace


Different default addressing rule.
No address
1,12
1,$ or %
.,.+10

current line only.


Change lines 1 through 12
for changes in the entire file.
from current line(.) to
current line plus 10 lines (.

+10)
Example: :%s/Ohiho/Iowa/g

:%s/dev/had /dev/sdag

:%s/\/dev\/had/\/dev/\sda/g

170

Allow selection of blocks of text

v
character-oriented highlighting
V
line-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

171

:r
newfile
:r
!date
:1,20w xfile
:.,$w yfile
:1,20w >>zfile
:n
otherfile
:n!
otherfile
:n#

172

Configuring on the fly

Configure permanetly

:set or :set all


~/.vimrc or ~/.exrc

A few common configuration items

:set
:set
:set
:set

showmatch or :se sm or :se nosm


autoindent or :se ai or :se noai
textwidth=65 (vim only)
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

173

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

174

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.
175

String Processing with Regular


Expressions

176

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

177

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.

178

Prints lines of files where a pattern is


matched

Also used as filter in pipelines.

$grep gmustafa /etc/passwd


$ls | grep .c

Uses regular expressions

$grep [0-9][A-Z]\{3\}[0-9]\{3\} cars

179

-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.
180

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.
181

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

182

Finding and processing Files

183

$ 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.

184

$ find name 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 named show.png

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.

185

Criteria and ANDed together by default.


Can be ORd or negated with o and not
Parentheses can be used to determine logic order, but
must be escaped in bash.

$
$
$
$
$

find
find
find
find
find

user gmustafa not group root


-user gmustafa o user root
not\(-user gmustafa o user root \)
/ -name *.png user gmustafa mtime+12
/ -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.

186

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
187

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 files data (-mtime)

$ find / -ctime -5
The date of the last changes to the files metadata. (-ctime)

188

$find name *.conf exec cp {} {}.orig \;

$find /tmp ctime +3 user gmustafa ok rm


{} \;

Prompt to remove gmustafas tmp files over 3 days old.

$find ~ -perm +2 exec chmod o-w {} \;

Create backup copies of configuration files, adding a


.orig extension

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.

189

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.

190

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.

191

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 bashs command prompt will send;
to find after bash has done its interpretations.

192

Investigating and Managing processes

193

A process is an executing program which


has many components and properties
Exec thread
PID
Priority
Memory context
Environment
File descriptors
Security credentials

194

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.

195

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.

196

A process can be on of the many states:

-R

-S

Runnable (on the run queue)


process in the run queue. It is waiting for its turn to run or it is executing.

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 its 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.

197

ps
Displays processes information
Syntax:
ps [options]
Useful options:

a
x
u
w
f

Processes by all users


process from all terminal
show process owner
include command arguments
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 owners
UID.
$ ps alx | grep lpd
$ pgrep lpd
The above two command are identical.

198

Syntax:

Sends the specific signal to a process


Defult signal is TERM

$ kill [-signal ] pid(s)


$ kill [-signal] %jobID

$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.

199

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.

200

Display real-time process information


Allow sorting, killing and renicing
Command-line: top
GUI: gnome-system-monitor
Read: man top and help of gnomesystem-monitor

201

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.

202

At process invocation time


Syntax:

Processes are scheduled with a Defult priority of


0
Priority value can range from -20 (highest
priority) to 19 (lowest).

$ nice [-n adjustment] command

$ 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.

203

renice changes the priority of a running process.

Once a priority value is raised, a non-privileged user can


not lower it.

$ renice # [ [ -p | g] PID] [[-u]user]


# is the priority value.
$ renice 15 p < PID of users offending, greedy process>
$ renice -15 p <PID of some users 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

204

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.

205

Foreground jobs can be suspended:


temporarily halted without being killed
Suspend a foreground process with <Ctrlz>
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.
206

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 %.

207

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]

208

List of commands separated by semicolons


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


209

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
at
at
at

8:00pm December 7
7 am Thursday
now + 5 minutes
midnight +23 minutes

210

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 users
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.

211

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]

212

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 #

213

Network Clients

214

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.

215

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

216

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.

217

ssh/scp
telnet and the r services
rsync
FTP clients
Nautilus

218

Public-key encryption technology


replacement for various unsecured
services.
Allows secure access to remote systems
$ ssh gmustafa@oss2
-or $ ssh -l gmustafa oss2
gmustafa@oss2s password:
$ ssh oss2 ls /tmp
$ ssh oss1 df -h

219

Secure replacement for rcp


Layered on top of ssh

$ scp source destination


Remote files can be specified using:
user@host:/path/to/file
-r
-p
-c

optoin enables recursion


preserves time and permissions
compresses datastream

Also available is sftp, an interactive file-transfer


program similar to a simple ftp client. The remote
hosts 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.
220

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.

221

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

222

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.

223

Applications/Internet/gFTP
Graphical FTP Client
Allows Drag-and-Drop transfers
Anonymous or Authenticated access
Optional secure transfer via ssh (sftp).

224

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).

225

ping

tracroute

Performs a service similar to host in greater datail.

netstat

Performs hostname to IP address translation, as well as the reverse.

dig

The mtr command is a repetitive version of traceroute, giving continually


updated connection time statistics.

host

Displays the computers through which a packet must pass to reach another
system.

mrt

Detects if it is possible to communicate with another system. Many systems no


longer respond to pings.

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 its icon from the internet section of the Application Menu. Note that,
this tool may no be installed by default.

226

Bash Shell Scripting

227

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

228

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.

229

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.

230

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.

231

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.

232

#!/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 multipleword 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

233

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

234

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

235

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.

236

if selection structures execute the body


of the structure only if the condition
tested is true.

if [condition]; then
do something

fi

237

File tests:
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

-f

source ~/lib/functions

fi

238

-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.

239

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

240

-z STRING
-n STRING
STRING1=STRING2
STRING1!=STRING2

STRING1<STRING2

STRING1>STRING2

-o OPTION

! EXPR
EXPR1 a EXPR2
EXPR1 o EXPR2
ARG1 OP ARG2
eq, -ne,

True if string is empty.


True if string is not empty.
True if the strings are equal.
True if the strings are not
equal.
True if STRING1 sorts before
STRING2 lexicographically.
True if STRING1 sorts after
STRING2 lexicographically.
True if the shell option
OPTIOIN is enabled.
True if EXPR is false.
True if both EXPR1 AND EXPR2 is true.
True if either EXPR1 OR EXPR2 is true.
Arithmetic tests. OP is one of the
-lt, -le, -gt, or ge.

241

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

242

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

243

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

244

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

245

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.

246

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.

247

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.

248

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

249

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
IFS=$(echo)
<code>
IFS=$OLD_IFS

#Backup old delimiter


#Set empty delimiter
#Do Stuff
#Restore IFS

250

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.

251

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 $?.

252

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

253

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

254

Advanced Features of the vi and vim


Editors

255

Special features of vim


File and screen repositioning
Setting marks in text
Mapping complex commands to
keystrokes (:map)
Advanced read and save

256

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.

257

gq reformats text width.


Usage:

Make sure textwidth is set:


:set textwidth=65

Issue the gq command followed by the


paragraph indicator:
gq}
1GgqG

258

Recording keystrokes and playback.


qc
q
@c

record keystrokes saving to buffer c


stop recording keystrokes
play back keystrokes from buffer c

Other configuration options


:syntax <LANGUAGE | off>
:nohl

259

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

260

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>: <Ctrlv><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.

261

Returns to your previous


location
J
Join with the next file.
~
Change character case
>>
Indent
<<
Unindent.

262

LUGs (bdlug)
http://www.redhat.com/training
http://www.redhat.com/training/developter/cours
es/
http://www.redhat.com/training/architect/courses
http://www.redhat.com/training/developter/cours
es/
http://www.idiom.com/free-compilers
http://www.redhat.com/apps/support/knowldedg
ebase
http://www.tldp.org
http://www.linuxquestions.org
263

It is worth working hard today to learn a


thing and so be lazy tomorrow!!

264

You might also like