UNIX Chapter 09 Basic File Processing Mr. Mohammad Smirat.
Published byModified over 6 years ago
Presentation on theme: "UNIX Chapter 09 Basic File Processing Mr. Mohammad Smirat."— Presentation transcript:
UNIX Chapter 09 Basic File Processing Mr. Mohammad Smirat
Viewing Complete Files You can display the complete contents of one or more files on screen by using the cat command. cat[options][file-list] Concatenate/display the files in file-list on standard output. Options are: -e display $ at the end of each line -n put line numbers with the displayed lines.
CAT Examples $cat student_records [ contents of file displayed] $cat ~/courses/cis310/exam1 ~/courses/cis311/exam2 [ contents of exam1 and exam2] When cat command is executed without arguments, it takes input from the keyboard. $cat This is a test. This is a test.
Viewing Complete Files (cont … ) The UNIX utility nl allows you to display files having line numbers, so the nl student_records will display the lines in the student records file with line numbers. $ nl student_record 1 john Doe ECE 3.54 2 Pam Meyer CIS 3.75 3 Amy Nash CIS 3.25 The same task can be performed by: $cat -n student_record
Viewing Complete Files (cont … ) You can use the pr utility to display file contents of a file with time stamp and page number. $pr student_records Feb 10 11:24 2001 student_records Page 1 John Doe ECE 3.54
Viewing Files One Page at a time more[option][file list] To concatenate/display the files in file-list on standard output a screenful at a time. Options: +/str start at the first line containing str. -nN display N lines per screen/page +N start displaying the contents of the file at line number N.
MORE Examples $ more sample [ contents of sample one page at a time] $ more sample letter memo [ contents of sample, letter, and memo] $ more -n20 sample [ contents of sample 20 lines per page] $more +/*.c [ contents of all.c files in the current directory]
Viewing the Head or Tail of a File The UNIX command for displaying the head or tail of a file are head and tail. head[options][file list] will display the beginning portion (head) of files in file list, the default head size is 10 lines. Options -N display first N lines. $head sample [first 10 lines of sample displayed] $head sample memo phones [ first 10 lines of sample, memo, and phone] $head -5 sample [first 5 lines of sample]
Viewing the Head or Tail of a File (CONT … ) Tail[options][file-list] display the last portion (tail) of the files in file_list, the default tail size is 10 lines. Options -f follow growth of the file after displaying the last line of a file, and display lines as they appended to the file +n/-n start n lines from the beginning of the file for +n, and n lines from the end of the file. -r display lines in reverse order (last line first)
Viewing the Head or Tail of a File (CONT … ) $tail sample … last 10 lines of sample … $ tail -5 sample … last 5 lines of sample … $ tail +8 sample … all the lines in sample starting with line 8 … $tail -5r sample …last 5 lines of sample displayed in reverse order.. $tail -f sample … last 10 lines of sample … more data as it is append to sample …
Copying Files The UNIX command for copying files is cp. cp[options] file1 file2 will copy file1 to file2 if file2 is a directory, make a copy of file1 in this directory. Options -i if destination exist, prompt before overwriting. -p preserve permissions and modifications times. -r recursively copy files and subdirectories.
COPY Examples $ls memo sample temp $cp temp temp.bak $ls memo sample temp temp.bak $cp ~/.profile ~/sys.backups/.profile.bak It will produce an error if.profile does not exist, or do not have permission to read it, or the other file does exist or you do not have a write permission to it. $cp -i lab* ~/courses/cis311 The above command copies all file in the current directory starting with the string lab to the directory ~/courses/cis311. The i option to prompt you for overwriting a file if it exist in the destination directory.
Moving Files The operation move in UNIX may result in simply renaming a file if it is on the same file system. If the source and destination are on different file system, the move operation results in a physical copy of the source file to the destination, followed by removal of the source file. mv [options] file1 file2 mv [options] file-list directory will move file 1 to file 2 or rename file1 as file2. Move all files in file_list to directory. Options: -f force the move regardless of the permission of the destination file. -i prompt the user before overwriting the destination.
MV Examples $mv temp temp.moved $mv temp backups/temp.old The first will move temp to temp.moved. The second will move temp to backups directory as temp.old. $mv -f temp temp.old The above command is a sure move one, it will force the move regardless of the permission for the target file. $mv dir/* dir2 will move all file and subdirectories of dir to dir2, after the move dir2 becomes an empty directory.
Removing/deleting Files The UNIX command for removing (deleting) files is rm. rm[options] file list To remove files in file list from the file structure. Options -f force remove regardless of the permission for file_list. -i prompt the user before removing the files in file_list. -r recursively remove the files in the directory which is passed as an argument. You want to be sure that you want to do so before using this option.
RM Examples $rm temp $rm temp backups/temp.old $rm -f phones grades ~/letters/letter.john $rm ~/dir/* The first removes file temp. The second removes the temp.old from the backups directory. The third removes the files phone, grades and ~/letter/letter.john regardless of their access permission. The fourth removes all the files from ~/dir directory.
RM Examples (cont … ) $rm [kK]*.prn will remove all files from current directory with name starting in letter k and with extension.prn $rm [a-kA-Z]*.prn all file with extension.prn and starting with lower case letter a-k or upper case letter. $rm -r ~ The above command you should never execute unless you know what you are doing??? You should always combine -i and -r options? Why? $ rm -ir ~/dir1 rm: examines files in direcrory /home.dir1(y/n)? rm: remove /home/dir1/file1 (y/n)? rm: remove /home/dir1/file2 (y/n)?
Determining File Size The two commands commonly used are the ls -l and wc. wc [options] file list will display the size of the files in the file list as number lines, words, and characters. Options -c display # of chars. -l display # of lines. -w display # of words.
WC Examples $wc sample 424224sample lineswordsbytesname $wc letter sample test [ display each file info (lines,words,and bytes) then total them] $wc -lw letter 44250letter $wc /etc/passwd 000/etc/passwd $wc /usr/include/sys/* … output of the command …
Appending to Files cat[file_list] >> destination file $cat sample >> temp will append the contents of sample at the end of file temp. $cat memo1 memo2 memo3 >> memos.dat will append the contents of the three files at the end of memos.dat.
Appending to Files (cont … ) $cat business_lunch hi john, could we meet tomorrow at lunch $cat >> business_lunch we need to discuss some business please confirm by this afternoon, thanks $cat business_lunch hi john, could we meet tomorrow at lunch we need to discuss some business please confirm by this afternoon, thanks
Combining Files cat[file-list] > destination file The destination file is overwritten if it is already exists. If you do not have permission you will get an error. $cat memo1 memo2 memo3 > memos.file will put all three files into memos.file in the same order. The above command is the same as the following: $cat memo1 > memos.file $cat memo2 >> memos.file $cat memo3 >> memos.file
Removing repeated Lines You can use the unique command to remove all but one copy of successive repeated lines in a file. The command is designed to work on sorted files. Uniq [options][input file][output file] remove repetitions lines from the sorted file, and send unique lines to output file. Options -c precede each output line by the number of times it occurs. -d display the repeated lines -u display the lines that are not repeated.
Removing repeated Lines (cont...) $uniq -c sample display unique lines and precede each line by # of times it occurs. $uniq -d sample out find out the repeated lines and send output to out file.