Reading and manipulating files
Goals By the end of this lesson you will be able to Read files without using text editors Access specific parts of files Count the number of words and lines in a file Sort data within files Examine the diference between files Determine what type a file is Redirect input and output with respect to files
Types of files There are multiple types of files Text files contain human-readable text Binary files mainly consist of encoded machine code that is not readable by humans Most commands for reading files in Linux can or should only be used on text files
Displaying a file Three primary commands: cat filename dump file to terminal (without filename, just echos what you type ctrl-c to exit) more filename page-by-page look at file Enter to go down a little Space to go down a page b to go up q or ctrl-c to exit less filename page-by-page look at file Similar to above, but more intuitive keys q to exit
Displaying beginning & end of file Two commands head filename show first 10 lines of file tail filename show last 10 lines of file Other options exist (use the man page!), but the most useful is -n to change from the default number of lines
File diferences diff filea fileb can be used to find the diferences in files No output indicates the files are identical If output is generated, it will provide details about the specific lines Options exist for: Displaying diferences side-by-side instead of sequentially Ignoring diferences in whitespace and case Many other things You can also provide two directories to compare files within
Counting lines, words, and bytes wc filename displays the number of lines, words, and bytes, in that order, for a file Lines are delimited by newline characters Words are delimited by spaces Bytes are delimited by characters If you don t need all that information, options are useful Don t forget the wildcard * use it to count all the lines in the current directory
File information file filename will tell you what type of file it is Not guaranteed to be accurate, but usually is Want to know how it works? Check the man page
touch touch filename will do two primary things: If filename does not exist, it will create the file and it will be empty If filename does exist, it will update the access & modification time to the current time touch can also be used to change the access and modification times individually as well as set them to any arbitrary time
Sorting a text file sort filename will display the file with lines sorted By default lines are sorted according to the first character (if they match, it compares the second character, etc.) Numbers come before letters Modify the sort using options to sort in reverse Use -o option to output to a file sort inputfile -o outputfile (these can be the same!) You can give multiple input files and it will sort all of the data: sort filea fileb
Redirecting output to a file If want to have output go to a file instead of to the terminal you can redirect the output using > (angle bracket or greater than) Example: ls -l > filelist.txt cat filea > fileb (efectively cp) sort file > output (efectively sort file -o output)./a.out > output cat > filename (use ctrl-c to exit and save) Use >> to append to the file instead
Redirecting input If a command or program would typically require input from the keyboard you can redirect input from a file instead using < It s somewhat common to use this to provide input to a program that you write, either for general use or testing purposes if lots of keyboard input would be cumbersome to type out Another option for taking input from a file into a program you write is to open & process the file from within the program
Redirecting output to a command/program We ve investigated redirecting output to a file, but what if we d rather have the output of one command/program to the input of another command/program? Piping with the character will accomplish this It is typically above the enter key on your keyboard Examples: Count the number of characters in a directory listing (ls and wc) Examine a long directory listing one page at a time (ls and less)
Exploration When is it better/easier to use cat versus less? Examine various files using file Become familiar with redirecting input and output as well as piping these are critical tools for efectively using Linux and programming