Sie sind auf Seite 1von 154

Linux Foundations

n + 1, Inc
May 10, 2010

Copyright (c) 2010 n + 1, Inc. Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or any later version published
by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no BackCover Texts. A copy of the license is included in the section entitled GNU Free Documentation
License.

Page 2

c
2010
n + 1, Inc All Rights Reserved

Contents
1

Introduction to Linux

11

1.1

History of Unix and Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

1.1.1

Unix Design Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13

1.1.2

GNU and the GPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

14

1.1.3

The Linux Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

1.1.4

Linux Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16

Logging into Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

17

1.2.1

Multi-user Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

1.2.2

User Info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

19

1.2.3

System Info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

20

1.2.4

date and cal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21

1.3

Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

22

1.4

The root user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

1.4.1

su . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

1.4.2

passwd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

25

Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

26

1.2

1.5
2

Files and Directories

27

2.1

The Linux File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

28

2.1.1

29

File System Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


3

CONTENTS
2.2

2.3

2.4

2.5

2.6
3

Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

30

2.2.1

ls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

31

Creating Directories and files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

2.3.1

cp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

2.3.2

mv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

35

2.3.3

rm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

36

Viewing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

37

2.4.1

cat and less . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

38

2.4.2

head and tail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

39

2.4.3

wc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

40

Finding Files with locate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

41

2.5.1

Finding Files with find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42

Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

43

The vim editor

45

3.1

History of vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

46

3.2

Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

47

3.3

Saving and Exiting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

48

3.4

Movement commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

49

3.5

Insertion commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

50

3.6

Editing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

51

3.7

Search and Replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

52

3.8

Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

53

The BASH shell

55

4.1

Command Shell Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

56

4.1.1

Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

57

4.1.2

Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

58

Page 4

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS
4.1.3

Pipe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59

4.2

Filename Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

60

4.3

Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

61

4.4

Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

62

4.5

aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

63

4.6

Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

64

File Permissions

65

5.1

Owners and Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66

5.1.1

useradd and userdel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

67

5.1.2

chown and chgrp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

68

File Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

69

5.2.1

chmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

71

5.3

umask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

72

5.4

Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

73

5.2

Text Processing

75

6.1

Text Searching with Grep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

76

6.1.1

Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

77

6.1.2

Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

78

6.1.3

Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

79

6.1.4

Parenthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

80

6.2

sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

81

6.3

tr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

83

6.4

sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

84

6.5

uniq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

85

6.6

cut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

86

6.7

Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

87

Page 5

c
2010
n + 1, Inc All Rights Reserved

CONTENTS
7

File Management Utilities

89

7.1

Disk Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

90

7.2

Disk Usage within Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

91

7.3

Compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

92

7.3.1

Compressing files with gzip . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

93

7.3.2

Compressing files with bzip2 . . . . . . . . . . . . . . . . . . . . . . . . . . . .

94

7.3.3

Compressing files with zip . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

95

7.4

Archiving Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

96

7.5

Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

97

Processes and Jobs


8.1

8.2

Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
8.1.1

States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

8.1.2

Viewing Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

8.1.3

Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

8.1.4

Signal Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Job Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106


8.2.1

8.3
9

Managing Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

Package Management
9.1

99

109

RPM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
9.1.1

Binary Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

9.1.2

Source Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

9.1.3

Query and Verify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

9.2

Yum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

9.3

Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

10 Secure Shell
Page 6

117
c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS
10.1 Secure Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
10.1.1 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
10.1.2 Using SSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
10.1.3 File Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
10.2 SSH Key Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
10.3 Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
11 Mounting Filesystems

125

11.1 Filesystems under Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126


11.1.1 Mounting Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
11.1.2 Filesystem Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
11.2 NFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
11.3 Samba . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
11.4 Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
12 Shell Scripting

133

12.1 Shell Script Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134


12.1.1 Exit Status and Test Command . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
12.1.2 Variables and the read Command . . . . . . . . . . . . . . . . . . . . . . . . . 137
12.1.3 Special Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
12.2 if-then-ilif-else-fi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
12.3 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
12.4 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
12.5 The case Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
12.6 Lab Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Appendices
A

Page 7

147

GNU Free Documentation License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

c
2010
n + 1, Inc All Rights Reserved

CONTENTS

Page 8

c
2010
n + 1, Inc All Rights Reserved

List of Tables
2.1

File Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

28

2.2

Filesystem Hierarchy Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

29

2.3

Options for the ls command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

31

2.4

Options for the cp command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

2.5

Options for the mv command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

35

2.6

Options for the rm command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

36

2.7

Options for the wc command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

40

2.8

Search criteria for the find command . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42

3.1

Saving and Exiting Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

48

3.2

Movement Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

49

3.3

Paging Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

49

3.4

Insertion Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

50

3.5

Editing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

51

3.6

Search Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

52

4.1

Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

60

5.1

Fields in /etc/passwd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66

5.2

Fields in /etc/group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66

6.1

Options for the grep command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

76

LIST OF TABLES
6.2

Predefined character classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

78

6.3

Regular expression quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

79

6.4

Options for the sort command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

84

6.5

Options for the uniq command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

85

6.6

Options for the cut command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

86

7.1

Options for the df command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

90

7.2

Options for the du command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

91

7.3

Options for the gzip command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

93

7.4

Options for the bzip2 command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

94

7.5

Options for the zip command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

95

7.6

Options for the tar command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

96

8.1

Common Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

9.1

Options for the rpm command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

9.2

Options for the rpm query command . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

9.3

Options for the rpm verify command . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

9.4

Options for the yum command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

12.1 Options for the test command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

Page 10

c
2010
n + 1, Inc All Rights Reserved

Chapter 1

Introduction to Linux
Objectives
Understand the History of Unix and Linux
Understand the licensing and distribution of Linux
Be able to obtain basic system and user info
Obtain help for commands in multiple ways
Be able to become root and run basic account administration commands

11

CHAPTER 1. INTRODUCTION TO LINUX

1.1 History of Unix and Linux


In the late 1950s, ATT realized it needed to create an operating system to automate the processing of
batch jobs. The BESYS operating system was created at the research arm of ATT , Bell Labs, for this
purpose. ATT freely gave copies of BESYS (including source code) to any interested party, although
with no support. If bugs were found by any one party, fixes were mutually exchanged.
IN 1964, the BESYS operating system was showing its age and a replacement was sought. After
much wrangling, ATT joined forces with MIT and GE to create a new operating system, MULTICS.
The MULTICS project was intended to show that general-purpose, multi-user, timesharing systems
were practical and possible. Over the next few years, lots of cutting-edge research was done by the
academics at MIT and the top computer scientists at GE and ATT. The mind set of how computers
should operate changed dramatically, and all the parties came to the common view that computing
should be an interactive and multi-user experience.
With mounting delays, ATT pulled out of the MULTICS project in 1969. The MULTICS project had
been too ambitious, and each party had differing visions that were hard to reconcile. Bell Labs
researchers Ken Thompson and Dennis Ritchie started working on an alternative to MULTICS. The
operating system was named UNIX, to distinguish it from the complexity of MULTICS.
Starting with the sixth edition of UNIX, ATT licensed the source code at low cost. However, they
kept the UNIX name trademarked and closely held. Each licensee had to come up with their own
name.
There are two main branches of the UNIX family tree, the System V branch and the bSD branch.
The System V branch was maintained by ATT and the BSD branch by the University of California
at Berkeley.

Page 12

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

1.1.1 Unix Design Concepts


As work continued on the Bell Labs operating system, the researchers developed a set of principles
to guide their work. Among these principles were:
Make each program do one thing well.
Expect the output of each program to become the input to another program.
Design and build software to be tried early.
Use tools to lighten a programming task.
Everything is a file.
Configuration files are text.
Small programs and can be used together via pipes, which allows for simple, fast solutions to
otherwise complex tasks.
The use of ASCII configuration files facilitates remote administration, such as over ssh. In addition,
it allows editing via simple text editors as opposed to single purpose tools built into the operating
system.

Page 13

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 1. INTRODUCTION TO LINUX

1.1.2 GNU and the GPL


The GNU project was started in the 1983 by Richard Stallman with the goal of creating a free
software operating system: GNU. In addition to defining the guiding philosophy for the GNU
project, Richard Stallman also personally contributed code and is the principle author of several
important software components, such as the GCC compiler and the EMACS text editor.
The GNU project created a large set of tools and started creating a kernel called HURD in 1990.
However, the HURD micro-kernel architecture never worked out and the GNU project was missing
a strong kernel to create a complete system.
The GNU tools are released under the GPL, the General Public License. This license guarantees
that free software remains free. Under this license, a user of software has the following:
The freedom to run the program for any purpose
The freedom to study the program and adapt the source code to their needs
The freedom to redistribute copies of the program
The freedom to improve the program and release your improvements to the public

Page 14

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

1.1.3 The Linux Kernel


In 1991, Linus Torvalds released his first version of the Linux kernel. It was licensed under the GPL
and filled the need the GNU project had for a kernel. Combined with the GNU tools, it allowed for
a complete operating system containing only open source code.
The original Linux kernel only supported AT hardware and Linus suggested that it would probably
never support anything else. Today, Linux supports a wide spectrum of hardware, from small
embedded applications to large mainframes.
The linux kernel is currently maintained by a large group of developers headed by Linux Torvalds.
Linus has final say over what changes are introduced into the offical kernel. Torvalds is sponsored
by the Linux Foundation, a merger of OSDL and the Free Standards Group.
The Linux kernel version is designated by a major number, minor number and patch level. For
example, the 2.6.18 kernel has a major number of 2, a minor number of 6 and a patch level of 18.
Until recently, the even minor number kernels were stable release and and odd minor numbers
signified a developmental kernel. However, the 2.6 kernel was released in 2003 and there are
currently no plans for a 2.7 kernel branch.

Page 15

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 1. INTRODUCTION TO LINUX

1.1.4 Linux Distributions


A Linux distribution is a collection of applications in addition to the Linux kernel. It includes an
installation program, a way to selectively install applications, and possibly, documentation and
support.
Most Linux companies producing distributions have teams of developers working full time to
produce free software. The companies earn money from services related to their distribution
including support, consulting and selling boxed copies.
Most distributions are available for download off the internet. Most have an CD or DVD ISO image
that can be downloaded and burned to media. Generally a boxed set of media can also be ordered.
The two most popular enterprise-ready distributions are Red Hat Enterprise Linux and SUSE Linux
Enterprise Server. Both Red Hat and SUSE have free versions that are more cutting edge, community
oriented versions. For Red Hat it is Fedora Core and for SUSE it is called openSUSE.
In addition, some other popular distributions are Slackware, Debian, and Ubuntu. Ubuntu has
gained great popularity, it uses Debians package management system and has made a number of
changes to make the system very user friendly for desktop/laptop usage.

Page 16

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

1.2 Logging into Linux


On Linux, the operating system identifies each user as they login to the system. The most common
method of identifying the user is through a combination of username and password.
Linux supports logins directly on the box either in a terminal or through a graphical login. Network
logins are also supported, thru a number of different technologies, althouth the current preferred
method is via ssh (secure shell).

Page 17

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 1. INTRODUCTION TO LINUX

1.2.1 Multi-user Concept


Linux systems are both multi-user and multitasking. Linux and Unix have always been multi-user.
A multi-user system allows multiple users to be logged in the system at the same time. On the other
hand, many other popular operating systems are multiple user. They have multiple accounts, but
only one user can be logged in at a time. A multi-user system allows simultaneous execution of
multiple processes, each with its own distinct user context.
A multitasking system has the ability to run mulitple processes simultaneously. To handle multitasking, a system has to handle time sharing of processes. On multiprocessor machines, this may
involve true simultaneous executions of processes.

Page 18

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

1.2.2 User Info


Although users are identified by their username, the system uses numbers to identify the users of
the system. Each user account has a number associated with it called the UID. This UID is used to
track processes and files owned by the user.
When a user logs in to the system, their is a terminal associated with the session. The terminal is
represented by a device file. The user never communicates directly with hardware under Linux, it
is always done via the kernel. Thus, a device file can be written to or read from as if it was a file,
but the kernel knows how to properly interpret that to the device. Within a terminal, a user has a
command prompt that allows them to type commands that the shell then runs.
There are a number of commands that will show user information. The id command will show the
UID of a user along with their primary group ID and all groups the user is a member of.
$ id
uid=500(robc) gid=500(robc) groups=500(robc)
The whoami command returns the username of the current user. While this seems silly, if a person
has multiple accounts or has a root account in addition to their regular user account, it can get
confusing. It is important to make sure the correct user is running some commands.
$ whoami
robc
The tty command returns the terminal that is associated with the current session.
$ tty
/dev/pts/2

Page 19

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 1. INTRODUCTION TO LINUX

1.2.3 System Info


Sometimes it is useful to get information about the system you are on. The uname command list
information about the host you are connected to. With the all option, the command uname -a returns
all the information uname provides for a system. This includes the hostname, information about
the operating system, including version, and some basic hardware information.
$ uname -a
Linux localhost 2.6.24-24-generic #1 SMP Tue Aug 18 16:22:17 UTC 2009 x86_64 GNU/Linux
The hostname command can be used to get information about the hostname. Other networking
information is available with the command ifconfig.
$ hostname
localhost.localdomain
$ /sbin/ifconfig
eth0
Link encap:Ethernet HWaddr 00:12:3F:9F:41:4C
inet addr:192.168.1.101 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::212:3fff:fe9f:414c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12429458 errors:0 dropped:0 overruns:0 frame:0
TX packets:8733236 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2685495366 (2.5 GiB) TX bytes:1132852348 (1.0 GiB)
lo

Link encap:Local Loopback


inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:1756897 errors:0 dropped:0 overruns:0 frame:0
TX packets:1756897 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:423443191 (403.8 MiB) TX bytes:423443191 (403.8 MiB)

It is also useful to know information about other users on the system. The primary command for
determing who is logged in is called, amazingly enough, who. This command will show all the
users logged in, their terminals, and the time of login.
$ who
robc
robc
robc

Page 20

:0
pts/1
pts/2

2009-08-10 08:41
2009-10-09 08:53 (:0.0)
2009-10-09 09:06 (:0.0)

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

1.2.4 date and cal


There are a couple of basic time related commands that are useful. The cal command shows a
calendar. By defaul, it shows the current month of the current year. However, it can show any
entire year but passing the year in as an argument. A specific month from a specific year can be
displayed by passing both into cal.
$ date
Sun May

3 18:06:09 EDT 2009

$ cal
May 2009
Su Mo Tu We Th Fr
1
3 4 5 6 7 8
10 11 12 13 14 15
17 18 19 20 21 22
24 25 26 27 28 29
31

Sa
2
9
16
23
30

$ cal 8 2009
August 2009
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

Page 21

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 1. INTRODUCTION TO LINUX

1.3 Getting Help


There are three primary methods for getting help about commands in Linux. The man command
will show all the options and usage of the command. The information in a man page can be
overwhelming at first, but with continued usage, man pages become a major resource for the user.
The info command was created by the GNU project as a replacement for man. It uses an HTML type
navigation with links. When an info page exists, it is often better than the equivalent man page.
However, the existence of info pages is not as complete as with man.
The final method for getting help about a command is from the command itself. For most commands
using the - -help option after the command will return information about the command and its usage.
This information is not as complete as from the man or info page, but when you are needing syntax
or basic option help, it can be very useful.

Page 22

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

1.4 The root user


Most operating systems have the concept of a superuser with abilities and permissions beyond that
of the regular user. On a Linux system, the superuser is called root. Some of the things a root user
can do include:
Change to any directory and read and write files regardless of permission
Install software and make it available across the system
Modify device settings
Modify network configurations
Change the time on the system
Add/modify/remove users from the system
Change to any user account
The root user has a UID of 0.

