<<

Files and Its Contents CS 40: INTRODUCTION TO AND LINUX OPERATING SYSTEMS Objectives • Introduce UNIX commands such as: • • diff • cp • Allows you to copy files within a directory or from one directory to another • The structure is: cp file1 file2 (copy within same dir) or cp (s) dir (copy to different dir) diff • Used to compare files to see if one file or directory is a copy of another • For files, its structure is: diff file1 file2 • The output of diff is a list of commands describing how to convert file1 into file2 diff (2) • For directories, its structure is: diff -rq dir1 dir2 • The output of diff is a list of files that are different between two directories, as well as files that are unique to one directory (i.e. in one directory and not the other) • The main use of diff is to determine if the two files or directories have the same contents uniq • Removes consecutive duplicates from a file • If two or more consecutive lines are the same, only one copy of the line is sent forward to the output • The command structure is: uniq [options] file • There are options to show only lines that were not duplicated, counts of lines that were duplicates, and other options uniq (2) • If the file repeat contains: • The output of uniq repeat is: aaa $uniq repeat aaa aaa bbb bbb bbb ccc ccc ccc head • Displays the first few lines of a file or files • The command structure is: head [-N] file-list • By default, it will display 10 lines from each file specified in the command • A –N option will display N lines • When more than one file is specified, the name of each file is printed before each group of lines tail • Displays the last few lines of a file • The command structure is: tail [±[N] [options]] file • By default, it will display the last 10 lines • A –N option will display the last N lines • A +N option will display lines starting N lines from the beginning of the file tail (2) • Other options may be used with tail to change the units from lines to a block of characters or to reverse the order of the display • To show the last 5 characters of a file: tail –5c file • To show the last 5 lines in reverse: tail –5r file wc • Stands for word count • Counts the words, characters, and lines of file(s) and displays the counts on your terminal screen • The command structure is: wc [options] file-list wc (2) • When more than one file is specified, the name of each file is printed next to each group of counts and a total for the group is displayed • Options l, , and count lines, words and characters respectively • The default is to count all categories grep • Its name comes from the command g/re/p (globally search a and print) • Performs a global search with the regular expression pattern and prints all matching lines • The command structure is: grep [options] pattern file-list grep (2) • Some of the more useful options that grep provides are: • -c: print a count of the number of lines that match the pattern • -: print out the lines that don't match the pattern • -r: recursively search in the current directory and subdirectory for files that match the pattern grep (3) • So, if we typed the command line: $ grep -r hello *.txt • This grep searches all .txt text files in the current directory and all subdirectories for lines that contain the word hello