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, using a CLI • Command ‐ line utilities are often faster, more powerful • Sometimes there is no GUI counterpart to a textual utility
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
• 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 \\
• 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.:
• The following command sends the output of echo through a pipe to od (octal display) to display CONTROL ‐ U as octal 25 (025):
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!
Ls: Lists the Names of Files • Use the vim editor to create a file named practice
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
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 option 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
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
hostname: Displays the System Name • The hostname utility displays the name of the system you are working on E.g.:
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
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
• 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
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
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
Sends the file report to the printer mailroom • 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
• You can send more than one file to the printer with a single command:
grep: Searching for a String • The grep utility searches through one or more files to see whether any contain a specified string of characters
• 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
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:
• The utility can display any number of lines. Include a hyphen followed by the number of lines you want head to display:
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
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
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
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 ( ‐ )
• The diff –u command breaks long, multiline text into hunks , where What the hunk covers from both files 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
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
| (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
$ sort months | head ‐ 4 Apr Aug Dec Feb Displaying the # of files in a directory. The wc (word $ ls | wc ‐ w count) utility with the –w option displays the # of words 14 in its standard input or in a file you specify on the command line $ tail months | lpr
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
• 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
date: Displays the Time and Date • You can choose the format and select the contents of the output of date:
Recommend
More recommend