Mostafa Z. Ali Mostafa Z. Ali mzali@just.edu.jo 1 1 The Linux - - PowerPoint PPT Presentation

mostafa z ali mostafa z ali
SMART_READER_LITE
LIVE PREVIEW

Mostafa Z. Ali Mostafa Z. Ali mzali@just.edu.jo 1 1 The Linux - - PowerPoint PPT Presentation

Fall 2009 Lecture 5 Operating Systems: Configuration & Use CIS345 The Linux Utilities Mostafa Z. Ali Mostafa Z. Ali mzali@just.edu.jo 1 1 The Linux Utilities Linux did not have a GUI. It ran on character based terminals only,


slide-1
SLIDE 1

1‐1

Operating Systems: Configuration & Use

CIS345

Mostafa Z. Ali Mostafa Z. Ali

mzali@just.edu.jo

Fall 2009

The Linux Utilities

Lecture 5

slide-2
SLIDE 2

The Linux Utilities

  • Linux did not have a GUI. It ran on character‐based

terminals only, using a CLI

  • Command‐line utilities are often faster, more

powerful

  • Sometimes there is no GUI counterpart to a textual

utility

slide-3
SLIDE 3

Special Characters

  • Avoid using special characters as regular ones until you understand

how the shell interprets them

  • E.g.: Avoid using these special characters in a file name because

they make the file harder to reference on the command line:

