Skip to content

File Content Viewing (cat, less, head, tail)

Category: Linux Command Basics
Type: Linux Commands
Generated on: 2025-07-10 03:05:21
For: System Administration, Development & Technical Interviews


File Content Viewing Cheatsheet (cat, less, head, tail)

Section titled “File Content Viewing Cheatsheet (cat, less, head, tail)”

This cheatsheet covers essential Linux commands for viewing file content: cat, less, head, and tail. It provides practical examples, common options, and advanced usage scenarios for system administrators and developers.

  • cat (concatenate): Displays the entire file content to standard output. Useful for small files or when piping to other commands.

  • less: A pager program that allows you to view large files one screen at a time. Offers navigation, searching, and other features. Preferred for viewing large files as it doesn’t load the whole file into memory at once.

  • head: Displays the first n lines of a file. Useful for quickly checking the file header or first few lines of a log file.

  • tail: Displays the last n lines of a file. Most commonly used for monitoring log files in real-time.

Terminal window
cat [OPTIONS] [FILE]...
less [OPTIONS] [FILE]...
head [OPTIONS] [FILE]...
tail [OPTIONS] [FILE]...

3.1. cat

  • Display the content of a file:

    Terminal window
    cat myfile.txt

    Sample Output (myfile.txt):

    This is the first line.
    This is the second line.
    This is the third line.
  • Concatenate multiple files:

    Terminal window
    cat file1.txt file2.txt file3.txt

    This will print the contents of file1.txt, file2.txt, and file3.txt sequentially to the terminal.

  • Create a new file by concatenating existing files:

    Terminal window
    cat file1.txt file2.txt > newfile.txt

    WARNING: This will overwrite newfile.txt if it already exists. Use >> to append instead (see below).

  • Append to an existing file:

    Terminal window
    cat file1.txt >> existingfile.txt

    This appends the content of file1.txt to the end of existingfile.txt.

3.2. less

  • View a file with less:

    Terminal window
    less largefile.log

    Navigation:

    • Spacebar: Scroll down one page.
    • b: Scroll up one page.
    • j: Scroll down one line.
    • k: Scroll up one line.
    • g: Go to the beginning of the file.
    • G: Go to the end of the file.
    • /pattern: Search for pattern (press n for next, N for previous).
    • q: Quit less.
  • View output from a command with less:

    Terminal window
    ls -l /etc | less

3.3. head

  • Display the first 10 lines of a file:

    Terminal window
    head myfile.txt

    Sample Output (myfile.txt - first 3 lines):

    This is the first line.
    This is the second line.
    This is the third line.
  • Display the first 5 lines of a file:

    Terminal window
    head -n 5 myfile.txt
    head -5 myfile.txt # Shorthand
  • Display the first few bytes of a file (rarely used):

    Terminal window
    head -c 10 myfile.txt # Display the first 10 bytes

3.4. tail

  • Display the last 10 lines of a file:

    Terminal window
    tail myfile.txt

    Sample Output (myfile.txt - last 3 lines):

    This is the last third line.
    This is the last second line.
    This is the last line.
  • Display the last 5 lines of a file:

    Terminal window
    tail -n 5 myfile.txt
    tail -5 myfile.txt # Shorthand
  • Follow a log file in real-time (most common use):

    Terminal window
    tail -f /var/log/syslog

    This will continuously display new lines added to the /var/log/syslog file. Press Ctrl+C to stop following.

  • Follow a log file and display more context:

    Terminal window
    tail -n 20 -f /var/log/nginx/access.log

    This will display the last 20 lines and then follow the log file in real-time.

  • Multiple files with tail:

    Terminal window
    tail -f file1.log file2.log

    This will follow both file1.log and file2.log, prefixing each line with the filename.