Page 23

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 1. INTRODUCTION TO LINUX

1.4.1 su
The su command allows a user to switch to another user account. When the command is run, the
UID and GID values of the new user are substituted for the current user values.
To change to the guest user, a user would use the following command:
$ su guest
The user will be prompted for guests password. When a user first logs in, a number of initialization
scripts are run that set up the environment for the user. When the su command is used, this
environment does not chanege. However, you often want the environment of the new account. To
get the scripts run for that user, the dash option is added to the command.
$ su - guest
To run just a single command as another user, the -c option is used. This will login as the new user,
run the command, and then exit back to the original user.
A common use of the su command is for a user with root access, to change from their regular
account to the root account. When switching to root, no user name is necessary on the command
line.
$ su For security reasons, it is best for a user, even with superuser access, to use their regular account
for most tasks. Changing to the root user only for specific tasks is the best procedure.

Page 24

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

1.4.2 passwd
The passwd command is used to change a users password. If a regular user uses the command, it
changes their password. It first prompts for the current password. To make sure it is actually the
user changing the password, they must remember their current password.
If the root user runs the passwd command with a user as a parameter, root is prompted for a new
password without needing to know the users password. In both cases, the passwd command
verifies that the password is of sufficient strength. However, root can override this feature and use
a poor password anyway, although this is not recommended.

Page 25

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 1. INTRODUCTION TO LINUX

1.5 Lab Activity


Part 1. Log in to your account. Start a terminal window. At the command prompt run the following
commands:
$ whoami
$ id
$ tty
Part 2. Press Ctrl-Alt-F1 to bring up a terminal. Log in again. Then run the following commands:
$ tty
$ who
Press Ctrl-Alt-F7 to return to graphical interface.
Part 3. What day of the week was July 4th on in 1776? This year? Use the man command to find
the option to the cal command that shows Monday as the first day of the week. Run the command
with that option.
Part 4. Run the following commands:
$ ifconfig
$ su root
$ ifconfig
$ exit
$ su $ ifconfig
Why does the ifconfig command only work the 3rd time?

Page 26

c
2010
n + 1, Inc All Rights Reserved

Chapter 2

Files and Directories


Objectives
Understand the Filesystem Hierarchy
Be able to navigate, create, and delete directories in the file system
Be able to view, copy, and move files
Understand how to find files in the file system

27

CHAPTER 2. FILES AND DIRECTORIES

2.1 The Linux File System


Linux supports a wide variety of file systems. It supports file systems from many operating systems
and historical file systems. Some file systems that are supported include Minix, MS-DOS, NTFS,
NFS, HPFS, SYSV, SMB, and many, many more. The primary file systems used in Linux include
ext2, ext3, JFS, XFS, and ReiserFS. The final four from the previous list are journaling file systems.
The file system ext2 was, for a long time, the Linux standard. Most distributions have switched to
using ext3 because of its journaling ability.
The table below shows a description of some of the common file systems.
File System

Description

ext3

Journaling version of ext2

XFS

Journaling filesystem from SGI

JFS

Journaling filesystem from IBM

ReiserFS

Journaling filesystem excellent at handling small files

SMB

Allows Linux to mount Windows network shares

NTFS

Windows filesystem
Table 2.1: File Systems

Page 28

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

2.1.1 File System Characteristics


The Linux file system has a number of characteristics that are different from other operating systems.
First, Linux is singly rooted. All file systems are fitted into a single tree descending from a single
root, the / directory. Linux file names are case sensitive and long file names are allowed. Finally,
there are three time stamps associated with every file on the system: a creation time, an access time,
and a modification time.
Most Linux distributions follow the Filesystem Hierarchy Standard (FHS). In the Unix world,
different operating systems often put files in different places on the file system. With the FHS,
Linux has tried to standardize these locations. The FHS provides guiding principles for each area of
the filesystem. It also provided predictable locations for files and directories. The following shows
the directories that are defined under the FHS and a description of each one.
Directory

Description

The root directory

/bin

Essential command binaries

/boot

Boot loader and kernel

/dev

Device files

/etc

System configuration

/home

User home directories

/lib

Shared libraries

/media

Mount point for removable media

/mnt

Mount point for temporary file systems

/opt

Add-on software

/root

Root user home directory

/sbin

Essential system binaries

/tmp

Temporary files

/usr

Non-essential read only data

/var

Variable data files


Table 2.2: Filesystem Hierarchy Standard

Page 29

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 2. FILES AND DIRECTORIES

2.2 Navigation
The pwd command is used to show the current directory of the user. It will show an absolute path
name starting with the root directory (/). For example, after logging on, a user is initially located in
their home directory.
$ pwd
/home/user
The cd command is used to change directories. To change to any directory, the user can type the
cd command with any full path name, starting with the root directory. This is called an absolute
path name. An absolute path name always begins with /. The cd command can also be used with
relative path names, which are directory instructions relative to the current directory. If there is a
subdirectory under /home/user called docs, a user in /home/user can change directory to docs in
two ways.
$ cd /home/user/docs
$ cd docs
Both of the previous commands would change the user to the docs directory. In any directory,
there are two special files. The first is the . directory. This is a reference to the current directory.
The second is the .. directory. This is a reference to the parent directory. To change to the parent
directory, the user can type cd ... Multiple .. directories can be strung together to change up many
levels in the file system. To change from /home/user/docs to /etc, the following command can be
used.
$ cd ../../../etc
The first three ..s move up 3 levels in the file sytem, the the etc goes down into the etc directory.
There are some other special characters that can be used with cd. The cd command typed by itself
always takes the user to their home directory. The command cd will also change directories to
the users home directory. The command cd username will change to that users home directory.
Finally, the command cd - will return to the previous directory. It works like the return button on a
remote control, allowing the user to bounce back and forth between two directories.

Page 30

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

2.2.1 ls
The ls command lists the contents of a directory. By default, it lists the current directory but can
also be used to list other directories. There are a number of important options, shown in the table
below:
Option

Description

-a

Show hidden files

-l

Long listings

-d

Show directories not contents

-r

Reverse sort

-t

Sort by modification time

-S

Sort by file size

-R

Recursively list directories


Table 2.3: Options for the ls command

The ls command shows a simple list of files with no detail about the files. With the long listing
option, ls -l shows a list of files with details about the file. The command ls -a shows hidden files.
In Linux, a hidden file is any file that begins with a dot (.). There is nothing special about hidden
files other than the name. The general purpose of hidden files are for configuration files stored in
user home directories. These files are necessary but clutter up a file listing, so they are named with
a dot so they dont show up in normal listings.
There are a couple of sorting options with ls. The command ls -lt gives a long listing sorted by
modification time. The command ls -lS sorts by size.

Page 31

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 2. FILES AND DIRECTORIES


$ ls
emailform.php linux_foundations.pdf
$ ls -l
total 396
-rw-rw-r-- 1 robc robc
2659 Aug 25 2009 emailform.php
-rw-r--r-- 1 robc robc 391751 Mar 5 15:29 linux_foundations.pdf
$ ls -la
total 460
drwx-----drwxr-xr-x
-rw-------rw-r--r--rw-r--r--rw-r--r--rw-rw-r--rw-------rw-r--r--rw-------

2
7
1
1
1
1
1
1
1
1

robc
root
root
robc
robc
robc
robc
root
robc
robc

robc
4096 Mar
root
4096 Jun
root
1268 Mar
robc
24 Jun
robc
176 Jun
robc
124 Jun
robc
2659 Aug
root
35 Mar
robc 391751 Mar
robc
4118 Dec

5
26
8
15
15
15
25
8
5
14

15:29
2009
14:20
2009
2009
2009
2009
14:20
15:29
12:49

.
..
.bash_history
.bash_logout
.bash_profile
.bashrc
emailform.php
.lesshst
linux_foundations.pdf
.viminfo

$ ls -latr
total 460
-rw-r--r--rw-r--r--rw-r--r-drwxr-xr-x
-rw-rw-r--rw-------rw-r--r-drwx------rw-------rw-------

1
1
1
7
1
1
1
2
1
1

robc
robc
robc
root
robc
robc
robc
robc
root
root

robc
124 Jun
robc
176 Jun
robc
24 Jun
root
4096 Jun
robc
2659 Aug
robc
4118 Dec
robc 391751 Mar
robc
4096 Mar
root
35 Mar
root
1268 Mar

15
15
15
26
25
14
5
5
8
8

2009
2009
2009
2009
2009
12:49
15:29
15:29
14:20
14:20

.bashrc
.bash_profile
.bash_logout
..
emailform.php
.viminfo
linux_foundations.pdf
.
.lesshst
.bash_history

Page 32

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

2.3 Creating Directories and files


The touch command creates a file with the name provided. It is a zero size file. If the file already
exists, the touch command updates the modification date on the file to the current time.
To create a directory, the mkdir command is used. This command will create a new subdirectory
under the current directory with a relative path name, or can create a directory in the given location
with an absolute path name. With the recursive option (-p), any needed directories that dont exist
in order to create the directory will also be created.
$ mkdir testdir
$ mkdir -p testa/testb/testc
In the previous example, the testa and testb directories will be created if necessary before creating
directory testc.
To remove a directory, the rmdir command is used. In order to remove the directory, the directory
must be empty. If the directory is not empty, the command will fail with an error message. The -p
option can also be used with rmdir. It will remove all the directories in the command, starting with
the lowest in the directory structure and remove them moving up, until a non-empty directory is
encountered. In a later section, we will see how to remove a directory that is not empty.

Page 33

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 2. FILES AND DIRECTORIES

2.3.1 cp
The copy command, cp allows a user to make a copy of a file or directory. The syntax of the copy is
as follows:
cp options source target
Some of the common options are shown below:
Option

Description

-i

Interactive

-r

Recursive
Table 2.4: Options for the cp command

If the target file name already exists, the source file will copy over the top of the target. The
interactive option exists in order to prompt the user if the target already exists. This prevents
accidently copying over a needed file. If the target is a directory, the file is copied into the directory,
not over the top of the directory.
$ ls
filea
$ cp filea fileb
$ ls
filea fileb
$ cp -i filea fileb
cp: overwrite fileb?
$ cp filea

Page 34

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

2.3.2 mv
The mv command is very similar to the cp command, only it moves a file instead of copying it. Like
with copy, move will overwrite a target file without prompting.
Option

Description

-i

Interactive

-r

Recursive
Table 2.5: Options for the mv command

$ ls
filea fileb
$ mv filea filec
$ ls
fileb filec
$ mv -i fileb filec
mv: overwrite filec?y
$ ls
filec
$ mv filec

Page 35

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 2. FILES AND DIRECTORIES

2.3.3 rm
The remove command, rm, deletes a file or directory. Unlike other operating systems, when a file
is deleted, there is no reasonable way to get it back. The rm command should be used with care. If
the rm command is used on a directory, it will not delete the directory unless the recursive option
is used. This will remove all files in the directory and will recurse all subdirectories, deleting all the
files from them, before removing the subdirectories and the target directory. The interactive option
will prompt before removing any file, just to make sure. There is also a -f option, which will force
deletion without any prompting.
Option

Description

-i

Interactive

-r

Recursive

-f

Force
Table 2.6: Options for the rm command

$ ls -l
-rw-rw-r--rw-rw-r--rw-rw-r-drwxrwxr-x

1
1
1
2

$ rm filea
$ ls
fileb filec

robc
robc
robc
robc

robc
0
robc
0
robc
0
robc 4096

May
May
May
May

3
3
3
3

19:16
19:11
19:17
19:17

filea
fileb
filec
testdir

testdir

$ rm -i fileb
rm: remove regular empty file fileb?y
$ ls
filec testdir
$ rmdir testdir
rmdir: testdir: Directory not empty
$ ls testdir
filed
$ rm -r testdir
$ ls
filec

Page 36

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

2.4 Viewing Files


Unlike in other operating systems, file extensions have no meaning in Linux. They are used at
indicators of the file type for humans but have no real meaning. The file extensions are just part
of the file name. The Linux kernel only distinguishes between executable and non-executable files.
Some commands use the extension for convenience.
The file command reports the type of file by examing the file contents.
$ ls
emailform.php linux_foundations.pdf test
$ file *
emailform.php:
PHP script text
linux_foundations.pdf: PDF document, version 1.2
test:
directory
$ mv linux_foundations.pdf file.doc
$ ls
emailform.php file.doc test
$ file *
emailform.php: PHP script text
file.doc:
PDF document, version 1.2
test:
directory

Page 37

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 2. FILES AND DIRECTORIES

2.4.1 cat and less


The cat command was originally created to concatenate files together and display the output on
STDOUT. With only one file, it just displays the output to STDOUT. Thus, it is commonly used to
view files. The problem with cat is that for large files most of the file scrolls off the screen.
Thus, a command that shows a single screen at a time was necessary. Originally, the more command
was created, but it was fairly simple. Later, an improved version, named less was created. Within
less, a user can move up and down with the file, viewing the entire file. The less command ends by
hitting q.

Page 38

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

2.4.2 head and tail


The head and tail commands are used to view the top and bottom of files respectively. By default,
they each show ten lines from the file. However, this can be changed for both commands with the
-n option and a number.
$ head sample
207.46.199.42 208.138.254.150
208.138.254.150
208.138.254.150
208.138.254.150
208.138.254.150
208.138.254.150
208.138.254.150
208.138.254.150
208.138.254.150

[01/May/2010:18:24:28 -0400]
- [01/May/2010:18:30:32 -0400]
- [01/May/2010:18:30:33 -0400]
- [01/May/2010:18:30:33 -0400]
- [01/May/2010:18:30:33 -0400]
- [01/May/2010:18:30:34 -0400]
- [01/May/2010:18:30:34 -0400]
- [01/May/2010:18:30:34 -0400]
- [01/May/2010:18:30:34 -0400]
- [01/May/2010:18:30:34 -0400]

$ head -n 5 sample
207.46.199.42 - - [01/May/2010:18:24:28 -0400]
208.138.254.150 - - [01/May/2010:18:30:32 -0400]
208.138.254.150 - - [01/May/2010:18:30:33 -0400]
208.138.254.150 - - [01/May/2010:18:30:33 -0400]
208.138.254.150 - - [01/May/2010:18:30:33 -0400]
$ tail -3 sample
67.195.111.185 - - [01/May/2010:19:09:34 -0400]
67.195.111.185 - - [01/May/2010:19:09:35 -0400]
207.46.199.190 - - [01/May/2010:19:15:32 -0400]
The tail command also has the very useful -f option. This option shows the last lines of the file
and they continues to run. As new lines are added to a file, they are displayed on STDOUT. This
command is very useful for watching log files to track down problems. To end the tail -f command,
hit Ctrl-C.

Page 39

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 2. FILES AND DIRECTORIES

2.4.3 wc
The word count file doesnt display a file, but does show statistics on the content of the file. Without
any options, it shows the number of lines, words, and characters in a file. The options shown in the
table below give counts on just one of the stats.
Option

Description

-l

Count of lines

-w

Count of words

-c

Count of characters
Table 2.7: Options for the wc command

$ wc /usr/share/dict/words
98569 98568 931467 /usr/share/dict/words

Page 40

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

2.5 Finding Files with locate


