How to Use the Cat Command in Linux

Updated on 22 May, 2025
How to Use the Cat Command in Linux header image

The cat command, short for "concatenate," is one of the most fundamental utilities in Unix-like operating systems. First introduced in Version 1 Unix (1971), it comes pre-installed on nearly all Linux distributions. While it is most commonly used to display file contents in the terminal, it also supports merging files, creating or appending content, and redirecting output, making it a versatile part of everyday shell scripting and file manipulation.

This article explains how to use the cat command in a variety of practical scenarios. It covers common use cases like viewing files, joining multiple files, redirecting input and output, numbering lines, and working with pipes. You'll also explore useful flags and options to make your usage of cat more efficient and adaptable across different environments.

Prerequisites

Before you begin, ensure you have:

  • A Linux instance running Ubuntu or another supported distribution.
  • Access to the instance via SSH.
  • A non-root user with sudo privileges.
  • A text editor like nano to create and modify configuration files.

cat Command Options

The cat command is a powerful and flexible utility in Linux. It allows you to view, concatenate, and manipulate file contents. The name cat stands for concatenate, reflecting its original purpose of merging files into a single output stream. While many users rely on cat to display files in the terminal, its options enable a variety of additional use cases, such as formatting, debugging, and stream editing.

This section explains the command syntax, describes common flags, and demonstrates practical examples.

Command Syntax

The cat command derives from the word concatenate, emphasizing its design to merge file contents into a single output. While concatenation remains its core functionality, cat is commonly used to display files, inspect content, and pipe data between commands.

The general syntax of the cat command is:

cat [options] [file(s)]
  • [options]: Flags that modify cat's behavior. Use these to change output formatting or processing logic.

  • [file(s)]: One or more files to process. If omitted, cat reads from standard input.

When you run cat without arguments, it enters interactive mode. Type input and press Ctrl + D to end. This behavior is useful for quick notes or piping input manually.

Summary of Common Options

The table below summarizes the most commonly used cat options.

Option Description Example
-b Number non-empty output lines cat -b example.txt
-s Squeeze multiple blank lines cat -s example.txt
-E Show $ at end of each line cat -E example.txt
-T Show tabs as ^I cat -T tabbed.txt
-A Equivalent to -vET cat -A example.txt
-v Show non-printing characters cat -v ctrlc.txt

Demonstrating Selected Options

The cat command supports several options that enhance its functionality. Below examples show how they work in practice.

Prepare the Test File

  1. Create and open a file named example.txt:

    console
    $ nano example.txt
    
  2. Add the following content to the file:

    Hello,  everyone!
    
    Greetings from Vultr!
  3. Save and exit using Ctrl + X, then press Y and Enter.

Try These cat Options

  • -b: Numbers only non-empty lines. Useful for code or logs where spacing matters less than content.

    console
    $ cat -b example.txt
    

    Output:

    1  Hello,  everyone!
    
    2  Greetings from Vultr!!
  • -s: Squeezes multiple consecutive blank lines into a single one. Helps tidy up cluttered files.

    console
    $ cat -s example.txt
    
  • -E: Adds a $ at the end of each line to highlight line endings or trailing whitespace.

    console
    $ cat -E example.txt
    

    Output:

    Hello!$
    $
    Greetings from Vultr!$
  • -T: Shows tab characters as ^I, making it easier to spot indentation issues. You can create a file with tabs like this:

    console
    $ echo -e "Hello,\tWorld!" > tabbed.txt
    $ cat -T tabbed.txt
    

    Output:

    Hello,^IWorld!
  • -A: Combines -E, -T, and -v to show line endings, tabs, and non-printable characters all at once.

    console
    $ cat -A example.txt
    

    Output:

    Hello,^Ieveryone!$
    $
    Greetings from Vultr!$
  • -v: Reveals non-printing characters like control codes. For example:

    console
    $ echo $'\x03' > ctrlc.txt
    $ cat -v ctrlc.txt
    

    Output:

    ^C

Display Files Using the cat Command