& ; | * ? ‘ “ ` [ ] ( ) $ < > { } # / \ ! ~

  • RETURN, SPACE, and TAB have special meanings to the shell, but are

not considered special characters

  • RETURN ends a command line and initiates execution of a

command

  • SPACE and TAB characters separate elements on the command line

and are collectively known as whitespace or blanks

slide-4
SLIDE 4
  • If you want to use a character that has a

special meaning to the shell as a regular character, you can quote (or escape) it.

  • A slash (/) is always a separator in a path

name, EVEN when you quote it!

  • To quote a character, precede it with a

backslash (\)

** would be entered as \*\* \ would be entered as \\

slide-5
SLIDE 5
  • Another way to quote special characters is to enclose

them between single quotation marks E.g.: ** would be entered as ‘**’ E.g.: ‘This is a special character:>’ are all interpreted as regular characters

  • The only way to quote the erase character

(CONTROL‐H), the line kill character (CONTROL‐U), and other control characters (CONTROL‐M) is by preceding each with (CONTROL‐V). Single quotation marks and backslashes do not work! E.g.:

slide-6
SLIDE 6
  • The following command sends the output of

echo through a pipe to od (octal display) to display CONTROL‐U as octal 25 (025):

slide-7
SLIDE 7

Basic Utilities

  • Tip: Use a terminal, terminal emulator within

a GUI, or a virtual console to experiment with all utilities

  • A directory is a resource that can hold files
  • Tip: When you log in on the system, you are

working in your home directory. All the files you create are in your home directory unless you specify otherwise!

slide-8
SLIDE 8

Ls: Lists the Names of Files

  • Use the vim editor to create a file named

practice

slide-9
SLIDE 9

Cat: Displays a Text File

  • The cat (catenate, which means to join

together) utility displays the contents of a text file

  • The ls utility displays the name of a file,

whereas cat displays the contents of a file

slide-10
SLIDE 10

rm: Deletes a File

  • The rm (remove) utility deletes a file
  • Tip: a safer way of removing files is to use the

interactive form by following rm with the –i

  • ption and then the name of the file to be

deleted, to make sure you only delete the files that you intend to

  • You can create an alias for rm –i and put it in your

startup file so that rm always runs in interactive mode

slide-11
SLIDE 11

Less ls more: Display a Text File One Screen at a Time

  • When you want to view a file that is longer than one

screen, you can use either the less utility or the more utility

  • Each of these utilities pauses after displaying a screen of
  • text. You need to press SPACE bar to display the next screen
  • These utilities are called pagers, because they display one

page at a time

  • At the end of the file, less displays an END message and

waits for you to press q to return to the shell. Whereas more returns you directly to the shell

  • You can press h while using any of these utilities to display

the help screen while paging through a file E.g.: use the command less /etc/adduser.conf and see what happens

slide-12
SLIDE 12

hostname: Displays the System Name

  • The hostname utility displays the name of the

system you are working on E.g.:

slide-13
SLIDE 13

Working w ith Files

  • Tip: after you enter one or more letters of a

filename (following a command) on a command line, press TAB and the Bourne Again Shell will complete as much of the filename as it can

slide-14
SLIDE 14

Cp: Copies a File

  • The cp utility makes a copy of a file
  • You can use cp to make a backup copy of a file
  • r a copy to experiment with
  • Syntax: cp source‐file destination‐file

The destination‐file is then name that cp assigns to the resulting (new) copy of the file

slide-15
SLIDE 15
  • The period in memo.copy is part of the filename –

just another character

  • Sometimes it is useful to incorporate the date in the

name of a copy of a file E.g.:

  • Cp can destroy a file if the destination‐file exists

before you give a cp command.

  • Use –i (interactive) option after the cp command
slide-16
SLIDE 16

mv: Changes the Name of a File

  • The mv (move) utility can rename a file

without making a copy of it

  • Syntax: mv existing‐filename new‐filename
  • Mv can destroy a filename accidentally so use

the –i option to avoid it

slide-17
SLIDE 17

Lpr: Prints a File

  • The lpr (line printer) utility places one or more

files in a print queue for printing

  • On systems that have access to more than one

printer, you can use lpstat –p to display a list of available printers

  • The –p option instructs lpr to place a file in the

queue for a specific printer

  • This command does not specify a printer, so it

goes to the default printer

slide-18
SLIDE 18
  • You can see which jobs are in the print queue by

giving an lpstat –o command or using the lpq utility

  • You can use the job number with the lprm utility to

remove the job from print queue and stop it from printing

Sends the file report to the printer mailroom

slide-19
SLIDE 19
  • You can send more than one file to the printer

with a single command:

slide-20
SLIDE 20

grep: Searching for a String

  • The grep utility searches through one or more

files to see whether any contain a specified string of characters

slide-21
SLIDE 21
  • The –w option causes grep to match only

whole words

  • You do not need to enclose the string you are

searching for in single quotes, but doing so allows you to put SPACEs and special characters in the search string

slide-22
SLIDE 22

head: Displays the Beginning of a File

  • The head utility displays

the first ten lines of a file

  • The utility helps you

remember what a particular file contains

  • E.g.: Assuming you have a

file named months that lists 12 months of the year in calendar order,

  • ne to a line:
slide-23
SLIDE 23
  • The utility can display any number of lines.

Include a hyphen followed by the number of lines you want head to display:

slide-24
SLIDE 24

tail: Displays the End of a File

  • The tail utility is similar to head but by default

displays the last ten lines of a file

  • You can monitor lines as they are added to the

end of the growing file named logfile with the following command:

  • Press the interrupt key (CONTROL‐C) to stop

tail and display the shell prompt

slide-25
SLIDE 25

sort: Displays a File in Order

  • The sort utility displays

the contents of a file in

  • rder by lines but does

not change the original file

  • The –u option generates

a sorted list in which each line is unique (no duplicates)

  • The –n option puts a list
  • f numbers in numerical
  • rder
slide-26
SLIDE 26

Uniq: Removes Duplicate Lines from a File

  • The uniq (unique) utility

displays a file, skipping adjacent duplicate lines, but does not change the

  • riginal file
  • If a file is sorted before

it is processed by uniq, this utility ensures that no two lines in the file are the same

slide-27
SLIDE 27

diff: Compares Two Files

  • The diff (difference) utility compares two files and

displays a list of the differences between them

  • It is useful when you want to compare two

versions of a letter, report, or source code for a program

  • When using the –u (unified output format) option

first displays two lines indicating which of the files you are comparing will be denoted by a plus sign (+) and which by a minus sign (‐)

slide-28
SLIDE 28
  • The diff –u command

breaks long, multiline text into hunks, where each hunk is preceded by a line starting and ending with 2 at signs (@@)

  • The hunk identifies the

starting line number and the number of lines from each file for this hunk

What the hunk covers from both files

slide-29
SLIDE 29

File: Identifies the Contents of a File

  • The file utility is used to learn about the

contents of any file on a Linux system without having to open the file and examining it yourself

slide-30
SLIDE 30

| (Pipe): Communicates Betw een Processes

  • Process: the execution of a command by Linux
  • One of the hallmarks of both UNIX and Linux is

the communication between processes

  • The pipe (|) appearing as a solid or broken

vertical line on your keyboard, provides the simplest form of this kind of communication

  • The pipe takes the output of one utility and sends

it as an input to another utility

  • Most of what a process displays on the screen is

sent to standard output

slide-31
SLIDE 31

$ sort months | head ‐4 Apr Aug Dec Feb $ ls | wc ‐w 14 $ tail months | lpr

Displaying the # of files in a directory. The wc (word count) utility with the –w option displays the # of words in its standard input or in a file you specify on the command line

slide-32
SLIDE 32

Echo: Displays Text

  • The echo utility copies the characters you type
  • n the command line after echo to the screen

When an unquoted asterisk (*) is used

  • n the command line, the shell

expands the asterisk into a list of filenames in the directory

slide-33
SLIDE 33
  • You can use echo to create a simple file by

redirecting its output to a file:

$ echo ‘My new file.’ > myfile $ cat myfile My new file

slide-34
SLIDE 34

date: Displays the Time and Date

  • You can choose the format and select the contents of

the output of date:

slide-35
SLIDE 35

Script: Records a Shell Session

  • The script utility records all or part of a login

session, including your input and system’s responses

  • It does capture the session with vim, but vim

uses control characters to position the cursor the output will be difficult to read cat a file to make the session quickly pass before your eyes

slide-36
SLIDE 36
  • By default script captures the session in

a file named typescript. To specify a different filename:

  • Use the –a option to append to a file,
  • therwise script overwrites an existing

file $ Script filename

slide-37
SLIDE 37

todos: Converts Linux and Macintosh Files to Window s Format

  • The todos (to DOS) utility converts a Linux text

file so it can be read on Windows or Macintosh system

  • This utility is part of the tofrodos software
  • package. To install it:
  • E.g.:

Sudo aptitude install tofrodos $ Todos memo.txt

slide-38
SLIDE 38
  • Use the –b (backup) option to cause todos to

make a copy of the file with a .bak filename extension before modifying it

  • To convert Windows or Macintosh files so they

can be read on a Linux system, use the fromdos utility as in:

$ fromdos memo.txt

slide-39
SLIDE 39
  • You can use tr (translate) to change a

Windows or Macintosh text file into a Linux text file

  • The (>) symbol in the previous example

redirects the standard output of tr to the file named memo.txt

$ cat memo | tr ‐d ‘\r’ > memo.txt

The –d (delete) option causes tr to remove RETURNs (represented by \r) as it makes a copy of the file

slide-40
SLIDE 40

Compressing and Archiving Files bzip2: Compresses a File

  • The bzip2 utility compresses a file by analyzing it and recoding

it more efficiently

  • The bzip2 utility works particularly well on files that contain a

lot of repeated information such as text and some image data

  • The file in the previous example becomes 50 bytes long. The

bzip2 utility also renamed the file, appending .bz2 to its name

The –l (long) option causes ls to display more information The –v (verbose) option causes bzip2 to report how much it was able to reduce the size of the file

slide-41
SLIDE 41
  • The bzip2 utility (and its counterpart, bunzip2)

remove the original file when they compress or decompress a file, so you need to use the –k (keep)

  • ption to keep the original file
slide-42
SLIDE 42

bunzip2 and bzcat: Decompresses a File

  • Bunzip2 utility restores a file that has been compressed

with bzip2

  • The bzcat utility displays a file that has been compressed

with bzip2

  • The equivalent of cat for .bz2 files, bzcat decompresses

the compressed data and displays the decompressed data

slide-43
SLIDE 43
  • Like cat, bzcat does not change the source file
  • The bzip2recover utility recovers from media

errors (for corrupted files)

slide-44
SLIDE 44

Gzip: Compresses a File

  • gzip (GNU zip) utility is older and less efficient than bzip2
  • Its flags and operation are very similar to those of bzip2
  • A file compressed by gzip is marked by a .gz filename

extension

  • Use gzip, gunzip, and zcat just as you would use bzip2,

bunzip2, and bzcat, respectively

  • The compress utility can also compress files, not as well as
  • gzip. It marks a file that it has compresses by adding .Z to

its name

  • Tip: do not confuse gzip and gunzip with zip and unzip

utilities (system running Windows)

slide-45
SLIDE 45

tar: Packs and Unpacks Archives

  • tar: short for tape archive and it was used to create and read

archives and backup tapes

  • It is used to create a single file (called tar, archive, or tarball)

from multiple files or directory hierarchies and to extract files from a tar file

tar uses the –c (create), ‐v (verbose), and –f (write to or read from a file)

  • ptions to create the

archive all.tar

slide-46
SLIDE 46
  • The tar utility adds overhead as in the previous example

which is more appreciable on smaller files

  • The –t option displays a table of contents for the archive
  • The –x option can be used instead of the –t in that example

to extract files from a tar archive

  • You can use bzip2, compress, or gzip to compress tar files,

making them easier to store and handle

  • Apply tar then bzip2 filename extension of .tar.bz2 or

.tbz

  • Apply tar then gzip filename extension of .tar.gz or .tz
  • Apply tar then compress .tar.Z extension
slide-47
SLIDE 47
  • To unpack a tarred and gzipped file:
  • The asterisk (*) in the filename matches any character in

any filenames which saves typing

slide-48
SLIDE 48
  • The –d (directory ) option causes ls to display only

file and directory names, not the contents of the directory as it normally does

slide-49
SLIDE 49
  • You can combine the gunzip and tar commands on one

command line with a pipe (|):

  • The –c option causes gunzip to send its output through

the pipe instead of creating a file

  • The final hyphen (‐) causes tar to read from standard

input

  • A simpler solution is to use –z option to tar which causes

tar to call gunzip (or gzip when you are creating an archive) directly and simplifies the preceding command to:

slide-50
SLIDE 50

slocate: Searches for a File

  • The slocate is part of the slocate software
  • package. To install it:

sudo aptitude install slocate

slide-51
SLIDE 51

Obtaining User and System Information w ho: Lists Users on the System

  • The who utility displays a list of users who are logged in on the local

system

  • The 2nd column shows the device that each user’s terminal,

workstation, or terminal emulator is connected to

  • The 3rd column shows the date and time the user the user logged in
  • The 4th column is optional and shows in parenthesis the name of

the system that a remote user logged in from

slide-52
SLIDE 52
  • The information that who displays is useful when you

want to communicate with a user on the local system

  • When the user is logged in, you can write to establish

communication immediately

  • If the output of who runs off the screen redirect the
  • utput through a pipe to less, or you can use the pipe

to redirect the output through grep to look for a specific name

  • If you need to find out which terminal you are using ,
  • r what time you logged in, you can use the command

who am i as in:

slide-53
SLIDE 53

finger: Lists Users on the System

  • In addition to usernames, finger supplies each user’s full

name along with information about the device the user’s terminal is connected to, how recently you typed something using the keyboard, when the user logged in, and what contact information is available

$ finger Login Name Tty Idle Login Time Office Mostafa Mostafa Ali *tty2 Jul 25 16:42 Mostafa Mostafa Ali Pts/4 3 Jul 25 17:27 (Razor) Sonia Sonia the flower *tty4 29 Jul 25 17:18 Alex Alex Isaac *tty1 1:07 Jul 25 16:39

The * means that the user has blocked messages sent to the terminal

slide-54
SLIDE 54
  • Most of the information in this example was collected by

finger from system files. The finger utility searched for a file named .plan in Alex’s home directory and displayed its contents

  • In a similar manner, finger displays the contents of the

.project and .pgpkey files in your home directory

$ finger alex Login: alex Name: Alex Isaac Directory: /home/alex Shell: /bin/bash On since Wed Jul 25 16:39 (PDT) on tty1 (messages off) 5 minutes 52 seconds idle On since Wed Jun 6 12:47 (PDT) on tty1 from Razor Last login Wed Jun 6 12:47 (PDT) on 1 from Razor New mail received Wed Jul 25 11:16 2009 (PDT) Unread since Wed Jul 25 11:16 2009 (PDT) Plan: I will be at a conference in Hawaii all next week. If you need to see me, contact CIS Dep., x1693.

slide-55
SLIDE 55
  • The finger utility, which is not case sensitive, can search for

information on Mostafa using his first or last name:

$ finger Mostafa Login: Excalibur Name: Mostafa Ali …. $ finger Ali Login: Excalibur Name: Mostafa Ali

slide-56
SLIDE 56

W: Lists Users on the System

  • The w utility displays a list of the users who are logged

in

  • The JCPU and PCPU columns identify how much

computer processor time each user has used during this login session and on the task that is running.

  • The last column shows the command each user is

running

slide-57
SLIDE 57
  • The first line that the w utility displays includes the

time of day, the period of time the computer has been running (days, hours, minutes), the # of users logged in, and the load average (how busy the system is)

  • The three load average #s represent the # of jobs

waiting to run, averaged over the past 1, 5, and 15 minutes

  • Use the uptime utility to display just this line
slide-58
SLIDE 58
slide-59
SLIDE 59

Communicating w ith other Users Write: Sends a Message

  • The write utility sends a message to another user

who is logged in which establishes two‐way communication

  • Syntax:
  • The terminal is an optional device name that is useful

if the user is logged in more than once

slide-60
SLIDE 60
  • Sometimes it helps to type o (over) when you are

ready for the other person to type and type oo (over and out) when you are ready to end the conversation:

  • Pressing CONTROL‐D stops the communication and

returns to the shell

slide-61
SLIDE 61

mesg: Denies or Accepts Messages

  • Messages to the screen are blocked by default
  • To allow other users to send you messages:
  • If max had given this command before Zach

tried to send him a message, Zach would have seen:

$ mesg y $ write max Write: max has messages disabled

slide-62
SLIDE 62

Using vim to Create and Edit a File

  • In addition to working with these slides

, you may want to try vim’s instructional program (vimtutor). Give its name as a command to run it

  • Vimtutor and vim help files are not

installed by default. To install:

  • Start vim as:
  • Which opens a screen like this

$ vim practice sudo aptitude install vim‐runtime

slide-63
SLIDE 63
  • The tildes (~) at the left means that the file is empty. They

disappear as you add lines of text to the file

  • If you start vim with a terminal that is not in the terminfo

database, vim displays an error message and the terminal type defaults to ansi, which works on many terminals

  • To reset the terminal type, press ESCAPE and then give the

following command to exit vim and display the shell prompt: Which quits without saving your work

:q!

slide-64
SLIDE 64

Command and Input Modes

  • vim’s modes of operation:

– Command mode (Normal mode) and – Input mode – Last line mode

  • In input mode vim accepts anything you enter as

text and displays it on the screen. Press ESCAPE to return vim to the Command mode

  • Vim displays INSERT at the lower‐left corner of

the screen while it is in Insert mode

  • To display line numbers next to the text you are

editing:

:set number RETURN

slide-65
SLIDE 65
  • The colon (:) puts vim into

another mode, last line mode

  • To turn off line numbers:
  • Remember that vim is case

sensitive

:set nonumber RETURN

slide-66
SLIDE 66

Entering Text

  • When you start vim, you must put it in Input

mode before start typing by pressing the i (insert before cursor) key or the a (append after cursor) key

  • The keys that backup and correct a shell

command line serve the same functions when vim is in input mode

  • The erase, line kill, and word kill keys (CONTROL‐

H, CONTROL‐U, and CONTROL‐W, respectively)

slide-67
SLIDE 67
  • While vim is in Command mode, you use the RTURN key, the SPACE

bar, and the ARROW keys to move the cursor

  • If your terminal does not have ARROW keys or if the emulator does

not support them, you can use h, j, k, l keys to move the cursor left, down, up, and right, respectively

  • To delete a single character you can move the cursor until it is over

the character you want to delete and then giving the command x

  • To delete a word, move the cursor on the first letter of the word

and then giving the command dw (delete word)

  • To delete a line of text, move the cursor until it is anywhere on the

line and then the command dd

  • A shorthand for the two commands dw followed by the i command

is cw (change word), which puts vim into Input mode

slide-68
SLIDE 68

Undoing Mistakes

  • If you delete a character, line, or word by

mistake or give any command you want to reverse, give the command u (Undo) immediately after the command you want to undo

  • With the compatible parameter set however,

vim can undo only the most recent change

slide-69
SLIDE 69

Entering Additional Text

  • To insert new text within existing text, you can:

– move the cursor so it is on the character that follows the new text you plan to enter. Then give the i (Insert) command to put vim in Input mode, enter the new text, and press ESCAPE to return vim to Command mode – Or you can position the cursor on the character that precedes the new text and use the a (Append) command

  • To enter one or more lines, position the cursor on the

line above where you want the new text to go. Give the command o (Open) vim opens a blank line, puts the cursor on it, and goes into Input mode

  • The O command works in the same way o works, except

that it opens a blank line above the line the cursor is on

slide-70
SLIDE 70

Ending the Editing Session

  • When vim is in Command mode, use ZZ

(uppercase Zs) command to write then newly entered text to the disk and end the editing session