c6e2e44707b511635a44b049e0d26f0a.ppt
- Количество слайдов: 25
1 You may ignore last slides 20 – 25 Credits: Parts of the slides are based on slides created by UNIX textbook authors, Syed M. Sarwar, Robert Koretsky, Syed A. Sarwar, 2005 Addison Wesley Jozef Goetz, 2014 expanded by Jozef Goetz, 2014 Copyright © 2005 Pearson Addison. Wesley. All rights reserved.
Objectives You may ignore last slides 20 – 25 n n n n To discuss how to display contents of a file To explain copying, appending, moving/ renaming, and removing/ deleting files. To describe how to determine the size of a file To discuss commands for comparing files To describe how to combine files To discuss printer control commands To cover the commands and primitives n > , >> , ^, ~, [ ], *, ? , cancel, cat … Jozef Goetz, 2014 2
Viewing Contents of Text Files n Viewing Complete Files n n cat [options][file-list] Viewing files One Page at a Time n more [options][file-list] 3 Options: -n – display line number -N – display N line +N - display the contents at line N and after q+10 /str start searching str from line 10 $ more +10 +/author my. . . skipping --author with -l, print the author of each file Jozef Goetz, 2014 -b, --escape print octal escapes for nongraphic characters
less command n less [options] file-list display files in ‘file-list‘ one screen at a time Options: -N display line number -p pattern search for the first occurrence of ‘pattern’ in the file $ less -Np 'author' my 24 --author 25 with -l, print the author of each file 26 27 -b, --escape $ less -N my 1 GNU bash, version 3. 2. 25(1)-release (i 386 -redhat-linux-gnu) 2 These shell commands are defined internally. Type `help' to see this list. 3 Type `help name' to find out more about the function `name'. Exit: ctrl+z or q It starts displaying file without reading all the file, which makes more efficient than the more commands Jozef Goetz, 2014 4
Table 9. 1 Some Useful less Commands (continued on next slide) Jozef Goetz, 2014 5
Table 9. 1 Some Useful less Commands (continued from previous slide) Jozef Goetz, 2014 6
nl vs cat command nl - display the file with line numbers [cs 253 u@shell cs 253 u]$ nl my 1 date 2 ls -al 3 dir [cs 253 u@shell cs 253 u]$ cat -n my 1 date 2 ls -al 3 dir [cs 253 u@shell cs 253 u]$ Jozef Goetz, 2014 7
Viewing Contents of Text Files Viewing the Head or Tail of a File n n n head [option][file-list] -n – display 1 st n lines tail [option][file-list] -n – display last n lines $ head my line 1 line 2 line 3 line 4 line 5 line 6 $ head -2 my line 1 line 2 ~$ tail my line 1 line 2 line 3 line 4 line 5 line 6 $ tail -3 my line 4 line 5 line 6 Jozef Goetz, 2014 see cmd more –n S more -5 my line 1 line 2 line 3 line 4 line 5 8
Copying, Moving and Removing Files n Copying Files n cp [options] file 1 file 2 n n n Jozef Goetz, 2014 Options: -f force copying if there no write permission on the destination file -i prompt before overwriting -p preserve owner ID, group ID, permissions, and modification time -r recursively copy files and subdirectories 9
Copying, Moving and Removing Files • Moving Files • • mv [options] file 1 file 2 // move or rename mv [options] file-list directory § Options: § -f force move if there no write permission on the destination file § -i prompt before overwriting Jozef Goetz, 2014 $ mv dir/* dir 2 - move all files and directories to dir 2 10
Copying, Moving and Removing Files • Removing/Deleting Files • rm [options] file-list Options: -f force remove regardless of the permission -i prompt before -r recursively remove files and subdirectories Jozef Goetz, 2014 11
Copying, Moving and Removing Files n Determining File Size n n ls –l word count wc [options] file-list Options: -l # of lines -w # of words -c # of characters • this is the order of display for wc file Jozef Goetz, 2014 12
Find 13 find - search for files in a directory hierarchy SYNOPSIS find [path. . . ] [expression] -name - the file being evaluated meets this criterion if filename matches its name -print - display the result find. –name ‘a*’ –print finds and display the filenames of all the files in the working dir, and all subdirectories, that have filenames that begin with a -type c File is of type c: b block (buffered) special c character (unbuffered) special d directory p named pipe (FIFO) f find my* my my 1 my 2 regular file l $ find -name ‘my’ /my symbolic link s socket D door (Solaris) find /usr/bin –type d - find the directories in usr/bin [jgoetz@faculty usr]$ find /usr/bin -type l|wc –l Jozef Goetz, 2014 208 - find the number of lines with directories
Find # of directories, links [jgoetz@faculty usr]$ cd /usr [jgoetz@faculty usr]$ ls -l total 224 drwxr-xr-x 2 root 36864 Dec 11 04: 02 bin drwxr-xr-x 2 root 4096 Oct 1 2009 etc drwxr-xr-x 2 root 4096 Oct 1 2009 games drwxr-xr-x 55 root 4096 Oct 30 14: 24 include drwxr-xr-x 6 root 4096 Jun 5 2013 kerberos drwxr-xr-x 102 root 57344 Dec 11 04: 02 lib drwxr-xr-x 7 root 4096 Dec 11 04: 02 libexec drwxr-xr-x 11 root 4096 Oct 1 2009 local drwx------ 2 root 16384 Dec 10 2008 lost+found drwxr-xr-x 2 root 20480 Dec 11 04: 02 sbin drwxr-xr-x 161 root 4096 Jan 15 2013 share drwxr-xr-x 4 root 4096 Mar 30 2010 src lrwxrwxrwx 1 root 10 Mar 30 2010 tmp ->. . /var/tmp drwxr-xr-x 3 root 4096 Dec 10 2008 X 11 R 6 // to list all symbolic links [jgoetz@faculty usr]$ ls -l|grep -e ^l lrwxrwxrwx 1 root 10 Mar 30 2010 tmp ->. . /var/tmp [jgoetz@faculty usr]$ ls -l|grep -e ^l|wc -l 1 // to find # of directories [jgoetz@faculty usr]$ ls -l|grep -e ^d|wc -l 13 Jozef Goetz, 2014 -e - patern 14
Appending to Files and Combining Files cat [file-list] >> destination-file n append at the end of ‘destination-file’ cat [file-list] > destination-file n n n Jozef Goetz, 2014 - combine the files in ‘file-list’ and put them in ‘destination-file’ The ‘destination-file’ is overwritten if it already exist cat f 1 f 2 f 3 > f 4 15
Comparing Files diff [options][file 1][file 2] - display differences line by line Jozef Goetz, 2014 16
Comparing Files 17 See details p. 232 -3 Sarwar, UNIX 2005 //-e generates and display a script for the ed editor //that can be executed to change Fall_OH to Spring_OH //save the differences in diff. script //add 2 lines (w –write, q –quite) and send diff. script // now Spring_OH is the same as Fall_OH Jozef Goetz, 2014
Removing Repeated Lines 18 uniq [options][+N][input-file][output-file] - remove repetitious lines from the sorted ‘inputfile’ and send unique lines to ‘output-file’ Options: -c precede each output line by # of occurs -d display repeated lines -u display unrepeated lines $ cat sample This is a test file for the uniq command. It contains some repeated and some nonrepeated lines. Some of the repeated lines are consecutive, like this. And, some are not consecutive, like the following. Some of the repeated lines are consecutive, like this. The above line, therefore, will not be considered a repeated line by the uniq command, but this will be considered repeated! $ uniq sample This is a test file for the uniq command. It contains some repeated and some nonrepeated lines. Some of the repeated lines are consecutive, like this. And, some are not consecutive, like the following. Some of the repeated lines are consecutive, like this. The above line, therefore, will not be considered a repeated line by the uniq command, but this will be considered repeated! $ Jozef Goetz, 2014
Removing Repeated Lines 19 uniq [options][+N][input-file][output-file] $ 1 1 3 1 1 1 2 Options: -c precede each output line by # of occurs -d display repeated lines -u display unrepeated lines uniq -c sample This is a test file for the uniq command. It contains some repeated and some nonrepeated lines. Some of the repeated lines are consecutive, like this. And, some are not consecutive, like the following. Some of the repeated lines are consecutive, like this. The above line, therefore, will not be considered a repeated line by the uniq command, but this will be considered repeated! $ uniq -d sample Some of the repeated lines are consecutive, like this. line by the uniq command, but this will be considered repeated! $ uniq -d sample out $ cat out Some of the repeated lines are consecutive, like this. line by the uniq command, but this will be considered repeated! $ Jozef Goetz, 2014
Printing Files Jozef Goetz, 2014 20
Printing Files and Controlling Print Jobs n Printing Files: UNIX System V, LINUX versions: lp [options] file-list lp -n -d -P 21 Options: N prt page-list BSD version: lpr [options] file-list lpr Options: -# N print N copies -P prt submit for the ‘ptr’ printer print N copies submit for the ‘ptr’ printer print pages specified in page-list // header on every page // as above with line numbers Jozef Goetz, 2014
Print Status – System V n lpstat - display status of the print jobs Jozef Goetz, 2014 lp Options: -d status on the default printer for lp -p printer-list status all specified printers -u user-list status of print jobs from ‘user-list’ -a status all printers 22
Print Status – BCD n Jozef Goetz, 2014 lpq [options] Options: -P printer-list status all specified printers 23
cancel – System V n Canceling Your Print Job n cancel [options] [printer] Options: -job. ID-list -ulogin Jozef Goetz, 2014 cancel jobs specified in ‘job. ID-list’ cancel jobs that were issued by the user ‘login’ 24
cancel – BSD n Canceling Your Print Job (Contd) n lprm [options][job. ID-list][user(s)] Options: -Pptr print queue for the ‘ptr’ - remove all jobs owned by ‘user’ Jozef Goetz, 2014 25