cat - Concatenate And Display File Contents

The cat command is used to print the content of one or more files to the standard output(terminal). It can also be used to combine multiple files into one.

Essential Options

OptionsDescription
cat file.txtDisplay the contents of a file.
cat file1.txt file2.txtDisplay contents of multiple files in order
cat file1.txt file2.txt > both.txtCombine both files into a new file
cat -n file.txtNumber all output lines
cat -b file.txtNumber non-blank lines only
cat -s file.txtSqueeze multiple blank lines
cat -E file.txtShow $ at end each line

Example

Use this sample file: notes.txt

Line 1: This is the first line.

Line 2: This line has trailing spaces.    
     
Line 3: This line is followed by multiple blank lines.


Line 4: The end of the sample file.    
  1. Number all lines in notes.txt
cat -n notes.txt
  1. Number only the non-blank notes.txt file
cat -b notes.txt
  1. You have a file notes.txt with many consecutive blank lines. Display it with all multiple blank lines reduced to a single blank line.
cat -s notes.txt
  1. How do you know where the lines ends?
cat -E notes.txt

head | tail - Show The First/Last Lines Of A File

The head command displays the first N lines of a file (default is 10). The tail command displays the last N lines of a file (default is 10).

Example

head file.txt               # first 10 lines
head -n 8 file.txt         # first 8 lines
head -n 2 file.txt          # Only the first 2 lines
tails file.txt               # last 10 lines
tails -n 8 file.txt         # last 8 lines
tails -n 2 file.txt          # Only the last 2 lines

Note: tail -f, this flag is used to show the text files’s live update. Meaning, it follows the files as it grows. AKA, that’s how logging work, you see the last update (recently happened ones).

less - Interactive Pager For Viewing Large Files

the less command allows you to view and scroll through large files interactively, making it easier to navigate logs, reports, or any lengthy text files withtout loading the entire file into memory.

Essential Options

KeyDescription
/patternSearch forward for pattern
n/NGoto next/prev search match
bPage up
fPage down

Examples

  1. View notes.txt and search for “yum”
less notes.txt
# Press -> /yum -> Enter Button
  1. How to page up and down while inside the less view
  • b = Page Up
  • f = Page Down
  1. How to go to next/prev search matches
  • n = Next
  • N = Previous