The cat command is most commonly used to display the contents of files directly in the terminal. It's a simple and fast way to view text files such as logs, scripts, or configuration files without opening a text editor. This section demonstrates how to display single files, multiple files, and efficiently view large files.

  1. Create a file named tutorial.txt with a single line of content:

    console
    $ echo "Learn cat commands with Vultr!" > tutorial.txt
    
  2. Display the contents of tutorial.txt:

    console
    $ cat tutorial.txt
    

    Output:

    Learn cat commands with Vultr!
  3. Display multiple files by listing them after the cat command. For example, to view example.txt followed by tutorial.txt:

    console
    $ cat example.txt tutorial.txt
    

    Output:

    Hello,  everyone!
    
    Greetings from Vultr!
    Learn cat commands with Vultr!

    The display order depends on the sequence of file names. To reverse it, use the tac command instead of cat. The tac command is simply cat spelled backward. It displays files line-by-line in reverse order.

  4. Use a wildcard (*) to display all .txtfiles in the current directory:

    console
    $ cat *.txt
    

    To read .txt files from a specific directory, you can specify the path to the directory before the wildcard character.

    console
    $ cat /path/to/directory/*.txt
    

    Replace /path/to/directory/ with the actual file path.

  5. To view large files without flooding the terminal, combine cat with other commands using pipes.

    1. Pipe cat with head to read from the top of the file. For example, to read the first 20 lines of a file named largefile.txt :

      console
      $ cat largefile.txt | head -n 20
      
    2. Similarly, pipe cat with tail to read from the end of the file.

      console
      $ cat largefile.txt | tail -n 20
      

      Replace largefile.txt with a large file of your choice.

    3. You can also use the less command to view the contents in a scrollable window.

      console
      $ cat tutorial.txt | less
      

Combine Files in Linux using cat

True to its "concatenate" namesake, cat excels at merging files into unified outputs. This capability is invaluable for log analysis, configuration management, and batch text processing. Explore workflows ranging from basic merging to advanced stream manipulation below:

  1. Merge two files into a single file. The content would be arranged in the new file in order of how the files were listed using the cat command. In this example, merge example.txt to tutorial.txt and write to result.txt.

    console
    $ cat example.txt tutorial.txt > result.txt
    

    Verify the content of result.txt.

    console
    $ cat result.txt
    

    Output:

    Hello,  everyone!
    
    Greetings from Vultr!
    Learn cat commands with Vultr!

    The > redirects the merged output to the new file result.txt while keeping the original files intact.

  2. You can combine files using the wildcard (*) to select multiple files and save them into a new file. For example, to combine all files in the directory that end with .txt into result2.txt:

    console
    $ cat *.txt > result2.txt
    

    This would also create the file result2.txt if it doesn't exist. To verify the content of result2.txt:

    console
    $ cat result2.txt
    

    Output:

    Hello,  everyone!
    
    Greetings from Vultr!
    Hello,  everyone!
    
    Greetings from Vultr!
    Learn cat commands with Vultr!
    Learn cat commands with Vultr!
  3. You can also insert separators or any text between files you want to combine. For example, you can insert a divider line between example.txt and tutorial.txt as they appear in the new file result3.txt.

    console
    $ (cat example.txt; echo -e "\n----- DIVIDER -----\n"; cat tutorial.txt) > result3.txt
    

    To verify the content of result3.txt:

    console
    $ cat result3.txt
    

    Output:

    Hello,  everyone!
    
    Greetings from Vultr!
    
    ----- DIVIDER -----
    
    Learn cat commands with Vultr!

Redirect Input to Files using cat

While cat is typically used to display file contents, it can also accept input directly from the terminal and write it to a file. This makes cat a quick way to create or append content without using a text editor, especially useful on headless systems.

  1. Create a new file named file1.txt by redirecting input interactively:

    console
    $ cat > file1.txt
    

    Type the following content when prompted:

    Web Server: Nginx  
    OS: Ubuntu 22.04 

    Press Ctrl + D to save and exit.

  2. Verify the contents of file1.txt:

    console
    $ cat file1.txt
    

    Output:

    Web Server: Nginx
    OS: Ubuntu 24.04
  3. Append content to file1.txt using the >> redirection operator:

    console
    $ cat >> file1.txt
    

    When prompted, add the following line:

    Service Provider: Vultr

    Press Ctrl + D to finish. Then verify the updated contents:

    console
    $ cat file1.txt
    

    Output:

    Web Server: Nginx
    OS: Ubuntu 24.04
    Service Provider: Vultr
  4. Write multi-line templates using a here-document (<<EOF):

    console
    $ cat <<EOF > file2.txt
             ServerName: Vultr_Demo
             DocumentRoot: /var/www/html
       EOF
    
  5. View the contents of file2.txt:

    console
    $ cat file2.txt
    

    Output:

    ServerName: Vultr_Demo
    DocumentRoot: /var/www/html
  6. Inject shell variables into files using cat. First, store the current date in a variable:

    console
    $ CURRENT_TIME=$(date)
    
  7. Append the timestamp to file2.txt using here-strings (<<<):

    console
    $ cat <<< "Timestamp: $CURRENT_TIME" >> file2.txt
    

    You can use cat <<< "text" to mimic echo, but echo is more efficient for single-line output. This example uses cat to stay consistent with the rest of the article.

  8. Verify the final contents of file2.txt:

    ServerName: Vultr_Demo
    DocumentRoot: /var/www/html
    Timestamp: Wed Apr 16 18:13:32 GMT 2025

Add Line Numbers to Files using cat

Line numbering is essential for debugging scripts, analyzing logs, or collaborating on code. The cat command provides two streamlined options (-n and -b) to add line numbers directly in the terminal, eliminating the need for external tools. This section demonstrates how to implement both methods and their practical applications in system administration and development workflows.

Number All Lines with -n

  1. Display the contents of /etc/hosts with line numbers:

    console
    $ cat -n /etc/hosts
    

    Output:

    1  127.0.0.1       localhost
    2  127.0.1.1       ubuntu-server
    3  
    4  # IPv6 entries
    5  ::1     ip6-localhost ip6-loopback
  2. Save a timestamped copy of /etc/os-release with line numbers:

    console
    $ cat -n /etc/os-release > os_snapshot_$(date +%F).txt
    
  3. Verify the new file:

    1  NAME="Ubuntu"
    2  VERSION="22.04.3 LTS (Jammy Jellyfish)"
    3  ID=ubuntu
    4  ID_LIKE=debian

Number Only Non-Empty Lines with -b

Adding line numbers by combining cat with the option -b. The -b option numbers only non-empty lines, which is useful for focusing on meaningful content in files with many blank lines, such as configuration files or sparse logs.

  1. Print /etc/ssh/ssh_config while ignoring empty lines:

    console
    $ cat -b /etc/ssh/ssh_config
    

    Output:

    1 # This is the ssh client system-wide configuration file. See
    2 # ssh_config(5) for more information. This file provides defaults for
    3 # users, and the values can be changed in per-user configuration files
    4 # or on the command line.

Combine Line Numbering with Pipes

You can combine cat -n or cat -b with other tools for advanced filtering and formatting.

  1. Search numbered lines for error messages in syslog.

    console
    $ sudo cat -n /var/log/syslog | grep "error"
    

    Example Output:

    1452  Mar  1 14:22:01 ubuntu-server cron[1234]: ERROR: Failed to start job  
    1789  Mar  1 14:35:01 ubuntu-server kernel: [UFW BLOCK] IN=eth0 SRC=203.0.113.5  
  2. Extract specific lines from example.txt using awk:

    console
    $ cat -n example.txt | awk 'NR==1 || NR==3 {print "Line " $1 ": " $0}'
    

    Output:

    Line 1:      1  Hello, everyone!
    Line 2:      2 
    Line 3:      3  Greetings from Vultr!

Conclusion

In this article, you explored how to use the cat command to view, merge, and manipulate file content on Linux systems. You learned how to display files, redirect input and output, append content, inject variables, and add line numbers—both interactively and through automation-friendly scripts.

Whether you're managing logs, writing scripts, or debugging configuration files, cat remains a reliable and essential utility in every Linux user's toolkit. For more advanced use cases, refer to the Linux cat man page.