Jozef Goetz, expanded by Jozef Goetz, 2009 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 Copyright © 2005 Pearson Addison- Wesley. All rights reserved.
Jozef Goetz, Objectives You may ignore last slides 19 – 24 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 >,>>,^, ~, [ ], *, ?, cancel, cat …
Jozef Goetz, Viewing Contents of Text Files Viewing Complete Files cat [options][file-list] Viewing files One Page at a Time more [options][file-list] -N – display N line per screen/page +N - display the contents at line N and after -pr – display as cat + partitions into pages with a header for each page + /str start 2 lines before the 1 st line containing str Options: -n – display line number
Jozef Goetz, 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 'while' ch9.c less –N my It starts displaying file without reading all the file, which makes more efficient than the more command less command
Jozef Goetz, nl vs cat command nl - display the file with line numbers cs253u]$ nl m 1 date 2 ls -al 3 dir cs253u]$ cat -n m 1 date 2 ls -al 3 dir cs253u]$
Jozef Goetz, Table 9.1 Some Useful less Commands (continued on next slide)
Jozef Goetz, Table 9.1 Some Useful less Commands (continued from previous slide)
Jozef Goetz, Viewing the Head or Tail of a File head [option][file-list] tail [option][file-list] s -n – display 1 st n lines -n – display last n lines see cmd more –n +n – starting with line n -r - reverse order Viewing Contents of Text Files
Jozef Goetz, Copying, Moving and Removing Files Copying Files cp [options] file1 file2 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
Jozef Goetz, Moving Files mv [options] file1 file2 // 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 $ mv dir/* dir2 - move all files and directories to dir2 Copying, Moving and Removing Files
Jozef Goetz, Removing/Deleting Files rm [options] file-list Options: -f force remove regardless of the permission -i prompt before -r recursively remove files and subdirectories Copying, Moving and Removing Files
Jozef Goetz, Determining File Size 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 Copying, Moving and Removing Files
Jozef Goetz, Find 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 regular file l symbolic link s socket D door (Solaris) find /usr/bin –type d - find the directories in usr/bin find. –type l | wc l - what does it do?
Jozef Goetz, cat [file-list] >> destination-file append at the end of ‘destination-file’ cat [file-list] > destination-file - combine the files in ‘file-list’ and put them in ‘destination-file’ The ‘destination-file’ is overwritten if it already exist Appending to Files and Combining Files cat f1 f2 f3 > f4
Jozef Goetz, diff [options][file1][file2] - display differences line by line Comparing Files
Jozef Goetz, Comparing Files //add 2 lines (w –write, q –quite) and send diff.script //save the differences in diff.script //-e generates and display a script for the ed editor //that can be executed to change Fall_OH to Spring_OH // now Spring_OH is the same as Fall_OH See details p Sarwar, UNIX 2005
Jozef Goetz, uniq [options][+N][input-file][output-file] - remove repetitious lines from the sorted ‘input- file’ and send unique lines to ‘output-file’ $ 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! $ Options: -c precede each output line by # of occurs -d display repeated lines -u display unrepeated lines Removing Repeated Lines
Jozef Goetz, Removing Repeated Lines uniq [options][+N][input-file][output-file] $ uniq -c sample 1 This is a test file for the uniq command. 1 It contains some repeated and some nonrepeated lines. 3 Some of the repeated lines are consecutive, like this. 1 And, some are not consecutive, like the following. 1 Some of the repeated lines are consecutive, like this. 1 The above line, therefore, will not be considered a repeated 2 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! $ Options: -c precede each output line by # of occurs -d display repeated lines -u display unrepeated lines
Jozef Goetz, Printing Files
Jozef Goetz, Printing Files and Controlling Print Jobs Printing Files: lp [options] file-list lp Options: -n N print N copies -d prt submit for the ‘ptr’ printer -P page-list print pages specified in page-list // header on every page BSD version:UNIX System V, LINUX versions: lpr Options: -# N print N copies -P prt submit for the ‘ptr’ printer // as above with line numbers lpr [options] file-list
Jozef Goetz, Print Status – System V lpstat - display status of the print jobs 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
Jozef Goetz, lpq [options] Options: -P printer-list status all specified printers Print Status – BCD
Jozef Goetz, Canceling Your Print Job cancel [options] [printer] Options: -jobID-list cancel jobs specified in ‘jobID-list’ -ulogin cancel jobs that were issued by the user ‘login’ cancel – System V
Jozef Goetz, Canceling Your Print Job (Contd) lprm [options][jobID-list][user(s)] Options: -Pptr print queue for the ‘ptr’ - remove all jobs owned by ‘user’ cancel – BSD