Their are two commands for finding files on a system. The simpler to use method is the locate
command. It does not search the system in real time, but looks up the file name in a database.
The database is updated once a day, usually late at night. This leads to quick searches, but has
the problem of not finding correct results for files that have been added or deleted since the last
database update.
$ locate hostname
/bin/hostname
/etc/hostname
/etc/init.d/hostname.sh
/etc/rcS.d/S02hostname.sh
/usr/share/doc/hostname
/usr/share/doc/hostname/changelog.gz
/usr/share/doc/hostname/copyright
/usr/share/man/man1/hostname.1.gz
/usr/share/perl/5.8.8/hostname.pl
/var/lib/dpkg/info/hostname.list
/var/lib/dpkg/info/hostname.md5sums

Page 41

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 2. FILES AND DIRECTORIES

2.5.1 Finding Files with find


The find command is a more complicated and slower method for finding files on a system. However,
it can also find files by means other than their name. The find command takes three parameters. The
first parameter is the directory to start searching in. The find command always searches recursively.
If this option is not used, find defaults to the current directory. The second directory specifies the
search criteria. Some of the search options are shown in the table below. The final parameter is
the action to be performed. By default, this is the -print action, which displays the files that match.
Other actions include -ls, which gives full details on the matching file and -exec, which allows
commands to be run against each matching file.
Option

Description

-name

File name

-type

File type

-user

File owner

-perm

File permission

-mtime

Modification time, in days


Table 2.8: Search criteria for the find command

The following examples show ways to use the find command.


$ find /home -name *.jpg -print
Prints all jpeg files in the home directory
$ find . -mmin -30 -ls
Detailed list of all files modified in last 30 minutes
$ find / -user ann -exec chown john {} \;
Change ownership of all of anns files to john.
In the last example, the {} is used to indicate where the file name that was matched should go in the
command, the chown command is run once for each file matched. The final backslash and semicolon
indicates the end of the command to be executed.

Page 42

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

2.6 Lab Activity


Part 1. Type the following commands (but not the comments following the #):
$
$
$
$
$
$
$

cd
pwd
cd /
pwd
ls
cd
pwd

#change to home directory


#are you in correct directory
#change to root directory
#verify that
#list the files in the root
#change to home directory, a different way
#back home

Part 2. Type the command ls -Fa. How many files and directories are in your home directory? How
many hidden files?
Part 3. Use the touch command to create 3 files named filea, fileb, and filec. Use the comman ls -l.
What size are these files? When were they last modified?
Part 4. Create a directory called test. Change into that directory. Use the ls command with and
without the -l option. What is the difference? What are the files named . and ..?
Part 5. Copy file file /usr/share/dict/words into the test directory. How large is this file? View the
first and last 10 lines of this file. Use cat and less to view the file words. Why is one better than the
other?
Part 6. Make a directory under your home directory called test2 and another called test3. Copy the
words file from test to test2. Make sure it is in both test and test2. Is it the same size? Move words
from test2 to test3. Is it still in directory test2? Remove the directories test2 and test3 using rmdir.
What do you need to do to remove test3?
Part 7. Use the find command to find all files under your home directory that were modified in the
last hour.
Part 8. Use the locate command to find files with words in their name. Why didnt the copy inside
your test directory show up?

Page 43

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 2. FILES AND DIRECTORIES

Page 44

c
2010
n + 1, Inc All Rights Reserved

Chapter 3

The vim editor


Objectives
Understand the vi modes
Be able to use movement and editing commands
Explain search and replace options in vi

45

CHAPTER 3. THE VIM EDITOR

3.1 History of vim


The vim (Vi IMproved) editor is a full-screen text editor that is a component of Linux and Unix
operating systems. Originally developed by Bill Joy at the University of California Berkeley, this
visual editor is one of a number of editors that are available for Unix.
vim is the most popular replacement for vi on Linux machines. It is a text editor that extends the
basic functionality to provide a number of enhancements, including:
multi-level undo
multi-windows
multi-buffers
syntax highlighting
command line editing
filename completion
on-line help
visual selection

Page 46

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

3.2 Modes
The vim editor is modal, within vim commands must be issued to change between modes. There
are three modes: insert, command, and last line. Command mode is the default mode and is used
to issue commands. To enter command mode from insert mode, press the ESC key. From within
command mode, there are a number of different insertion commands that will enter insertion mode.
Each of these commands has a special effect, we will see their usage in a later section. Last line
mode echoes commands at the bottom of the screen, on the last line. Many of these commands
begin with a colon (:) and are called ex commands, named after the editor they originally came
from. Other last line commands begin with a backslash (/), question mark (?), or exclamation point
(!). The last line command is executed by pressing the Enter key.
When using vim, there are a few basics to remember. First, once in vim, the entire window is used.
If the number of lines in the file are less than the number of lines on the screen, the remaining lines
are displayed as a tilde ( ).
Secondly, the bottom of the file displays the file name followed by the total number of lines and
characters in the file. On the right side is the cursor location described as line number, column
number. The cursor location changes as the cursor is moved around the file. On the far right is the
completion percentage of the document of the current screen shot. It will read Top if at the top of
the Document, Bot if at the bottom and All if the entire document is shown on screen.
Finally, all commands are case sensitive. For example, w and W both move the cursor forward one
word, but the uppercase W ignores most punctuation.

Page 47

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 3. THE VIM EDITOR

3.3 Saving and Exiting


Several options are available for starting vim. With no arguments, vim opens a new file. Adding a
filename as an argument, vim will open a file and position the cursor is positioned in the default
location - upper left corner.
When a file is opened, vim copies the file into a buffer. A buffer is an area temporarily set aside in
the memory until a write command is given. Once this command is given, vim copies the edited
buffer back to the original file, overwriting the original text. When a new file is opened, vim opens
an unnamed buffer. The buffer is named when the file is saved.
There are various commands for saving and/or quitting vim. The table below lists the various save
and/or quit commands. Note the command charter, ! instructs vim to override any warnings that a
command produces, and to perform that command regardless.
Command

Description

:w

Write to current file

:w filename

Write to named file

:wq

Write and quit

:w!

Save even if read only

:wq!

Save and quit even if read only

:q!

Quit and ignore changes

ZZ

Same as :wq
Table 3.1: Saving and Exiting Commands

Note that vim does not allow the user to quit a file if changes have been made. Only the :q! command
allows the user to quit the file without saving the edits made from the last time the file was saved.

Page 48

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

3.4 Movement commands


Movement within a file is done by cursor control keys from within command mode. The table
below lists basic movements and the associated keys:
Command

Description

Move cursor left

Move cursor down

Move cursor up

Move cursor right

Move to next word

Move to previous word

Move to start of line


Table 3.2: Movement Commands

While in command mode, there are other commands that allow you to move in larger steps. The
parenthesis move the cursor to the next or previous sentence. The brackets move to the next or
previous paragraph. The command G moves the cursor to the last line of the file, while putting a
number before G moves to that specific line number.
Prefacing a command with a number causes the command to repeat itself that number of times.
For example, to move forward three sentences, the command 3) would be executed. Not only do
these numeric prefaces work when moving the cursor, they also work with insertion, deletion, and
changing text commands.
Control key commands move the cursor by larger chunks of text. The table below shows the control
key movement commands.
Command

Description

Ctrl-F

Move forward a screen

Ctrl-B

Move backward a screen

Ctrl-D

Move forward half a screen

Ctrl-U

Move backward half a screen


Table 3.3: Paging Commands

Page 49

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 3. THE VIM EDITOR

3.5 Insertion commands


The are 6 major insertion commands. These are listed in the table below.
Command

Description

Insert at cursor

Insert at beginning of line

Append after cursor

Append after end of line

Insert on line below current

Insert on line above current

Replace single character

Overwrite text
Table 3.4: Insertion Commands

The i command is the primary insertion command. It changes modes from command to insertion
and allows insertion of text at the point of the cursor. The a command is similar but appends text
after the point of the cursor. I and A work similarly to the lowercase equivalents only they start the
insertion at the beginning of the line and append at end of line respectively. The o and O commands
enter insertion mode on a blank line after or before the current line.
There are two more commands that are similar to the insertion commands. The r command allows
you to replace the single character under the cursor. For example, if you placed the cursor on the
letter t and then hit rc, that would replace the t with a c. The R command puts vim into Replace
mode, which types over the current text. Hitting esc exits back to command mode.

Page 50

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

3.6 Editing Commands


There are a large number of editing commands in vim. Most users know only a subset of these
commands. The power of vim comes from its ability to edit with ease. While no one uses all of the
available command, learning a large set of them is important.
Some of the more commonly used editing commands manipulate enter lines. The dd command
deletes an entire line. However, it is placed in a temporary buffer. The p command pastes that line
under the location of the cursor. Thus, dd is equivalent to a cut. The yy command yanks a line
into the buffer. It is equivalent to a copy. As with other commands, these can be proceeded with a
number to delete or yank multiple lines into the buffer.
The x command deletes the single character under the cursor. The d and y commands can be
combined with a movement command to delete or yank part of a line. For example, dw deletes
from the cursor position to the end of the word, yw would copy from the cursor position to the
end of the word. There is a version of this for all the movement commands. Like with the full line
commands, p puts the last block cut or yanked into the text after the cursor.
There are a few other editing commands that are very important. The J command joins two lines
together. The D command deletes to the end of the line. The u command is an undo. The last
command executed is undone.
The table below summarizes these commands. There are more editing commands in vim than those
covered here, but these are a good start to becoming a vim expert.
Command

Description

dd

Delete line

yy

Copy line

Paste buffer

Delete character

dw

Delete to end of word

yw

Copy to end of word

Delete to end of line

Join lines

Undo command
Table 3.5: Editing Commands

Page 51

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 3. THE VIM EDITOR

3.7 Search and Replace


Searching in vim is very easy. From command mode, start a search by typing the a /. The / will go
to the last line and the search term follows the /. This searches forward from the cursor until the
pattern is matched. Hitting the n repeats the search to the next match. N repeats the search in the
backwards direction.
The table below shows the basic searching commands.
Command

Description

/pattern

Search forward for pattern

?pattern

Serach backward for pattern

Repeat last search

Repeat last search in opposite direction


Table 3.6: Search Commands

The Search and Replace syntax is shown in the following table. The command begins with a :, an
optional range, and an s. Without a range, the Search and Replace is done on the current line only. If
the range is a percent sign, then the entire document is searched. Otherwise a pair of line numbers
separated by a comma gives the range to search.
Following the s, there are 3 slashes. Between the first two is the pattern to search for and between
the second and third is the pattern to replace it with. After the final slash, there is an optional g
for global. Without it, only the first match is replaced. With it, all of the matches in the range are
replaced.
:s/pattern/replace/

Replace pattern on current line

:%s/pattern/replace/ Replace first matching pattern


:%s/pattern/replace/g Replace all matching patterns
:10,20s/pattern/replace/g Replace all matching patterns in line 10 through 20

Page 52

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

3.8 Lab Activity


Part 1. Do the following motion commands in sequence, comments will allow you to verify that
you are at the right place:
$ vim doi.txt
7j
15l
4k
0
G
w
w
50G

#move
#move
#move
#move
#move
#move
#same
#move

down 7 lines, cursor should be on W in We


15 characters to the right, cursor should be on r in truth
up 4 lines, cursor should on o in to
cursor to beginning of line, e in equal
to last line, f in fortune
to next word, a in and
thing, now o in our
to line 50, H in He

try other motion commands to move around in file, then


:q!

Page 53

#quit without saving

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 3. THE VIM EDITOR


Part 2. Do the following commands to edit the file:
$ vim doi.txt
/unalienable
ri
xx
i
un
ESC
:65
2dd
kkk
p

#search for unalienable, un in unalienable


#replace u with i
#delete i and n
#enter insert mode
#enter un to make word unalienable again
#return to command mode
#move to line 65, H in He
#remove two lines to buffer
#move up three lines
#paste lines cut

/Fro
n
nnnnn
l
2x
i
or
ESC
jj
:s/Fro/For/
:81,87s/Fro/For/

#search for Fro, F on line 77


#F on line 79
#finds each instance of Fro, returning to line 77
#move to right, r in Fro
#delete r and o
#enter insert mode
#enter text, to spell For
#return to command mode
#go down two lines, o in Fro, line 79
#fix spelling on line 79
#fix spelling for lines 81 thru 87

/tyrant
w
w
D
J
:wq

#find word tyrant


#move to next word, which is , after tyrant
#move to next word, A
#delete to end of line
#join next line to this one
#save changes and quit

Part 3. Open memo.txt in vim. Change the memo so that your name is in the From field. Update
the date. Correct the misspellings of the words teh to the and ani to any. Use search and
replace on teh and change ani by hand. Save file as memo2.txt. Exit from vim.

Page 54

c
2010
n + 1, Inc All Rights Reserved

Chapter 4

The BASH shell


Objectives
Understand command line redirection
Understand wildcards and file globbing
Describe shell and environment variables
Explain command line quoting
Be able to run a shell script
Be able to use Bash shell shortcuts

55

CHAPTER 4. THE BASH SHELL

4.1 Command Shell Basics


The design of Linux puts the kernel at the center of all activity. The kernel handles communication
with all the devices and handles multitasking processes. Normal users never interact directly with
the kernel. Instead there is a shell which communicates with the kernel. Users interact with the
shell and the shell communicates with the kernel on behalf of the user.

Page 56

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

4.1.1 Channels
All programs in Linux have at least 3 communication channels associated with them. Every channel
also has a number associated with the channel. Many programs may open other communication
channels which also have numbers assigned to them at the time.
The first channel that all programs have is called STDIN and has a channel number of zero. This
communication channel is where the program gets input. It is usually from the keyboard.
The second channel is STDOUT and has a channel number of one. This is where the program sends
output. This is usually the terminal.
The third channel that all programs have associated with them is STDERR. This is where programs
send error messages. Like STDOUT, it usually sends these messages to the terminal.
Any other channels opened by a program are connected to file handles. These additional channels
have numbers starting with three.

Page 57

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 4. THE BASH SHELL

4.1.2 Redirection
File redirection allows the user to redirect the communication channels associated with programs
to files or other programs. The chart below shows the major file redirection characters.
> STDOUT to file
< STDIN from file
>> STDOUT append to file
2> STDERR to file
2>> STDERR append to file
&> STDOUT and STDERR to file
$ echo "This is a test" > echo.txt
$ cat echo.txt
This is a test
$ who >> whofile
$ cat <whofile
robc
:0
robc
pts/1
robc
pts/2
robc
pts/3

Page 58

2010-02-10
2010-03-09
2010-03-09
2010-03-10

08:41
08:53 (:0.0)
09:06 (:0.0)
08:15 (:0.0)

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

4.1.3 Pipe
The pipe character is an additional redirection character that affects two commands at once. It connects the STDOUT channel from the first command to the STDIN channel of the second command.
This allows the formation of complicated processes from a series of simple commands.
For example, often a command returns more than a screen full of information, Piping the output to
less allows the user to read the results one page at a time.
$ ls -l | wc -l
169
Building complex commands from a series of simple commands via the pipe is a common task of
Linux system administrators. It is part of the Unix design principles discussed previously.

Page 59

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 4. THE BASH SHELL

4.2 Filename Matching


Many commands take a list of filenames as arguments that would be tedious to type. Wildcard
patterns allow a way to match many filenames and easily pass them as arguments to a command.
This is called file globbing. There are a serious of special characters that are used as wildcards.
When these wildcards are used, the shell expands the wildcard to a full list of files. As far as the
command is concerned, the full list was passed to the command.
There are three wildcards for file globbing, listed in the table below.
Option

Description

Matches anything

Matches single character

[...]

Matches single character within group


