operating systems: configuration & use cis345mzali/courses/fall14/cis345/slides/chapter5.pdf ·...

70
11 Operating Systems: Configuration & Use CIS345 Mostafa Z. Ali [email protected] Fall 2009 The Linux Utilities Lecture 5

Upload: others

Post on 16-Aug-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

1‐1

Operating Systems: Configuration & UseCIS345

Mostafa Z. [email protected]

Fall 2009

The Linux Utilities

Lecture 5

Page 2: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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   

Page 3: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 4: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 5: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• Another way to quote special characters is to enclose them between single quotation marksE.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.: 

Page 6: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• The following command sends the output of echo through a pipe to od (octal display) to display CONTROL‐U as octal 25 (025):

Page 7: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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! 

Page 8: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

Ls: Lists the Names of Files

• Use the vim editor to create a file named practice

Page 9: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 10: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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 –ioption 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 

Page 11: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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 fileE.g.: use the command less /etc/adduser.conf and see what happens  

Page 12: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

hostname: Displays the System Name

• The hostname utility displays the name of the system you are working on

E.g.: 

Page 13: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

Working with 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

Page 14: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 15: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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 

Page 16: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 17: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 18: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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

Page 19: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• You can send more than one file to the printer with a single command:

Page 20: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

grep: Searching for a String

• The grep utility searches through one or more files to see whether any contain a specified string of characters

Page 21: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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

Page 22: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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, one to a line:

Page 23: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• The utility can display any number of lines. Include a hyphen followed by the number of lines you want head to display:

Page 24: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 25: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

sort: Displays a File in Order

• The sort utility displays the contents of a file in order 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 of numbers in numerical order

Page 26: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

Uniq: Removes Duplicate Lines from a File• The uniq (unique) utility displays a file, skipping adjacent duplicate lines, but does not change the original 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

Page 27: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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 (‐) 

Page 28: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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

Page 29: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 30: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

| (Pipe): Communicates Between 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

Page 31: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

$ sort months  |  head ‐4AprAugDecFeb

$ ls |  wc ‐w14

$ 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

Page 32: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

Echo: Displays Text

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

When an unquoted asterisk (*) is used on the command line, the shell expands the asterisk into a list of filenames in the directory

Page 33: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• You can use echo to create a simple file by redirecting its output to a file:

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

Page 34: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

date: Displays the Time and Date

• You can choose the format and select the contents of the output of date:

Page 35: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 36: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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, otherwise script overwrites an existing file

$  Script   filename

Page 37: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

todos: Converts Linux and Macintosh Files to Windows 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

Page 38: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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

Page 39: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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

Page 40: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

Compressing and Archiving Filesbzip2: 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

Page 41: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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) option to keep the original file 

Page 42: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 43: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• Like cat, bzcat does not change the source file

• The bzip2recover utility recovers from media errors (for corrupted files)

Page 44: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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 gunzipwith zip and unziputilities (system running Windows)

Page 45: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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) options to create the archive all.tar

Page 46: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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

Page 47: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• To unpack a tarred and gzipped file:

• The asterisk (*) in the filename matches any character in any filenames which saves typing 

Page 48: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• The –d (directory ) option causes ls to display only file and directory names, not the contents of the directory as it normally does 

Page 49: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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:

Page 50: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

slocate: Searches for a File

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

sudo aptitude install slocate

Page 51: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

Obtaining User and System Informationwho: 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

Page 52: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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 output 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 , or what time you logged in, you can use the command who am i as in:

Page 53: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 54: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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 alexLogin: alex Name: Alex IsaacDirectory: /home/alex Shell: /bin/bashOn since Wed Jul 25 16:39 (PDT) on tty1   (messages off) 

5 minutes 52 seconds idleOn since Wed Jun 6 12:47 (PDT) on tty1 from RazorLast login Wed Jun 6 12:47 (PDT) on 1 from RazorNew 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.

Page 55: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• The finger utility, which is not case sensitive, can search for information on Mostafa using his first or last name:

$  finger  MostafaLogin: Excalibur                        Name: Mostafa Ali….$  finger  AliLogin: Excalibur                        Name: Mostafa Ali

Page 56: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 57: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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

Page 58: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo
Page 59: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

Communicating with other UsersWrite: 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

Page 60: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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

Page 61: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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   maxWrite: max has messages disabled

Page 62: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 63: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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 terminfodatabase, 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!

Page 64: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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 

Page 65: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• The colon (:) puts vim into another mode, last line mode

• To turn off line numbers:

• Remember that vim is case sensitive

:set  nonumber RETURN 

Page 66: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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)

Page 67: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

• 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

Page 68: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 69: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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

Page 70: Operating Systems: Configuration & Use CIS345mzali/courses/Fall14/Cis345/slides/Chapter5.pdf · 1‐1. Operating Systems: Configuration & Use. CIS345. Mostafa Z. Ali. mzali@just.edu.jo

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