CommandOptionDescriptionExample
cat-nNumber all output lines.cat -n myfile.txt
cat-bNumber non-blank output lines.cat -b myfile.txt
cat-sSuppress repeated empty output lines.cat -s myfile.txt
less-NDisplay line numbers.less -N myfile.txt
less-SChop long lines instead of wrapping.less -S myfile.txt
less+/patternStart less at the first occurrence of pattern.less +/error logfile.txt
head-n / -Number of lines to display. Default is 10.head -n 20 myfile.txt or head -20 myfile.txt
head-cNumber of bytes to display.head -c 50 myfile.txt
tail-n / -Number of lines to display. Default is 10.tail -n 20 myfile.txt or tail -20 myfile.txt
tail-fFollow the file as new content is added.tail -f /var/log/syslog
tail-FFollow the file by name, even if it’s rotated. More robust than -f.tail -F /var/log/nginx/access.log
tail+NUMOutput beginning with line NUM, not from the end.tail +5 myfile.txt (lines 5 to end)
  • Using cat with grep to find specific lines:

    Terminal window
    cat myfile.txt | grep "error"

    This displays only the lines in myfile.txt that contain the word “error”.

  • Using tail with grep to monitor log files for specific events:

    Terminal window
    tail -f /var/log/nginx/error.log | grep "php"

    This monitors the Nginx error log and displays only lines containing “php”.

  • Combining head and tail to extract a specific range of lines:

    Terminal window
    head -n 100 myfile.txt | tail -n 10

    This extracts lines 91 to 100 from myfile.txt. First, head gets the first 100 lines, then tail gets the last 10 of those 100.

  • Using less to search for multiple patterns:

    Terminal window
    less myfile.txt
    /pattern1
    n # Find next occurrence of pattern1
    /pattern2
    n # Find next occurrence of pattern2
  • Following multiple log files with tail and using grep:

    Terminal window
    tail -f /var/log/apache2/access.log /var/log/apache2/error.log | grep -E 'error|warning'

    This command follows both Apache log files and uses grep with extended regular expressions (-E) to filter for lines containing either “error” or “warning”. This is very useful for real-time monitoring of potential issues.

  • Filtering out unwanted data with grep -v:

    Terminal window
    tail -f /var/log/nginx/access.log | grep -v "favicon.ico"

    This follows the Nginx access log but filters out lines containing “favicon.ico”, which are often irrelevant and clutter the output.

  • Use less for viewing any file larger than a few hundred lines. It’s more efficient than cat.

  • Use tail -F instead of tail -f for log files that are rotated. tail -F will continue to follow the log even after it’s been renamed or moved.

  • Remember to use Ctrl+C to exit tail -f or less.

  • Use aliases to shorten frequently used commands:

    Terminal window
    alias tlf='tail -F' # tail -F
    alias l='less -N -S' # less with line numbers and chop long lines

    Add these lines to your .bashrc or .zshrc file to make them permanent.

  • Check file size before using cat:

    Terminal window
    ls -l myfile.log

    If the file is very large (e.g., gigabytes), avoid using cat. Use less or head/tail instead.

  • Use set +m to prevent background jobs from printing to the terminal when using tail -f and disconnecting from SSH. This is useful when monitoring logs on a remote server and disconnecting without stopping the tail command.

  • cat displays binary data: The file is likely a binary file, not a text file. Use a hex editor (e.g., hexdump, xxd) to view the binary data.
  • tail -f doesn’t show new lines:
    • The application may not be writing to the log file, or the log file may be buffered.
    • Try flushing the buffers of the application writing to the log file.
    • Ensure you have permissions to read the file.
  • less displays strange characters: The file may have an unsupported character encoding. Try specifying the encoding with the -r option (e.g., less -r myfile.txt).
  • Permission denied: Ensure you have read permissions for the file. Use ls -l to check permissions.
  • tail -F stops working after log rotation: The application rotating the logs may not be using the correct method. Ensure it’s either using copytruncate or properly signaling the process to reopen the log file.
  • grep: Search for patterns within files.
  • wc: Count lines, words, and characters in a file.
  • find: Search for files based on various criteria.
  • sed: Stream editor for text manipulation.
  • awk: Pattern scanning and processing language.
  • echo: Display a line of text. Useful for creating small files.
  • tee: Read from standard input and write to standard output and files. Useful for both displaying and saving output. Example: cat myfile.txt | tee output.txt
  • journalctl: View and manage systemd journal logs (for systemd-based systems).

This cheatsheet provides a comprehensive overview of the cat, less, head, and tail commands, covering both basic and advanced usage scenarios. Remember to adapt the examples to your specific needs and always be cautious when using commands that can modify files.