Table 4.1: Wildcards

The most commonly used wildcard is the asterisk (*). This will match anything. The question mark
(?) matches any single character. The braces ([...]) match a character class. This can contain any
characters to be matched and ranges of characters can be represented with a dash.
Some examples of wildcards are shown below:
$ ls file*
file1 file10

file11

file12

file2

file3

file4

file8

file9

filea

fileb

The previous example would match all files beginning with file. The following would match all
files beginning with file and containing exactly one more character.
$ ls file?
file1 file2

file3

file4

file8

file9

filea

fileb

filec

This next example matches any filename beginning with file and then ending with a single digit.
$ ls file[0-9]
file1 file2 file3

file4

file8

file9

The previous example matches file1 and file9 but not file11.

Page 60

c
2010
n + 1, Inc All Rights Reserved

filec

LINUX FOUNDATIONS

4.3 Variables
There are two types of variables used by the shell. They are shell variables and environment
variables. Shell variables are only available to the current shell, if another shell is opened, those
variables will not exist there. Environment variables, on the other hand, exist across shells.
The export command is used to turn a shell variable into an environment variable. First the variable
must be set. To set a variable, the equals sign (=) is used. There can be no spaces around the equals
sign. Then the variable can be accessed using the dollar sign before the name.
$ AGE=40
$ echo $AGE
40
$ bash
$ echo $AGE

#start new shell

$ exit
$ echo $AGE
40
$export AGE
$ echo $AGE
40
$ bash
$ echo $AGE
40
The env command displays all of the environment variables on the system, along with their current
values.

Page 61

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 4. THE BASH SHELL

4.4 Quoting
When the shell parses input, certain characters have special meaning, and are interpreted instead
of being read literally. These special characters are called metacharacters. If you need to use these
characters, you need to require the shell to not use them for their special meaning, but to treat them
literally. In order for this to occur, the metacharacters must be quoted.
If only a single character needs to be quoted, the character can be proceeded with a backslash. The
backslash is a metacharacter with a special meaning that the next character is quoted. To use an
actual backslash, proceed it with an additional backslash (really, this works).
If there are many metacharacters in a line, backslashing them all can be tedious. In this case,
enclosing them within double quotes will treat the metacharacters literally, with one exception.
Variables still will be evaluated within double quotes, so the dollar sign is still a metacharacter.
However, within single quotes, all metacharaters are treated literally, thus dollar signs will be
treated as dollar signs, not as the start of a variable name.
One final type of quoting exists, this is the nesting of commands. If a piece of text is surrounded
by backticks, it will be treated as a command and the result of the command inserted into the text.
There is a second syntax for this, instead of the backticks, the command can be surrounded by
parenthesis and proceeded by a dollar sign. Examples of both ways are below.
$ echo "The date is date +%x"
$ echo "The date is $(date +%)"

Page 62

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

4.5 aliases
In Bash, command shortcuts can be created. These are called aliases. To create an alias, the alias
command is used. Examples are below:
$ alias rm="rm -i"
$ alias ll="ls -latr"
With those aliases, every time the user types rm, rm -i will be run instead. This allows the user to
create shorter versions of commonly run commands.
To remove an alias, the unalias command is used.
$ unalias rm

Page 63

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 4. THE BASH SHELL

4.6 Lab Activity


Part 1. Output the first 10 and last 10 lines of the file words into files called filea and fileb.
Part 2. Run the command cat /etc/shadow and output the error messages to stderr.txt. View this
file.
Part 3. How many files in the /etc directory begin with an s? Determine this using ls and the *
wildcard. How many begin with an s and have a 3rd character that is an a? Use ls, *, and the ?
wildcard.
Part 4. Create a shell variable called NUMBER with a value equal to 8. Use the echo command to
verify the value of NUMBER.
Part 5. Start a c shell. What is the value of NUMBER? Why is this the result? Exit the csh.
Part 6. Make NUMBER a environment variable. Repeat Step 6.

Page 64

c
2010
n + 1, Inc All Rights Reserved

Chapter 5

File Permissions
Objectives
Explain changing the owner or group associated with a file
Describe the file permissions of a file or directory
Be able to change permissions on a file or directory
Understand default permissions based on masking

65

CHAPTER 5. FILE PERMISSIONS

5.1 Owners and Groups


All files in Linux have an owner and group associated with them. The owner is a username and the
group contains a set of users. The file /etc/passwd contains information related to each user. The
table below shows the the fields in /etc/passwd.
Field

Description

username

Name of the account

password

x - Password in /etc/shadow

UID

User ID number

GID

Group ID number of users primary group

GECOS

Contains info about the user

home dir

Users home directory

shell

Users default shell


Table 5.1: Fields in /etc/passwd

Every user is a member of at least one, but quite possibly, many, groups. Information on groups is
stored in the file /etc/groups. The table below shows the /etc/group fields.
Field

Description

group name

Name of the group

password

x - Password in /etc/gshadow

GID

Group ID number

user list

Comma separated list of users in group


Table 5.2: Fields in /etc/group

Originally, passwords were stored as encrypted strings in the /etc/passwd and /etc/group files. All
users need read access to these files and this became a security issue. Passwords are no longer stored
in these files (although the field still exists). Instead, they are stored in files called /etc/shadow and
/etc/gshadow. Only root has read access to these files. In addition to the encrypted passwords,
these files also contain information related to aging of passwords.

Page 66

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

5.1.1 useradd and userdel


The useradd command can be used by the root user to add new users to the system. The command
creates a private group for the user and also creates the users home directory. The useradd command
also sets up the account in the /etc/passwd and /etc/group files.
The userdel command is used to delete a users account from the system. If the -r option is used
with userdel, in addition to removing the account, the users home directory and mail spool are also
deleted.

Page 67

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 5. FILE PERMISSIONS

5.1.2 chown and chgrp


With ls -l, the owner and group for a file can be seen. To change ownership, the chown command is
used. Only root can change ownership on a file. To change the group, the chgrp command is used.
However, chown can also change the group, so it usually used to do both.
The examples below show changing the ownership and then both the owner and group of a file.
# ls -l test.txt
-rw-r--r-- 1 robc robc 0 2009-08-10 10:45 test.txt
# chown root test.txt
# ls -l test.txt
-rw-r--r-- 1 root robc 0 2009-08-10 10:45 test.txt
# chown root:root test.txt
# ls -l test.txt
-rw-r--r-- 1 root root 0 2009-08-10 10:45 test.txt
# chgrp acct test.txt
-rw-r--r-- 1 root acct 0 2009-08-10 10:45 test.txt
To change both the owner and the group, the two terms are separated by a colon, with the owner
coming first. The chown and chgrp command also can be used recursively, with the -R option.

Page 68

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

5.2 File Permissions


When viewing the long listing of the ls command, the first 10 characters are a set of dashes and
letters.
$ ls -l test.txt
-rw-r--r-- 1 robc robc 0 2009-08-10 10:45 test.txt
The first character shows the type of file. A dash represents a regular file and a d represents a
directory. The next 9 characters cover the file permissions. The file permissions determine who can
read, write, or execute a file. For a directory, the permissions mean something slightly different.
Execute permission, on a directory, allows a user to change into the directory. Read permission
allows the user to list the directorys contents and write permission allows the user to create and
delete files from the directory.
Within the 9 characters showing the file permissions, there are 3 groups of 3 permissions, for three
different user types. The first 3 permissions show the read, write, and execute permissions for the
owner of the file. The next group of three show the same thing for members of the group associated
with the file. The final group of three shows the permissions for all other users. The order within
each group is read, then write, then execute and when the permission is give, is represented by a r,
w, and x respectively. If the permission is not allowed, a dash is shown.
Thus, on the example given above, the owner of the file has read and write permission. The group
members and all other users have read permission only. In the following example, the owner has
read, write, and execute permission. The group members have read and execute permission, and
all other users have no permission.
$ ls -l payroll.sh
-rwxr-x--- 1 robc acct 83397 2009-08-10 10:45 payroll.sh

Page 69

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 5. FILE PERMISSIONS


File permissions are often represented numerically. The numerical representation is a 3 digit number
with each digit showing the permissions for each grouping. Each digit is calculated by summing
the permissions, with read being a 4, write a 2, and execute a 1. Below shows the conversion and
some common examples.
rwx rwx rwx
421 421 421
rw-r--r-6 4 4
644
rwxr-x--7 5 0
750
r--r--r-4 4 4
444
rwxr-xr-x
7 5 5
755
rw------6 0 0
600

Page 70

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

5.2.1 chmod
The chmod command is used to change file permissions. Only the owner of a file (and the root
user) can change the permissions on a file. There are two methods for changing the permissions, a
symbolic representation and a numerical representation. The symbolical can add (+), subtract (-),
or set (=) permissions for the three groups, the user/owner (u), the group(g), and other users (o), or
it can be changed for all (a).
The examples below show how to combine these symbols.
Add write permission for owner
$ ls -l test.txt
-r--r--r-- 1 robc robc 0 May 10 10:41 test.txt
$ chmod u+w test.txt
$ ls -l test.txt
-rw-r--r-- 1 robc robc 0 May 10 10:41 test.txt
Add execute permission for everyone
$ chmod a+x test.txt
$ ls -l test.txt
-rwxr-xr-x 1 robc robc 0 May 10 10:41 test.txt
Subtract write permission from group and others
$ ls -l test2.txt
-rw-rw-rw- 1 robc robc 0 May 10 10:41 test2.txt
$ chmod go-w test2.txt
$ ls -l test.txt
-rw-r--r-- 1 robc robc 0 May 10 10:41 test2.txt
Set specific permissions on file
$ chmod u=rwx,g=rx,o= test.txt
$ ls -l test.txt
-rwxr-x--- 1 robc robc 0 May 10 10:41 test.txt
It is also possible to use chmod with the numerical representation learned in the previous section.
The final example from above is shown numerically below.
$ chmod 750 test.txt
Once the numerical representation is learned it is generally easier to use. However, when using
chmod with the recursive option (-R), it is generally better to use the symbolic version and add or
subtract the permissions that need to change. Generally, directories need the execute permission
and files dont, so recursively changing directories and files at the same time can lead to problems,
when the numerical method is used.

Page 71

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 5. FILE PERMISSIONS

5.3 umask
By default, files are created with a 666 permission (read and write for everone) while directories are
created with 777 permission (read, write, and execute for all).
$ mkdir testdir
$ touch testfile
$ ls -l
total 4
drwxr-xr-x 2 robc robc 4096 2009-07-11 11:21 testdir
-rw-r--r-- 1 robc robc
0 2009-07-11 11:22 testfile
The directory testdir has 755 permissions. The file testfile has 644 permissions. Why dont they match
the defaults above? This is due to the umask. The umask is subtacted from the defaul permissions
when a file or directory is created in order to set the permissions. The umask command shows the
current umask value. The command can also set the value.
$ umask
0022
$ umask 0002
$ umask
0002
$ mkdir testdir2
$ touch testfile2
$ ls -l
total 8
drwxr-xr-x 2 robc
drwxrwxr-x 2 robc
-rw-r--r-- 1 robc
-rw-rw-r-- 1 robc

robc 4096 2009-07-11 11:21 testdir


robc 4096 2009-07-11 11:25 testdir2
robc
0 2009-07-11 11:22 testfile
robc
0 2009-07-11 11:25 testfile2

The new direction now has 775 permission. The new file has 664 permission. The change in umask
from 0022 to 0002 allows write permission for groups.

Page 72

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

5.4 Lab Activity


Part 1. Change file permissions on filea so that everyone has read permission and the owner has
read and write permission. Use symbolic notation. Repeat for fileb using numerical notation.
Part 2. As root, create a new user named guest. Change ownership of filec so that guest is the
owner. Set permissions so that only the owner has read permission. Exit from root. As your regular
user, attempt to access filec.
Part 3. Touch filed. Change your umask to 0027. Touch filee. What is the permission difference
between filed and filee?

Page 73

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 5. FILE PERMISSIONS

Page 74

c
2010
n + 1, Inc All Rights Reserved

Chapter 6

Text Processing
Objectives
Explain the use of the grep command
Describe the different text processing tools
Understand piping together the tools to create a complex filter

75

CHAPTER 6. TEXT PROCESSING

6.1 Text Searching with Grep


The grep command searches a file for a specified pattern and then displays each line containing the
specified pattern to STDOUT. Both basic and extended regular expression syntaxes are supported
by grep. The format for this command is
grep pattern filename
If more than one file is specified, the filename will be displayed before each line.
If no filename is specified, grep takes its input from STDIN. Thus, grep can be utilized on either side
of a pipe to scan throught the output of another command. For example:
[user@localhost ]$ who | grep robc
robc
tty7
2009-08-11 06:56
robc
pts/0
2009-08-11 06:57
robc
pts/1
2009-08-11 07:10
robc
pts/2
2009-08-11 08:54
robc
pts/3
2009-08-11 08:55

(:0)
(:0.0)
(:0.0)
(:0.0)
(:0.0)

There are several options available for grep. The table belows lists only the most common.
Option

Description

-i

Ignore case

-v

Return lines that do not match

-c

Return count of matches

-A NUMBER

Return line matching and NUMBER of lines after match

-B NUMBER

Return line matching and NUMBER of lines before match

-C NUMBER

Return line matching and NUMBER of lines before and after


match
Table 6.1: Options for the grep command

Page 76

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

6.1.1 Regular Expressions


Regular expressions combine alphanumeric characters with special characters to match patterns
of characters in text. The special characters have many similarities to wildcards, but they are not
the same. Wildcards are used for file globbing. Regular expressions are used in a number of
applications, such as grep and vim.
Regular expressions are built up from expressions that match single characters. Most letters and
numbers match themselves. There are special characters, shown in the table below and in further
sections that match single characters or special circumstances. The . character matches any single
character. The caret and dollar sign are used to match the beginning and the end of line. The rest
of the table below shows special matches using the backslash and a character.
\t

Tab

\n

Newline

\r

Carriage return

Any single character

Beginning of line

End of line

Page 77

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 6. TEXT PROCESSING

6.1.2 Character Classes


Character classes match a single character that is contained within the class. A class can be created
by placed the class of characters to be matched within a pair of braces. The entire class can be listed
or ranged can be defined with a dash.
If a caret is placed at the beginning of the list of characters in the class, then the characters class
includes anything except the list of characters.
There are also a number of predefined character classes that are commonly used. Some of these are
shown in the following table. Note that the braces are part of the character class, so to use them
requires placing this within another set of braces. See the examples below.
Character Class

Description

[:alpha:]

Match all letters

[:digit:]

Match all digits

[:alnum:]

Match all letters and digits

[:lower:]

Match all lower case letters

[:upper:]

Match all upper case letters

[:space:]

Match all whitespace

[:cntrl:]

Match all control characters


Table 6.2: Predefined character classes

Match any 4 letter line beginning with a lowercase letter and ending with a n
$ grep [a-z]..n$ /usr/share/dict/words
Match any lines in /etc/passwd containing a uppercase letter
$ grep [[:upper:]] /etc/passwd
Same as above
$ grep [A-Z] /etc/passwd
Match any lines in words file that begin with a digit
$ grep [[:digit:]] /usr/share/dict/words
What would this match?
$ grep [:upper:] /etc/passwd

Page 78

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

6.1.3 Quantifiers
Quantifiers greatly expand the power of regular expressions. They match certain amounts of
repeated patterns of the character preceeding the quantifier.
The table below shows the quantifiers and the number of repeats they match.
Quantifier

Description

Match 0 or more times

