• "cat" simply takes whatever is in a file and displays it on the screen or adds it to another file.


  • "echo" lets you type something, and then it's echoed to the screen, or added to a file.


  • "head" and "tail" show the beginning or the end of files.


  • "sed" stands for stream editor that's used to substitute strings within files.


  • And "awk" is a Swiss Army knife that can do a lot of things in any file that has a series of fields in it.


  • It can extract certain columns, do manipulations, print things out and do all sorts of things.


  • So, we're going to discuss all of these and other utilities in quite a bit of detail.


  • cat is short for concatenate and is one of the most frequently used Linux command line utilities. It is often used to read and print files, as well as for simply viewing file contents. To view a file, use the following command:


  •  
    $ cat <filename>
         
    
  • For example, cat readme.txt will display the contents of readme.txt on the terminal.


  • However, the main purpose of cat is often to combine (concatenate) multiple files together.


  • You can perform the actions listed in the table using cat.


  • The tac command (cat spelled backwards) prints the lines of a file in reverse order.


  • Each line remains the same, but the order of lines is inverted. The syntax of tac is exactly the same as for cat, as in:


  •  
    $ tac file
    $ tac file1 file2 > newfile
     
    
    Command Usage
    cat file1 file2 Concatenate  multiple files and display the output; i.e. the entire content of the  first file is followed by that of the second file
    cat file1 file2 > newfile Combine multiple files and save the output into a new file
    cat file >> existingfile Append a file to the end of an existing file
    cat > file Any subsequent lines typed will go into the file, until Ctrl-D is typed
    cat >> file Any subsequent lines are appended to the file, until Ctrl-D is typed
  • cat can be used to read from standard input (such as the terminal window) if no files are specified.


  • You can use the > operator to create and add lines into a new file, and the >> operator to append lines (or files) to an existing file.


  • We mentioned this when talking about how to create files without an editor.


  • To create a new file, at the command prompt, type cat > <filename> and press the Enter key.


  • This command creates a new file and waits for the user to edit/enter the text. After you finish typing the required text, press Ctrl-D at the beginning of the next line to save and exit the editing.


  • Another way to create a file at the terminal is cat > <filename> << EOF. A new file is created and you can type the required input. To exit, enter EOF at the beginning of a line.


  • Note that EOF is case sensitive. One can also use another word, such as STOP.


  • cat command iteratively
  • echo displays (echoes) text. It is used simply, as in:


  •  
    $ echo string
     
    
  • echo can be used to display a string on standard output (i.e. the terminal) or to place in a new file (using the > operator) or append to an already existing file (using the >> operator).


  • The –e option, along with the following switches, is used to enable special character sequences, such as the newline character or horizontal tab.


  • \n represents newline


  • \t represents horizontal tab


  • echo is particularly useful for viewing the values of environment variables (built-in shell variables). For example, echo $USERNAME will print the name of the user who has logged into the current terminal.


  • The following table lists echo commands and their usage:


  • Command Usage
    echo string > newfile The specified string is placed in a new file
    echo string >> existingfile The specified string is appended to the end of an already existing file
    echo $variable The contents of the specified environment variable are displayed
  • Now, you often need to work with configuration files, text files, documentation files, log files.


  • These may become very large, quite a few megabytes or even gigabytes in size.


  • It's difficult to work with such a large file if you just want to look at a small part of it with normal editors and other viewing kinds of programs.


  • Here's an example that you can consider, a banking system which keeps a record of all the transactions at an ATM machine.


  • Suppose you just need to look at one particular record in that file.


  • Opening and edit may be problematic.


  • It may take a long time to load, it may have to load the entire file into memory, or it may just be very impractical.


  • So, instead, one can use, the "less" program, which lets you page through the file, scrolling through it and looking for particular lines or strings that you need without having to look at everything.


  • And it's very easy to just do that by saying "less somefile".


  • There's an older program called "more", which does sort of the same thing, but "less" has more capabilities and options and very few people use "more" anymore.


  • You can also "cat somefile" into "less" with the "pipe" command and that's exactly the same as just doing "less somefile".


  • So, that's just one of many ways to work with large text files.


  • head reads the first few lines of each named file (10 by default) and displays it on standard output. You can give a different number of lines in an option.


  • For example, if you want to print the first 5 lines from grub.cfg, use the following command:


  •  
    $ head –n 5 grub.cfg
     
    
  • You can also just say head -5 grub.cfg.


  • cat command iteratively
  • tail prints the last few lines of each named file and displays it on standard output.


  • By default, it displays the last 10 lines. You can give a different number of lines as an option.


  • tail is especially useful when you are troubleshooting any issue using log files, as you probably want to see the most recent lines of output.


  • For example, to display the last 15 lines of somefile.log, use the following command:


  •  
    $ tail -n 15 somefile.log
     
    
  • You can also just say tail -15 somefile.log.


  • To continually monitor new output in a growing log file:


  •  
    $ tail -f somefile.log
     
    
  • This command will continuously display any new lines of output in somefile.log as soon as they appear. Thus, it enables you to monitor any current activity that is being reported and recorded.


  • tail command iteratively