Match 1 or more times

Match 0 or 1 times

Match exactly n times

n,

Match n or more times

n,m

Match n to m times
Table 6.3: Regular expression quantifiers

The regular expression pattern ca*t would match all of the following:
ct
cat
caat
caaat
caaaaaaaaaaat
With advanced regular expressions, it is often necessary to use the -E option for extended regular
expressions.
Match all 15 letter words beginning with a vowel
$ grep -E [aeiou].{14}$ /usr/share/dict/words
Match all 12 letter words betting with a f, g, or h and ending in a r, s, or t
$ grep -E [fgh].{10}[rst]$ /usr/share/dict/words
Match all lines containing 2 or more uppercase letters in a row
$ grep -E [[:upper:]]{2,} /usr/share/dict/words

Page 79

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 6. TEXT PROCESSING

6.1.4 Parenthesis
When parenthesis surround part of a regular expression, it means one of three different things.
First, it makes a new atom out of that part of the expression. An atom can be acted upon by a
quantifier following the parenthesis. Instead of acting on the last character, it acts upon the entire
pattern within the parenthesis.
abc{3} matches abccc
(abc){3} matches abcabcabac
Secondly, the parenthesis can be used to create an option of regular expressions. The options are
separated with a pipe.
(abc|xyz) matches abc and xyz
Finally, the parenthesis can be used to create a group for referencing later. In some programs,
regular expressions can be used in a search and replace, and any patterns that match a part of the
expression within parenthesis is stored in a varible for use within the replace portion.

Page 80

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

6.2 sed
The sed command is a non-intereactive stream editor that uses commands similar to ed. The syntax
for sed takes a command and a file on which to act. The command is applied to each line of input
with results written to STDOUT. For example, to change also occurences of teh to the within a
file, the following sed command would be used:

$ cat memo.txt
To: All Staff
From: Heady teh Head Honcho
Date: June 1, 2006
Re: New Memo Format Effective June 1

In order to make interoffice communications easier, please adhere to teh following guidelines for wr
*
*
*
*
*
*
*

Clearly state teh purpose of teh memo in teh subject line and in teh first paragraph.
Keep language professional, simple and polite.
Use short sentences.
Use bullets if a lot of information is conveyed.
Proofread before sending.
Address teh memo to teh person(s) who will take action on teh subject, and CC those who need t
Attach additional information: dont place it in teh body of teh memo if possible.

$ sed s/teh/the/g memo.txt


To: All Staff
From: Heady the Head Honcho
Date: June 1, 2006
Re: New Memo Format Effective June 1

In order to make interoffice communications easier, please adhere to the following guidelines for wr
*
*
*
*
*
*
*

Page 81

Clearly state the purpose of the memo in the subject line and in the first paragraph.
Keep language professional, simple and polite.
Use short sentences.
Use bullets if a lot of information is conveyed.
Proofread before sending.
Address the memo to the person(s) who will take action on the subject, and CC those who need t
Attach additional information: dont place it in the body of the memo if possible.

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 6. TEXT PROCESSING


Two important concepts are illustrated here. First, enclose the sed command in single quotes.
Second, the sed command was applied to each line of the file, but no changes were made to the
original file. The results were displayed on STDOUT. To make changes permanent, you can redirect
the output to a temporary file and copy over the original file after changes have been verified as
correct.
$ sed s/teh/the/g memo.txt >tempfile
$ mv tempfile memo.txt
There are several options for sed and the most useful one for command line editing is the -n option.
Default output of sed is that each line is written to stdout whether or not the line was modified.
With -n, the only lines output are those specified with line addresses and the print function (p).
$ sed -n 1,5p memo.txt
To: All Staff
From: Heady the Head Honcho
Date: June 1, 2006
The sed command can also delete entire lines of text by using the d function. Specify line numbers
or a range of numbers and append the command string with a d. The remaining lines are written
to STDOUT.
$ sed 11,17d filename
To: All Staff
From: Heady teh Head Honcho
Date: June 1, 2006
Re: New Memo Format Effective June 1

In order to make interoffice communications easier, please adhere to teh following guidelines

Page 82

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

6.3 tr
The tr command is used to translate, squeeze and delete characters. As a default, tr looks for
characters in set1 and replaces them with characters in set2.
tr options set1 set2
With the -d (delete) or -s (squeeze) options, tr needs only set1, which will tell it what to delete or
squeeze. Squeeze removes repeated sets of characters, such as multiple white space characters.
$ cat tr.txt
this file is in all lower case.
$ cat tr.txt | tr a-z A-Z
THIS FILE IS IN ALL LOWER CASE.
$ cat tr.txt | tr -d s
thi file i in all lower cae.

Page 83

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 6. TEXT PROCESSING

6.4 sort
The sort command sorts, merges or compares all the lines from files or STDIN. By default, sort writes
the results to STDOUT. When given more than one file as input, the default operation is to sort each
file and then merge them into one. Some useful options include the following:
Option

Description

-n

Sort numerically

-r

Reverse sort order

-t separator

Use as field separator

-k NUMBER

Sort by field NUMBER

-o filename

Output to named file


Table 6.4: Options for the sort command

Here are some examples of sort. The final example stores the sorted data in a file instead of
displaying it on the screen.
$ cat data.txt
Carl:31
Ann:21
Donna:3
Bob:20
$ sort data.txt
Ann:21
Bob:20
Carl:31
Donna:3
$ sort -t: -k2 data.txt
Bob:20
Ann:21
Donna:3
Carl:31
$ sort -t: -k2 -n data.txt
Donna:3
Bob:20
Ann:21
Carl:31
$ sort -t: -k2 -odata.sort data.txt

Page 84

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

6.5 uniq
By default, the uniq command runs through a file or standard input, finds all duplicated lines and
removes the duplicates. In order for the uniq command to work properly, it must be fed sorted data.
Some useful options include:
Option

Description

-i

Ignore case

-d

Only print duplicated lines

-u

Only print unique lines

-c

Prefix lines with a count of occurences


Table 6.5: Options for the uniq command

If uniq is given a second filename on its command line, it will write the output to that file. Because
uniq requires sorted files, it is very common to run sort first and pipe the output to uniq, as in the
example below:
$ sort data.txt | uniq >nodups.txt

Page 85

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 6. TEXT PROCESSING

6.6 cut
The cut command cuts sections of text from files, or STDIN, and neatly pastes them together to
STDOUT. The cut command is most useful with files or input the contain information arranged
using tabs (default) or some other delimiting character. Some useful options include:
Option

Description

-d separator

Use as separator

-f range

Fields to cut
Table 6.6: Options for the cut command

When specifying a range, start from 1 at the beginning of the line. Specify multiple ranges by
separating them with a comma.
The /etc/passwd file contains several records related to user accounts. Here is an example using the
cut command to list only user names and UIDs from /etc/passwd.
$ cut -d: -f1,3 /etc/passwd
robc:501
user:502
The cut command is often used with other commands to create a complex text manipulation, as
shown in the examples below:
$ cut -d: -f7 /etc/passwd | sort | uniq -c | sort -rn
17 /bin/sh
11 /bin/false
2 /bin/bash
1 /bin/sync

Page 86

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

6.7 Lab Activity


Part 1. Copy the file /usr/share/dict/words to your directory. Find all lines in the file containing
the string life. Replace all instances of life with life and redirect to a second file. Verify that it
worked and that the original file is unchanged.
Part 2. Display each user and their login shell from /etc/passwd. Sort the result by username.
Part 3. Get a count of how many times each command exists in your .bash history file.
Part 4. Use grep and the file /usr/share/dict/words as a puzzle solver. Find all the results for the
following:
A.
B.
C.

Four letter words beginning with an f and ending with an h.


Nine letter words containing either cat or dog.
Words with all the vowels occurring once, in alphabetical order

How many results were there for each?

Page 87

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 6. TEXT PROCESSING

Page 88

c
2010
n + 1, Inc All Rights Reserved

Chapter 7

File Management Utilities


Objectives
Desribe the use of the disk usage commands
Understand how to compress and uncompress files and directories
Explain the difference between archiving and compressing

89

CHAPTER 7. FILE MANAGEMENT UTILITIES

7.1 Disk Usage


The df command displays how much space each file system is using on the disk, how much space
is available, and the mount points of the file system. Using the -i option shows inode usage instead
of free space. Every file on the system uses one inode and running out of either space or inodes can
cause serious problems to a system. The df command is very useful for monitoring both.
[user@localhost ]$ df -h
Filesystem
Size
/dev/sda2
16G
/dev/sda7
165G
/dev/sda5
19G
/dev/sda3
19G
/dev/sda1
99M
tmpfs
501M

Used Avail Use% Mounted on


4.3G
11G 30% /
188M 157G
1% /data
1.7G
17G 10% /var
3.0G
16G 17% /home
23M
71M 25% /boot
0 501M
0% /dev/shm

[user@localhost ]$ df -i
Filesystem
Inodes
IUsed
IFree IUse%
/dev/sda2
4100544 179253 3921291
5%
/dev/sda7
44597248
11 44597237
1%
/dev/sda5
5124480
2154 5122326
1%
/dev/sda3
5124480
25336 5099144
1%
/dev/sda1
26104
78
26026
1%
tmpfs
128209
1 128208
1%

Mounted on
/
/data
/var
/home
/boot
/dev/shm

The table below shows the most commonly used options for df.
Option

Description

-h

Print sizes in human readable format.

-i

Show inodes instead of block usage.

-T

Print file system type.


Table 7.1: Options for the df command

Page 90

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

7.2 Disk Usage within Directories


The du command shows the disk space used by files and directories. Without any options the du
command will recurse thru the file sytem starting with the current directory and output the sizes
of all files in the current directory or any subdirectories.
The summarize option (-s) can be used to show how much disk space the current or target directory
is using. The human readable option (-h) is available for du like with df.
The - -max-depth option can be used to limit the amount of recursion. This is commonly used when
trying to determine which subdirectory is using up disk space.
[root@localhost ]$ du -h --max-depth=1 /var
24K
/var/yp
384K
/var/run
8.0K
/var/local
6.8M
/var/www
1.3G
/var/lib
28K
/var/db
32K
/var/empty
20K
/var/gdm
2.0M
/var/spool
32K
/var/lock
16K
/var/lost+found
142M
/var/cache
137M
/var/log
1.6G
/var
[user@localhost ]$ du -hs
2.8G
.
The common options for du are shown in the following table.
Option

Description

-h

Print sizes in human readable format.

-s

Summarize by displaying total for each argument.

-S

Do not include size of subdirectories.

max-depth=N

Print totals for a subdirectory that are no more than N levels


below argument.
Table 7.2: Options for the du command

Page 91

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 7. FILE MANAGEMENT UTILITIES

7.3 Compression
There are a number of compression commands available in linux. Unix used the compress command,
which is available in some linux distributions. However, compress was not efficient and has been
surpassed by the other algorithms. The syntax of the compress command has stayed and is used by
most of the other commonly used compression commands.
Like compress, neither gzip nor bzip2 archives directories. They are used to compress an archive that
has already been created. The zip command has the ability to both archive and compress.

Page 92

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

7.3.1 Compressing files with gzip


The gzip command has a much higher compression ratios than compress. It is the most commonly
used compression commmand on linux and is widely used for distributing files.
With the gzip command, a filename or group of filenames is passed into the command, and each file
is compressed into a file ending in .gz. The original file is removed after the command is complete.
The -c option can be used to send the output to STDOUT and keep the original files.
[user@localhost ]$ ls
token2.txt token.txt
[user@localhost ]$ gzip token.txt
[user@localhost ]$ ls
token2.txt token.txt.gz
[user@localhost ]$ gzip -c token2.txt >token2.txt.gz
[user@localhost ]$ ls
token2.txt token2.txt.gz token.txt.gz
The compression ratio can be adjusted for the gzip command. The options from -1 to -9 can be used
for this adjustment. -1 gives the faster compression, while -9 gives the best compression ratio.
To uncompress a file compressed with gzip, use the gunzip command.
[robc@localhost stuff]$ gunzip token.txt.gz
[user@localhost ]$ ls
token2.txt token2.txt.gzip token.txt

Option

Description

-r

Recurse directories.

-v

Verbose mode.

-1 to -9

Regulate the speed of compression. -1 is fastest, -9 is best.


Table 7.3: Options for the gzip command

Page 93

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 7. FILE MANAGEMENT UTILITIES

7.3.2 Compressing files with bzip2


The bzip2 command is the newest compression program for Linux to gain popularity. It often
achieves higher compression ratios than gzip. The syntax of bzip2 is intentionally similar to that of
gzip and is beginning to replace gzip for much software distribution.
As seen below, usage is very similar to gzip.
[user@localhost
token.txt
[user@localhost
[user@localhost
token.txt.bz2
[robc@localhost
[user@localhost
token.txt

]$ ls
]$ bzip2 token.txt
]$ ls
stuff]$ bunzip2 token.txt.bz2
]$ ls

The -1 to -9 options work differently in bzip2. They are used to set block size for the compression
algorithm and dont control speed or compression ratios. Therefore, they are rarely used. Below is
a table of the commonly used options.
Option

Description

-r

Recurse directories.

-v

Verbose mode.
Table 7.4: Options for the bzip2 command

Page 94

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

7.3.3 Compressing files with zip


The zip command is compatible with PKZIP, so it is commonly used when sending compressed files
between MS Windows and Linux. The compression ratio for zip is similar to that provided by gzip.
Unlike gzip or bzip2, zip archives in addition to compressing files. The zip and unzip commands are
used to create or extract ZIP files. When zip is used with directories or muliple files, one ZIP archive
is created.
The syntax for the zip command is different than the other compression programs, in that the name
of the ZIP archive is the first parameter.
[user@localhost ]$ ls
token.txt
[user@localhost ]$ zip token.txt.zip token.txt
updating: token.txt (deflated 96%)
[user@localhost ]$ ls
token.txt token.txt.zip
[user@localhost ]$ unzip token.txt.zip
Archive: token.txt.zip
replace token.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: token.txt
The table below shows the options commonly used with the zip command.
Option

Description

-r

Recurse directories.

-v

Verbose mode.

-0 to -9

Regulate the speed of compression. -0 is no compression, -1 is


fastest, -9 is best.
Table 7.5: Options for the zip command

Page 95

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 7. FILE MANAGEMENT UTILITIES

7.4 Archiving Directories


The tar command stands for Tape ARchive. It was originally used for archiving files to tape drives.
However, its power has made it a commonly used tool for other purposes. However, many backup
systems still use the tar command as part of their process.
The tar command is used to archive a directory structure into a single file. This file can then be
compressed, if so desired. The two primary uses are for backing up files and for distributing files.
The tar command is used for both creating and extracting the archives.
The example below shows the syntax for creating an archive using compression at the same time
as the archive is created. This can be done as two steps, but is so commonly paired that the options
exist within tar.
[root@localhost ]# tar -czf etc.tar.gz /etc
tar: Removing leading / from member names
[root@localhost ]# ls
etc.tar.gz
To extract the archive, use the extract (-x) option instead of the create (-c) option.
[root@other /]# tar -xzf etc.tar.gz
In the example above, the etc.tar.gz file was moved to a second machine and then extracted. The
extraction will write its files over those in the /etc directory on the new machine.
The tar command only extracts into the current directory, so it is important to make sure your
current directory is where you want the extract to appear before running it.
The table below contains the commonly used tar options.
Option

Description

-c

Create archive.

-x

Extract archive.

-t

List contents of archive.

-v

Verbose mode.

-f

Archive to file.

-z

gzip archive.

-j

bzip2 archive.
Table 7.6: Options for the tar command

Page 96

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

7.5 Lab Activity


Part 1. Do the following commands related to making a tar archive:
$
$
$
$

mkdir testtar
cd testtar
touch file1 file2 file3
tar -cvf /test1.tar *

$ cd ..
$ tar -cvf test2.tar testtar/
$ tar -tf test1.tar
$ tar -tf test2.tar
$
$
$
$

mkdir testtar2
mv test1.tar testtar2
mv test2.tar testtar2
cd testtar2

$
$
$
$

tar -xvf test1.tar


ls -l
tar -xvf test2.tar
ls -l

What is the difference between the two methods?


Part 2. As root, make an uncompressed archive of /etc directory. How large is this archive?
Part 3. Make 3 copies of the archive from Part 1. Compress each one using a different compression
command. Compare the sizes of the three files after compression. Which command did the best
job?
Part 4. How much disk space is your home directory using? Try this with both the -h and -H
options. What is the difference in the results?
Part 5. What subdirectory under var is using the most disk space?

Page 97

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 7. FILE MANAGEMENT UTILITIES

Page 98

c
2010
n + 1, Inc All Rights Reserved

Chapter 8

Processes and Jobs


Objectives
Understand what Linux processes are
Use ps and top to display process status
Run and manage jobs in foreground and background
Use nice and renice to manage process priorities

99

CHAPTER 8. PROCESSES AND JOBS

8.1 Processes
A process is a running instance of a program. The terms process and program are often used
interchangeably but it is inaccurate. A program is an executable file. It is passive while a process
is active. Multiple instances of a single program can be run simultaneously, each as a different
process. Also, some programs may spawn several processes upon execution.
Every process has a set of attributes associated with it:
System Resources Threads, process ID, priority, file handles and environmental variables
Virtual Memory RAM and swap space
Security Attributes What resources the process can access
Processor State Physical memory addresses and contents of registers

Page 100

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

8.1.1 States
Just because a program has been launched does not mean that it is actively running. Processes can
be in several states.
running Currently being allocated CPU slices
stopped Still loaded in memory, but not running
sleeping Waiting for some event, such as user input
zombie Process not properly ended by parent process
It is important to understand the difference between sleeping processes and stopped processes.
When a process is stopped, it is no longer receiving any CPU cycles.

Page 101

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 8. PROCESSES AND JOBS

8.1.2 Viewing Processes


There are two Linux utilities that are primarily used to monitor system status as well as view the
most active processes. The ps command lists all running processes in a snapshot format, meaning
that whatever was happening when command executed is displayed. The top command displays
and updates information about processes in real time.
To view which processes are running, use ps. A list of all processes the user is running, as well as
processes on the current shell and other jobs, is generated. Information that will be displayed includes the process identification number (PID), controlling terminal (TTY), processing time (TIME),
and command name (CMD).
Due to the popularity of the ps command, it has many options, which have been carried over, with
differing syntax, from both UNIX and BSD systems. The two most common uses are ps -ef and ps
aux. They both show all processes with varying outputs. It is often common to combine ps with
the grep command to restrict the search to the processes you are searching for.
[user@localhost
user
6176
user
6660
user
6928

Page 102

]$ ps -ef | grep bash


6173 0 16:31 pts/0
00:00:00 bash
6173 0 16:51 pts/1
00:00:00 bash
6660 0 17:15 pts/1
00:00:00 grep bash

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS
The top command provides a continuous display of system staus with the most active processes
listed and updated every 5 seconds. Three different classes of information is displayed: system
data, memory data, and process data.
[user@localhost ]$ top
top - 17:18:04 up 48 min, 3 users, load average: 0.05, 0.04, 0.04
Tasks: 115 total,
1 running, 114 sleeping,
0 stopped,
0 zombie
Cpu(s): 0.8%us, 0.3%sy, 0.0%ni, 98.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem:
3786412k total,
645428k used, 3140984k free,
19596k buffers
Swap: 8391592k total,
0k used, 8391592k free,
370264k cached
PID
5617
5916
5921
1
2
3
4
5
6
7
8
9
10
11
44
45
48

USER
root
user
user
root
root
root
root
root
root
root
root
root
root
root
root
root
root

PR
20
20
20
20
15
RT
15
RT
RT
15
RT
15
15
15
15
15
15

NI
0
0
0
0
-5
-5
-5
-5
-5
-5
-5
-5
-5
-5
-5
-5
-5

VIRT RES SHR S %CPU %MEM


115m 29m 6436 S
2 0.8
130m 2972 1744 S
1 0.1
128m 13m 7916 S
1 0.4
4020 880 592 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0
0
0
0 S
0 0.0

TIME+
0:44.06
0:00.88
0:02.06
0:01.00
0:00.00
0:00.00
0:00.00
0:00.00
0:00.00
0:00.00
0:00.00
0:00.00
0:00.04
0:00.02
0:00.00
0:00.00
0:00.00

COMMAND
Xorg
gnome-screensav
metacity
init
kthreadd
migration/0
ksoftirqd/0
watchdog/0
migration/1
ksoftirqd/1
watchdog/1
events/0
events/1
khelper
kblockd/0
kblockd/1
kacpid

As long as top continues to run, the process information continues to update, To exit, type q.

Page 103

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 8. PROCESSES AND JOBS

8.1.3 Signals
Signals are used to communicate events to a process. Signals are represented by a number and a
symbolic name that corresponds to the intended use of the signal. Some signals depend on the
archetecture and cannot be found on all platforms.
Associated with each signal is a default action, represented by its name. For example, SIGINT is an
interrupt process sent from the keyboard. When a user hits Ctrl-C, the SIGINT signal is sent to the
process running in the foreground.
Many programs contain signal handlers to catch and deal with signals that are sent to the process.
With a few exceptions, the process can handle the signal as it sees fit. Thus, while SIGINT tells
a process to quit, it might choose to continue or use it to start a shutdown procedure, instead of
immediately dying.
Processes cannot install signal handlers for the SIGSTOP and SIGKILL signals. To provide the
desired behavior, these two signals are caught and enforced by the kernal.
The following table contains the most common signals:
Signal

Value

Action

Description

SIGHUP

Terminate

Hangup on terminal

SIGINT

Terminate

Interrupt from keyboard

SIGQUIT

Core Dump

Quit from keyboard

SIGFPE

Core Dump

Floating point exception

SIGKILL

Terminate

Kill signal

SIGTERM

15

Terminate

Termination signal

SIGCONT

18

Continue

Continue if stopped

SIGSTOP

19

Stop

Stop process

Table 8.1: Common Signals

Page 104

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

8.1.4 Signal Commands


There are a number of commands to send signals to processes on the system. The most common is
through the kill command. Because of its name, it is often assumed that this command can only be
used to send the kill signal. However, any valid signal can be with the kill command by specifying
either the signal number of symbolic name of the signal.
By default the kill command sends SIGTERM. In most cases, this will cause the process to terminate
nicely. However, it does not always work, and the SIGKILL option must be used instead.
[user@localhost ]$ kill 1010
[user@localhost ]$ kill -9 1010

Page 105

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 8. PROCESSES AND JOBS

8.2 Job Control


From the command line, the task of interacting with many processes poses a difficult problem.
Normally, when you lauch a program from the shell, it becomes the new foreground process and
you have no way of launching another process. The job control functions allow you to temporary
stop the process and return control to the shell. You can then use various job control commands to
start the process running in the background or foreground, or kill the process. You can also choose
to the leave the process stopped and move other processes to the foreground.
It is also possible to start a job directly in the background. To do this, put an ampersand after the
command. This will start the process in the background and return the shell to the prompt.
[user@localhost ]$ gnome-calculator &
[user@localhost ]$

Page 106

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

8.2.1 Managing Jobs


Typing Ctrl-Z while a process is running causes the foreground process to be stopped and returns
you to a shell prompt. You may then manipulate the state of the job, using the bg command to
continue it in the background or the fg command to continue it in the foreground.
The command jobs will list all the jobs present in the current shell. Each will have a job number
associated with it. This number can be used with the fg and bg command to manipulate the job.
The job number can also be used with the kill command, instead of the PID number, however it
must be appended with
[user@localhost ]$
Ctrl-Z
[1]+ Stopped
[user@localhost ]$
[1]+ Stopped
[user@localhost ]$
[1]+ xclock &
[user@localhost ]$
[1]+ Running
[user@localhost ]$
[user@localhost ]$
[1]+ Terminated

Page 107

xclock
xclock
jobs
xclock
bg 1
jobs
xclock &
kill %1
jobs
xclock

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 8. PROCESSES AND JOBS

8.3 Lab Activity


Part 1. Start a few processes (vi, less, man, top, etc.) that continue to run and then suspend them.
Start a few more jobs (xlogo, gnome-calculator, xclock, etc.) in the background. Run the jobs
command. Compare the states of the different jobs.
Part 2. Bring a suspended job to the foreground. Suspend it. Restart it in the background.
Part 3. Use ps to see what processes you have running.
Part 4. Kill the jobs you started in Part 1. Kill them by both their PID and job number. Use ps to
verify they are not running.

Page 108

c
2010
n + 1, Inc All Rights Reserved

Chapter 9

Package Management
Objectives
Describe the use of rpm commands for package management
Explain the different package options with RPM
Be able to use yum to manage packages

109

CHAPTER 9. PACKAGE MANAGEMENT

9.1 RPM
Installing from source presents many challenges for the administrator:
Build dependencies must be resolved manually
Dependencies are not tracked after installation
Files placed on the file system during installation are not tracked
Removing a package involves deleting each file individually
Versioning tasks, such as upgrading and rolling back, can be difficult
Modern package management systems attempt to solve these problems. The most widely used
package management system is RPM (RPM Package Manager). It is used by many distributions.
RPM keeps a database of all the packages installed on a system and all of the files placed on your
system by those packages. The rpm command is used to query the package database, install, and
delete packages.

Page 110

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

9.1.1 Binary Packages


The following table provides basic commands for the installation, upgrade, and removal of software
packages.
Option

Description

-i

Install new package

-U

Upgrade or install package

-F

Freshen (upgrade) package only

-v

Verbose mode

-e

Uninstall package

- -force

Force upgrade

- -nodeps

Ignore dependencies
Table 9.1: Options for the rpm command

The syntax for the rpm command is in the form:


# rpm -options packagename-version-release.rpm
The -U option is the most commonly used when installing a package, it will install if the package is
new and upgrade if the package is newer than the currently installed package. The - -force option
allows an installation of a older package over the top of a new one, it is generally recommended
against. The - -nodeps option will install a package without checking dependencies. This is almost
always a bad idea.
While RPM works well, it requires you to get packages for any uninstalled dependencies before
you can continue with your install. These often have dependencies too, which can lead to a chain
of tracking down the proper rpm packages. Fortunately, this problem has been fixed with the yum
command we will see later in this chapter.

Page 111

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 9. PACKAGE MANAGEMENT

9.1.2 Source Packages


There are two methods for installing an RPM package from a source package. The first is using the
rebuild option, which compiles and installs the software. The second method is to use the recompile
option, which does the same as the rebuild option but also builds a binary RPM package which can
be used for distribution.
# rpmbuild --rebuild package-version-release.src.rpm
# rpmbuild --recompile package-version-release.src.rpm

Page 112

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

9.1.3 Query and Verify


One basic premise of managing a system is to know what software and version is on the system.
The rpm command has a number of query methods that allows the administrator to view this
information. The rpm command can also be used to determine relationships between files and
packages.
One of the issues with package management is dependencies. Many packages require other packages, typically libraries, to be installed in order to work. These other packages are dependencies.
RPM proviced a querying mechanism for determing these dependencies before a pakcage is installed.
The table below lists the common command line options used to query the package database:
Option

Description

-qa

List all packages installed on the system

-qi packagename

Information about the named package

-ql packagename

List all files installed by named package

-qR

List package dependencies


Table 9.2: Options for the rpm query command

Every time a package is installed, upgraded, or deleted, RPM logs the changes in its database. The
RPM verify command allows an administrator to verify that their system configuration matches
the database. It verifies that every file installed by a package continues to exist on the system. It
also checks the attributes of each file to make sure the files are the intended ones with the proper
permissions.
The common command line options for RPM verify are listed in the following table:
Option

Description

-V packagename

Verify all files provided by the named package

-Va

Verify all packages installed

-Vf file

Verify all the files provided by the package that also provided
the named file
Table 9.3: Options for the rpm verify command

Page 113

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 9. PACKAGE MANAGEMENT

9.2 Yum
The yum command is used for installing RPM packages from repositories. It will determine dependencies and install those packages in addition to the package being installed. It is possible to
configure yum to work with multiple repositories and yum will use the most up-to-date versions
when different repositories contain the same packages.
The table below shows commonly used yum commands:
Option

Description

install

Install package

update

Update package or entire system

remove

Remove package

provides

Finds what package provides listed file

search

Searchs packages for given string


Table 9.4: Options for the yum command

The behavior of yum is determined by its configuration file, at /etc/yum.conf. A default yum.conf
file is shown below:
[main]
cachedir=/var/cache/yum
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
distroverpkg=redhat-release
tolerant=1
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
# Note: yum-RHN-plugin doesnt honor this.
metadata_expire=1h
installonly_limit = 5
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

Page 114

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS
Individual repositories are configured in files stored in /etc/yum.repos.d/, one file for each repository.
An example repository configuration looks like:
[adobe-linux-i386]
name=Adobe Systems Incorporated
baseurl=http://linuxdownload.adobe.com/linux/i386/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-adobe-linux

Page 115

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 9. PACKAGE MANAGEMENT

9.3 Lab Activity


Part 1. List all packages installed on your system. Sort them alphabetically and view the list using
less.
Part 2. Pick a package from the list above and verify it is installed properly.
Part 3. Use yum to update your system. Note: this may take a while.

Page 116

c
2010
n + 1, Inc All Rights Reserved

Chapter 10

Secure Shell
Objectives
Use SSH to access remote machines
Set up public/private keys
Use scp to transfer files securely

117

CHAPTER 10. SECURE SHELL

10.1 Secure Shell


In the early days of UNIX, there was an effort towards creating remote access to systems and
sharing of information but security was not considered. As the internet grew, security became a
more important issue. The secure shell (SSH) was created to replace a series of tool that were not as
fundamentally sound for security.
The first of these commands was telnet. The big problem with telnet is that it passes information,
such as passwords, in plaintext over the network. Thus, anyone on the network with sniffing
software could access passwords.
Later, there were a series of commands called the rprotocols. These commands, rlogin, rsh, and
rcp, for example, were not much better than telnet. They still transmitted over plaintext and used
hostname verification for logins instead of passwords. If the user/hostname pair was authorized to
access the system, they were let in. Unfortunatly, hostname is easy to spoof.
Finally, SSH was created. All communication is done through encryped channels. The secure
shell also supports tunneling of other procols over the SSH channel. Protocols such as HTTP, POP,
and IMAP can be accessed securely via SSH. SSH also supports RSA/DSA keys to allow for logins
without passwords.

Page 118

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

10.1.1 Configuration
SSH works under a standard client/server model. The SSH client connects to a server running the
sshd process. Once the client and server establish a connection and setup up an encrypted channel,
the user can then authenticate.
On both sides, there are a few files that can be configured. By default, most of these never need to
be changed. On the server, the following files handle configuration:
/etc/ssh/sshd_config

/etc/ssh/ssh_host_key

/etc/ssh/ssh_host_key.pub /etc/ssh/sshrc

The main configuration file. It lists the supported


features.
The servers private key. Generated the first time
sshd is started.
The servers public key. Also generated the first
time sshd is started.
Commands that execute when a client connects.

On the client side there are the following configuration files:


/etc/ssh/ssh_config

/.ssh/config

/.ssh/id_rsa

/.ssh/id_rsa.pub

The main configuration file for the client. It lists


the features supported and that will be requested.
Same as above, only for the individual user. Can
override system settings.
Private key for the client. Can also be in a file
named id_dsa if using that protocol.
Public key for the client. Like above, the file
could be id_dsa.pub.

There is another important file located in the users .ssh directory. It is the known hosts file. This
file contains the public key of the hosts that have been connected to. If you try to connect to a
host whose key doesnt match, ssh will warn you and not allow the connection. You then have
to manually remove the key from this file in order to connect. SSH also notifies you whenever
you are connecting to a system that you havent connected to before, one without an entry in the
known hosts file.

Page 119

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 10. SECURE SHELL

10.1.2 Using SSH


There are three different syntaxes for accessing a remote system via SSH. The first, and simplest is:
$ ssh server_name
This will attempt to connect to the server using your username on the client machine. If you wish
to connect with a different username, use the following syntax:
$ ssh user@server_name
Finally, SSH also allows for the remote execution of commands. SSH can connect, run a single
command, send back the output, then disconnect. For example, to restart a mail server:
$ ssh root@mail.nplus1.net /etc/init.d/postfix restart
Shutting down postfix:
Starting postfix:

Page 120

[
[

OK
OK

]
]

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

10.1.3 File Transfer


There are two tools that provide secure means of transferring files over SSH. The first is sftp which
replaces ftp (add to list of insecure commands).
The second tool is scp. Secure copy provides a non-interactive means of transferring files to and
from remote systems. The file uses the syntax of the emphcp command, only with server user and
server names in addition to directories. For example:
$ scp doi.txt robc@mail.nplus1.net:/home/robc
robc@mail.nplus1.nets password:
doi.txt

Page 121

100% 8049

7.9KB/s

00:00

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 10. SECURE SHELL

10.2 SSH Key Management


SSH allows for password-less logins for public/private keys. The command ssh-keygen is used to
generate a pair of keys. The private key is put in the .ssh directory and the public key is transferred
to the remote machine and placed in the users .ssh directory. Within that directory, there is a file
called authorized keys. The public key is appended to that file.
The following is an example of generating and setting up the keys.
$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/robc/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/robc/.ssh/id_dsa.
Your public key has been saved in /home/robc/.ssh/id_dsa.pub.
The key fingerprint is:
b8:83:b3:75:62:92:dc:7b:a4:e6:85:d7:e5:80:6c:b4 robc@localhost.localdomain
$ scp .ssh/id_dsa.pub robc@mail.nplus1.net:/home/robc/.ssh/
robc@mail.nplus1.nets password:
id_dsa.pub
100% 616
$
$
$
$

0.6KB/s

00:00

ssh mail.nplus1.net
cd .ssh
cat robc\@otho-id_dsa.pub >> authorized_keys
chmod 600 authorized_keys

In order to work, the .ssh directory must have file permissions of 700 and the authorized keys file
must have permissions of 600. SSH will not use them if anyone other than the owner has read
permission to the directory or file.

Page 122

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

10.3 Lab Activity


Part 1. Create an account for one of your classmates. Give them the password. Login via ssh to
the account given to you. Exit. Copy a file to your home directory on the remote machine. Login
again. Make sure the file is there.
Part 2. Create a public/private key pair. Do not create a passphrase. Transfer the public key to the
remote machine from Part 1. Set it up so that key exchange will work. Try to ssh to the remote
machine. Can you access it without a password?

Page 123

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 10. SECURE SHELL

Page 124

c
2010
n + 1, Inc All Rights Reserved

Chapter 11

Mounting Filesystems
Objectives
Understand mounting filesystems
Be able to add entries to /etc/fstab
Learn to mount and unmount NFS and SMB shares

125

CHAPTER 11. MOUNTING FILESYSTEMS

11.1 Filesystems under Linux


Linux is a single-rooted filesystem, all files descend from a root located at /. If an additional
filesystem is going to be accessed, it must be attached to the root filesystem at some point. The
process of attaching an additional filesystem is called mounting the filesystem. The place at
which the filesystem is attached is called the mount point. Both local and remote filesystems can
be mounted.
It is very common for Linux systems to have many filesystems. At a minimum, almost all systems
have a /, /boot, and swap partition. Other common filesystems include /home and /var.

Page 126

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

11.1.1 Mounting Filesystems


A filesystem is mounted with the mount command. Its syntax is as follows:
mount -t type options device dir
The -t option is used to declare the type of filesystem that is being mounted. The device is the
device file associated with the filesystem. Finally, the dir is the location in the directory structure
where the filesystem will be mounted. For example:
$ mount -t ext3 /dev/hda3 /var
The previous command mounts the hda3 partion as /var in the filesystem. If the mount command
is run without any of the expected options, it looks in the file /etc/fstab for a line that matches. We
will discuss the fstab file in more detail later, but as an example:
$ mount /mnt/media
The mount command will look in the /etc/fstab file for a line with a directory matching /mnt/media.
If it finds one, it will mount it using the options in the fstab file.
Without any options, the mount command lists all currently mounted filesystems.
To unmount a filesystem, the umount command is used with either the device or the directory.
$ umount /dev/hda3
$ umount /var

Page 127

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 11. MOUNTING FILESYSTEMS

11.1.2 Filesystem Table


The Filesystem Table is stored in the file /etc/fstab. When a system first boots, it will mount the
filesystems listed in this file. Each line of the file defines a different filesystem and their are six fields
on each line:
What to mount
Where to mount it
Filesystem type
Options
Dump command
Order to mount at boot time
The first three are the device, directory and type from the mount command. The fourth is additional
options, the fifth determines if the filesystems should be backed up by the dump command, and
the final is the order the filesystems should be mounted at boot. If the sixth field is zero, it isnt
mounted at boot.
The following command will mount everything in the /etc/fstab file:
$ mount -a

Page 128

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

11.2 NFS
The Network File System is the native file sharing method for UNIX systems. An NFS server
exports directories. This makes them available for sharing across the network. The client
machine then mounts the NFS exports. Users then use the shares as if they were local. From
the users perspective there is no difference between a remote NFS filesystem and a local filesystem.
The standard mount command is used to mount NFS shares. The type of filesystem is nfs and the
device points to a server and location on that server. For example, if the server thorin was mounting
a website, the mount command would look like:
$ mount -t nfs thorin:/export/website /mnt/web
If a share is continuously mounted, its description can be placed in /etc/fstab so that it mounts on
boot.
To determine what exports are available from an NFS server, use the showmount command as
follows:
showmount -e server_name

Page 129

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 11. MOUNTING FILESYSTEMS

11.3 Samba
SMB is the native file sharing protocol for Microsoft Windows. It is also called CIFS. It allows
servers to share directories, printers, users and other information. On the server side, samba allows
the Linux server to be a Windows file server on the network. On the client side, there are two tools
to access remote SMB shares.
First, there is the smbclient command. This provides an ftp-like interface to the SMB share.
smbclient //server/share -U user
Notice that the slashes are backwards from the normal Windows syntax. The user must provide a
password to access the share and then uses ftp style commands to access files.
The second method is to mount the share using the mount command. This requires the username
option as shown below.
mount -t smb -o username=user //server/share dir

Page 130

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

11.4 Lab Activity


Part 1. Perform the following commands to attach to a NFS share. The server name will be provided
by the instructor.
$ mkdir /mnt/server
$ mount -t nfs server_name:/export/tmp /mnt/server
$ mount

#verify the filesystem mounted

$ ls -la /mnt/server
$ umount /mnt/server
Part 2. Add a line to /etc/fstab for the NFS share mounted in Part 1. Mount it again without using
the full mount command. Make sure it mounts properly. Then unmount it.
Part 3. Repeat Parts 1 and 2 only for a SMB share the the instructor gives you the name of.

Page 131

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 11. MOUNTING FILESYSTEMS

Page 132

c
2010
n + 1, Inc All Rights Reserved

Chapter 12

Shell Scripting
Objectives
Understand the special variables used in shell scriping
Use the conditional constructs if and case
Use the looping constructs for and while

133

CHAPTER 12. SHELL SCRIPTING

12.1 Shell Script Basics


A shell script is a file containing a series of linux and shell commands. When the shell script is
executed, the commands in the script get executed, in order, starting with the first line in the file, as
if the commands had been typed in on the keyboard.
Shell scripts are used to automate common, repetitive sequences of commands. Shell scripts fit in
with Unix principles in that simple commands can be combined to make a more complex command.
Many system administrator tasks are accomplished with shell scripts.
To run a shell script, the file must be executable. When the script is run, a new shell is created that
the shell script runs in. By default, this shell will be the same as the current shell. However, the first
line of a shell script can be used to define the shell in which to run the script. This allows the script
to work, regardless of the shell of the user. The example below defines that the scripts is running in
the Bash shell.
#!/bin/bash

Page 134

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

12.1.1 Exit Status and Test Command


When a linux command finishes, it returns and exit status indicating whether it succeeded or failed.
This exit status is stored in a variable, $?. If the variable is set to 0, then the command succeeded. If
the variable has any other value, the command failed. The value depends on why it failed and this
value varies with each command.
$ ls

#this command will succeed

$ echo $?
0
$ ls no.dir
2
The return value of commands is used by a variety of flow control elements, covered in the next
section. One of the primary commands used by these elements is the test command. The test
command has two syntaxes:
test expression
[ expression ]
In the latter syntax, which is the most common within shell scripts, the space after the starting
brace and before the closing brace are manditory. There are a large number of options for the test
command, the most common are shown in the table below.
Option

Condition tested

-e filename

File exists

-d filename

File exists and is a directory

-f filename

File exists and is a regular file

-x filename

File exists and is executable

x=y

String x is equal to string y

x -eq y

Integer x is equal to integer y

x -ne y

Integer x is not equal to integer y

x -gt y

Integer x is greater than integer y

x -lt y

Integer x is less than integer y


Table 12.1: Options for the test command

Page 135

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 12. SHELL SCRIPTING


Some examples of the test command are shown below:
$ test -d file.txt
$ echo $?
1
$ test -f file.txt
$ echo $?
0
$ [ -f file.txt ]
$ echo $?
0

Page 136

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

12.1.2 Variables and the read Command


Variables are set with an equal sign. Spaces cannot surround the equal sign. To retrieve the value
of a variable it is dereferenced by preceeding it with a dollar sign.
The read command is used to prompt for input and store it in a variable. When the read command
is executed, the script pauses and waits for the user to input from the keyboard. Once the enter key
is pressed, whatever is typed is stored in the variable that is part of the read statement.
#!/bin/bash
myname=Rob
echo "Enter your name:"
read name
echo "$name, my name is $myname."

Page 137

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 12. SHELL SCRIPTING

12.1.3 Special Variables


When a shell script is executed, any parameters provided on the command line are stored in special
variables that are accessible inside the script. These variables are named 1, 2, 3, etc. They can be
accessed via $1, $2, etc. If there are more the nine parameters, brackets are needed around the
number, such as ${10}.
#!/bin/bash
#file echo3
echo $1 $2 $3
$ ./echo3 a b c d
a b c
The variable # stores the number of arguments pass in on the command line.
#!/bin/bash
#file echo3
echo $1 $2 $3
echo $#
$ ./echo3 a b c d
a b c
4
The shift command is used to access the parameters passed into the script, one at a time. Each time
the shift command is executed the parameters are shifted one to the left. The $1 variable is lost.
The $2 variable becomes the $1 variable. The $3 variable becomes the $2 variable. And so on. Also,
each time shift is executed, the # variable is decremented by 1.
#!/bin/bash
#file echo3
echo $1 $2 $3
echo $#
shift
echo $1 $2 $3
echo $#
$ ./echo3 a b c d
a b c
4
b c d
3

Page 138

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS
The variable dereferenced with $* is a list that expands to all of the command line parameters. It is
equivalent to $1 $2 $3 $4 etc.
#!/bin/bash
#file echo3
echo $*
echo $1 $2 $3
echo $#
shift
echo $1 $2 $3
echo $#

$
a
a
4
b
3

./echo3 a b c d
b c d
b c
c d

Page 139

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 12. SHELL SCRIPTING

12.2 if-then-ilif-else-fi
The most basic of the flow control constructs is the if-then construct. If a command returns a value
of 0, then a block of code is executed. The syntax of the command is shown below, with a simple
example.
#!/bin/bash
if [ -d backup ]
then
echo "Moving file.txt to backup directory."
mv file.txt backup
fi
If backup is a directory, then the echo and the mv commands are executed, otherwise they are not.
The indenting is for ease of reading, it has no other meaning.
Often, if the command fails, a separate block of commands need to be run, with the else part of the
construct, a block of code to execute if the command fails can be defined.
#!/bin/bash
if [ -d backup ]
then
echo "Moving file.txt to backup directory."
mv file.txt backup
else
echo "backup is not a directory."
fi
If the first if fails, it is sometimes needed to test another command. The elif construct can be used,
once or multiple times, to test these other commands.
#!/bin/bash
if [ -d backup ]
then
echo "Moving file.txt to backup directory."
mv file.txt backup
elif [ -f backup ]
then
echo "backup is a file, dont want to overwrite it."
elif [ -x backup ]
then
echo "backup is executable, view its contents"
cat backup
else
echo "backup is not a directory."
fi
Page 140

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

12.3 The for Loop


Another very common flow control construct is the for loop. The for is followed by a variable name
and a list. Each element of the list is assigned to the variable and then the do block of code is
executed for each element of the list. When the list is exhausted, the for loop is done.
#!/bin/bash
for pet in dog cat fish
do
echo "I own a $pet."
echo "How about you?"
done
The following example prints out command line arguments passed into the shell script.
#!/bin/bash
for p in $*
do
echo $p
done

Page 141

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 12. SHELL SCRIPTING

12.4 The while Loop


The while loop performs actions within its do-done block while a specified condition remains true.
The conditional command is executed, and if true, the block is executed. The command is then
executed again, and the block executed, over and over, until the conditional command is false. It is
important to make sure the conditional will eventually be false or an infinite loop is created.
The break command causes the loop to execute regardless of the command condition.
#!/bin/bash
if [ $# -lt 1 ]
then
echo "This script requires at least one parameter."
else
while [ $# -gt 0 ]
do
echo $1
shift
done
fi

Page 142

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS

12.5 The case Construct


The case construct executes a block of command based on matching a pattern. When the variable
in the case statement matches one of the patterns, the block following the ) is executed. The block
ends with ;;. If no pattern matches and the * case exists, then its block is executed. If there are no
matches and no * case exists, then nothing is executed.
#!/bin/bash
case $1 in
hi)
hello)
bye)
*)
esac

Page 143

echo
echo
echo
echo

"Hi there" ;;
"Hello to you too" ;;
"bye" ;;
"I dont have a response to that" ;;

c
2010
n + 1, Inc All Rights Reserved

CHAPTER 12. SHELL SCRIPTING

12.6 Lab Activity


Part 1. Using an editor, write a script that displays the following: The date, the path to the current
directory, and the users logged into the system.
Part 2. Write a script that uses a for loop to print out the last names of five Presidents. Make Lincoln
one of the Presidents.
Part 3. Modify the script from Part 2 so that inside the loop, the current name is checked and if it is
Lincoln, print out My first name is Abe after his name.
Part 4. Write a script that prompts for first name, then prompts for last name. Print out your name
in last, first format.
Part 5. Create a directory in your home directory called trash. Write a script that accepts one
argument that is a file name, copies that file to the trash directory and then removes the file.
Part 6. Write a script that copies one file to a new name. If a file exists with the new name, the script
should prompt if the user wants to overwrite it.

Page 144

c
2010
n + 1, Inc All Rights Reserved

Appendices

145

LINUX FOUNDATIONS

GNU Free Documentation License


Version 1.3, 3 November 2008
c 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.
Copyright

<http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies of this license document, but
changing it is not allowed.

Preamble
The purpose of this License is to make a manual, textbook, or other functional and useful document
free in the sense of freedom: to assure everyone the effective freedom to copy and redistribute
it, with or without modifying it, either commercially or noncommercially. Secondarily, this License
preserves for the author and publisher a way to get credit for their work, while not being considered
responsible for modifications made by others.
This License is a kind of copyleft, which means that derivative works of the document must
themselves be free in the same sense. It complements the GNU General Public License, which is a
copyleft license designed for free software.
We have designed this License in order to use it for manuals for free software, because free software
needs free documentation: a free program should come with manuals providing the same freedoms
that the software does. But this License is not limited to software manuals; it can be used for
any textual work, regardless of subject matter or whether it is published as a printed book. We
recommend this License principally for works whose purpose is instruction or reference.

1. APPLICABILITY AND DEFINITIONS


This License applies to any manual or other work, in any medium, that contains a notice placed by
the copyright holder saying it can be distributed under the terms of this License. Such a notice grants
a world-wide, royalty-free license, unlimited in duration, to use that work under the conditions
stated herein. The Document, below, refers to any such manual or work. Any member of the
public is a licensee, and is addressed as you. You accept the license if you copy, modify or
distribute the work in a way requiring permission under copyright law.
A Modified Version of the Document means any work containing the Document or a portion of
it, either copied verbatim, or with modifications and/or translated into another language.
A Secondary Section is a named appendix or a front-matter section of the Document that deals
exclusively with the relationship of the publishers or authors of the Document to the Documents
overall subject (or to related matters) and contains nothing that could fall directly within that overall
Page 147

c
2010
n + 1, Inc All Rights Reserved

APPENDIX
subject. (Thus, if the Document is in part a textbook of mathematics, a Secondary Section may not
explain any mathematics.) The relationship could be a matter of historical connection with the
subject or with related matters, or of legal, commercial, philosophical, ethical or political position
regarding them.
The Invariant Sections are certain Secondary Sections whose titles are designated, as being those
of Invariant Sections, in the notice that says that the Document is released under this License. If
a section does not fit the above definition of Secondary then it is not allowed to be designated as
Invariant. The Document may contain zero Invariant Sections. If the Document does not identify
any Invariant Sections then there are none.
The Cover Texts are certain short passages of text that are listed, as Front-Cover Texts or BackCover Texts, in the notice that says that the Document is released under this License. A Front-Cover
Text may be at most 5 words, and a Back-Cover Text may be at most 25 words.
A Transparent copy of the Document means a machine-readable copy, represented in a format
whose specification is available to the general public, that is suitable for revising the document
straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text
formatters or for automatic translation to a variety of formats suitable for input to text formatters.
A copy made in an otherwise Transparent file format whose markup, or absence of markup, has
been arranged to thwart or discourage subsequent modification by readers is not Transparent. An
image format is not Transparent if used for any substantial amount of text. A copy that is not
Transparent is called Opaque.
Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo
input format, LaTeX input format, SGML or XML using a publicly available DTD, and standardconforming simple HTML, PostScript or PDF designed for human modification. Examples of
transparent image formats include PNG, XCF and JPG. Opaque formats include proprietary formats
that can be read and edited only by proprietary word processors, SGML or XML for which the DTD
and/or processing tools are not generally available, and the machine-generated HTML, PostScript
or PDF produced by some word processors for output purposes only.
The Title Page means, for a printed book, the title page itself, plus such following pages as are
needed to hold, legibly, the material this License requires to appear in the title page. For works
in formats which do not have any title page as such, Title Page means the text near the most
prominent appearance of the works title, preceding the beginning of the body of the text.
The publisher means any person or entity that distributes copies of the Document to the public.
A section Entitled XYZ means a named subunit of the Document whose title either is precisely XYZ or contains XYZ in parentheses following text that translates XYZ in another language.
(Here XYZ stands for a specific section name mentioned below, such as Acknowledgements,
Dedications, Endorsements, or History.) To Preserve the Title of such a section when
you modify the Document means that it remains a section Entitled XYZ according to this definition.
The Document may include Warranty Disclaimers next to the notice which states that this License
applies to the Document. These Warranty Disclaimers are considered to be included by reference in
this License, but only as regards disclaiming warranties: any other implication that these Warranty
Page 148

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS
Disclaimers may have is void and has no effect on the meaning of this License.

2. VERBATIM COPYING
You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License
applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading
or further copying of the copies you make or distribute. However, you may accept compensation
in exchange for copies. If you distribute a large enough number of copies you must also follow the
conditions in section 3.
You may also lend copies, under the same conditions stated above, and you may publicly display
copies.

3. COPYING IN QUANTITY
If you publish printed copies (or copies in media that commonly have printed covers) of the
Document, numbering more than 100, and the Documents license notice requires Cover Texts, you
must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts: Front-Cover
Texts on the front cover, and Back-Cover Texts on the back cover. Both covers must also clearly
and legibly identify you as the publisher of these copies. The front cover must present the full title
with all words of the title equally prominent and visible. You may add other material on the covers
in addition. Copying with changes limited to the covers, as long as they preserve the title of the
Document and satisfy these conditions, can be treated as verbatim copying in other respects.
If the required texts for either cover are too voluminous to fit legibly, you should put the first ones
listed (as many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages.
If you publish or distribute Opaque copies of the Document numbering more than 100, you must
either include a machine-readable Transparent copy along with each Opaque copy, or state in or
with each Opaque copy a computer-network location from which the general network-using public
has access to download using public-standard network protocols a complete Transparent copy of
the Document, free of added material. If you use the latter option, you must take reasonably prudent
steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent
copy will remain thus accessible at the stated location until at least one year after the last time you
distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public.
It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version
of the Document.

4. MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the conditions of sections
2 and 3 above, provided that you release the Modified Version under precisely this License, with
Page 149

c
2010
n + 1, Inc All Rights Reserved

APPENDIX
the Modified Version filling the role of the Document, thus licensing distribution and modification
of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in
the Modified Version:
A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document,
and from those of previous versions (which should, if there were any, be listed in the History
section of the Document). You may use the same title as a previous version if the original
publisher of that version gives permission.
B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of
the modifications in the Modified Version, together with at least five of the principal authors
of the Document (all of its principal authors, if it has fewer than five), unless they release you
from this requirement.
C. State on the Title page the name of the publisher of the Modified Version, as the publisher.
D. Preserve all the copyright notices of the Document.
E. Add an appropriate copyright notice for your modifications adjacent to the other copyright
notices.
F. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under the terms of this License, in the form shown in the
Addendum below.
G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts
given in the Documents license notice.
H. Include an unaltered copy of this License.
I. Preserve the section Entitled History, Preserve its Title, and add to it an item stating at
least the title, year, new authors, and publisher of the Modified Version as given on the Title
Page. If there is no section Entitled History in the Document, create one stating the title,
year, authors, and publisher of the Document as given on its Title Page, then add an item
describing the Modified Version as stated in the previous sentence.
J. Preserve the network location, if any, given in the Document for public access to a Transparent
copy of the Document, and likewise the network locations given in the Document for previous
versions it was based on. These may be placed in the History section. You may omit a
network location for a work that was published at least four years before the Document itself,
or if the original publisher of the version it refers to gives permission.
K. For any section Entitled Acknowledgements or Dedications, Preserve the Title of the
section, and preserve in the section all the substance and tone of each of the contributor
acknowledgements and/or dedications given therein.
L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles.
Section numbers or the equivalent are not considered part of the section titles.
M. Delete any section Entitled Endorsements. Such a section may not be included in the
Modified Version.
Page 150

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS
N. Do not retitle any existing section to be Entitled Endorsements or to conflict in title with
any Invariant Section.
O. Preserve any Warranty Disclaimers.
If the Modified Version includes new front-matter sections or appendices that qualify as Secondary
Sections and contain no material copied from the Document, you may at your option designate
some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections
in the Modified Versions license notice. These titles must be distinct from any other section titles.
You may add a section Entitled Endorsements, provided it contains nothing but endorsements of
your Modified Version by various partiesfor example, statements of peer review or that the text
has been approved by an organization as the authoritative definition of a standard.
You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words
as a Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage
of Front-Cover Text and one of Back-Cover Text may be added by (or through arrangements made
by) any one entity. If the Document already includes a cover text for the same cover, previously
added by you or by arrangement made by the same entity you are acting on behalf of, you may not
add another; but you may replace the old one, on explicit permission from the previous publisher
that added the old one.
The author(s) and publisher(s) of the Document do not by this License give permission to use their
names for publicity for or to assert or imply endorsement of any Modified Version.

5. COMBINING DOCUMENTS
You may combine the Document with other documents released under this License, under the terms
defined in section 4 above for modified versions, provided that you include in the combination all
of the Invariant Sections of all of the original documents, unmodified, and list them all as Invariant
Sections of your combined work in its license notice, and that you preserve all their Warranty
Disclaimers.
The combined work need only contain one copy of this License, and multiple identical Invariant
Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same
name but different contents, make the title of each such section unique by adding at the end of it, in
parentheses, the name of the original author or publisher of that section if known, or else a unique
number. Make the same adjustment to the section titles in the list of Invariant Sections in the license
notice of the combined work.
In the combination, you must combine any sections Entitled History in the various original
documents, forming one section Entitled History; likewise combine any sections Entitled Acknowledgements, and any sections Entitled Dedications. You must delete all sections Entitled
Endorsements.

6. COLLECTIONS OF DOCUMENTS
Page 151

c
2010
n + 1, Inc All Rights Reserved

APPENDIX
You may make a collection consisting of the Document and other documents released under this
License, and replace the individual copies of this License in the various documents with a single
copy that is included in the collection, provided that you follow the rules of this License for verbatim
copying of each of the documents in all other respects.
You may extract a single document from such a collection, and distribute it individually under this
License, provided you insert a copy of this License into the extracted document, and follow this
License in all other respects regarding verbatim copying of that document.

7. AGGREGATION WITH INDEPENDENT WORKS


A compilation of the Document or its derivatives with other separate and independent documents
or works, in or on a volume of a storage or distribution medium, is called an aggregate if the
copyright resulting from the compilation is not used to limit the legal rights of the compilations
users beyond what the individual works permit. When the Document is included in an aggregate,
this License does not apply to the other works in the aggregate which are not themselves derivative
works of the Document.
If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the
Document is less than one half of the entire aggregate, the Documents Cover Texts may be placed
on covers that bracket the Document within the aggregate, or the electronic equivalent of covers if
the Document is in electronic form. Otherwise they must appear on printed covers that bracket the
whole aggregate.

8. TRANSLATION
Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special
permission from their copyright holders, but you may include translations of some or all Invariant
Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License, and all the license notices in the Document, and any Warranty Disclaimers,
provided that you also include the original English version of this License and the original versions
of those notices and disclaimers. In case of a disagreement between the translation and the original
version of this License or a notice or disclaimer, the original version will prevail.
If a section in the Document is Entitled Acknowledgements, Dedications, or History, the
requirement (section 4) to Preserve its Title (section 1) will typically require changing the actual
title.

9. TERMINATION
You may not copy, modify, sublicense, or distribute the Document except as expressly provided
under this License. Any attempt otherwise to copy, modify, sublicense, or distribute it is void, and
will automatically terminate your rights under this License.
Page 152

c
2010
n + 1, Inc All Rights Reserved

LINUX FOUNDATIONS
However, if you cease all violation of this License, then your license from a particular copyright
holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally
terminates your license, and (b) permanently, if the copyright holder fails to notify you of the
violation by some reasonable means prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is reinstated permanently if the copyright
holder notifies you of the violation by some reasonable means, this is the first time you have received
notice of violation of this License (for any work) from that copyright holder, and you cure the
violation prior to 30 days after your receipt of the notice.
Termination of your rights under this section does not terminate the licenses of parties who have
received copies or rights from you under this License. If your rights have been terminated and not
permanently reinstated, receipt of a copy of some or all of the same material does not give you any
rights to use it.

10. FUTURE REVISIONS OF THIS LICENSE


The Free Software Foundation may publish new, revised versions of the GNU Free Documentation
License from time to time. Such new versions will be similar in spirit to the present version, but
may differ in detail to address new problems or concerns. See http://www.gnu.org/copyleft/.
Each version of the License is given a distinguishing version number. If the Document specifies
that a particular numbered version of this License or any later version applies to it, you have the
option of following the terms and conditions either of that specified version or of any later version
that has been published (not as a draft) by the Free Software Foundation. If the Document does
not specify a version number of this License, you may choose any version ever published (not as
a draft) by the Free Software Foundation. If the Document specifies that a proxy can decide which
future versions of this License can be used, that proxys public statement of acceptance of a version
permanently authorizes you to choose that version for the Document.

11. RELICENSING
Massive Multiauthor Collaboration Site (or MMC Site) means any World Wide Web server
that publishes copyrightable works and also provides prominent facilities for anybody to edit
those works. A public wiki that anybody can edit is an example of such a server. A Massive
Multiauthor Collaboration (or MMC) contained in the site means any set of copyrightable
works thus published on the MMC site.
CC-BY-SA means the Creative Commons Attribution-Share Alike 3.0 license published by Creative Commons Corporation, a not-for-profit corporation with a principal place of business in San
Francisco, California, as well as future copyleft versions of that license published by that same
organization.
Incorporate means to publish or republish a Document, in whole or in part, as part of another
Document.
Page 153

c
2010
n + 1, Inc All Rights Reserved

APPENDIX
An MMC is eligible for relicensing if it is licensed under this License, and if all works that were
first published under this License somewhere other than this MMC, and subsequently incorporated
in whole or in part into the MMC, (1) had no cover texts or invariant sections, and (2) were thus
incorporated prior to November 1, 2008.
The operator of an MMC Site may republish an MMC contained in the site under CC-BY-SA on the
same site at any time before August 1, 2009, provided the MMC is eligible for relicensing.

ADDENDUM: How to use this License for your documents


To use this License in a document you have written, include a copy of the License in the document
and put the following copyright and license notices just after the title page:

c YEAR YOUR NAME. Permission is granted to copy, distribute and/or


Copyright
modify this document under the terms of the GNU Free Documentation License, Version
1.3 or any later version published by the Free Software Foundation; with no Invariant
Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is
included in the section entitled GNU Free Documentation License.

If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the with . . . Texts.
line with this:

with the Invariant Sections being LIST THEIR TITLES, with the Front-Cover Texts being
LIST, and with the Back-Cover Texts being LIST.

If you have Invariant Sections without Cover Texts, or some other combination of the three, merge
those two alternatives to suit the situation.
If your document contains nontrivial examples of program code, we recommend releasing these
examples in parallel under your choice of free software license, such as the GNU General Public
License, to permit their use in free software.

Page 154

c
2010
n + 1, Inc All Rights Reserved

Das könnte Ihnen auch gefallen