Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux cat Command: Concatenate Files And Print Output To Standard Output Device

The cat command in Linux (short for "concatenate") is a utility used to read, display, and concatenate the contents of files. In this tutorial, we will discuss how to use the cat command effectively, along with various options and examples.

  1. Basic usage of the cat command:

    To display the contents of a file, use the cat command followed by the name of the file:

    cat file.txt
    

    This command will display the contents of file.txt in the terminal.

  2. Displaying multiple files:

    You can display the contents of multiple files by providing multiple file names:

    cat file1.txt file2.txt file3.txt
    

    This command will display the contents of file1.txt, file2.txt, and file3.txt one after the other.

  3. Concatenating files:

    The cat command can be used to concatenate multiple files into a single file. To do this, provide multiple input file names and use a redirection operator to specify the output file:

    cat file1.txt file2.txt > output.txt
    

    This command will concatenate the contents of file1.txt and file2.txt into a new file named output.txt. If output.txt already exists, its contents will be overwritten.

  4. Appending files:

    If you want to append the contents of one or more files to an existing file, use the >> redirection operator:

    cat file1.txt >> output.txt
    

    This command will append the contents of file1.txt to output.txt. If output.txt does not exist, it will be created.

  5. Displaying line numbers:

    To display line numbers along with the file contents, use the -n or --number option:

    cat -n file.txt
    

    This command will display the contents of file.txt with line numbers.

  6. Squeezing blank lines:

    If a file has multiple consecutive blank lines, you can use the -s or --squeeze-blank option to reduce them to a single blank line:

    cat -s file.txt
    

    This command will display the contents of file.txt with consecutive blank lines squeezed into a single blank line.

  7. Displaying non-printable characters:

    To display non-printable and whitespace characters, use the -v, -t, and -e options:

    • -v or --show-nonprinting: Displays non-printable characters.
    • -t or --show-tabs: Displays TAB characters as ^I.
    • -e: Appends a $ character to the end of each line, making it easier to see line breaks.
    cat -vte file.txt
    

    This command will display the contents of file.txt with non-printable characters, TAB characters, and line breaks visible.

By following this tutorial, you should now have a good understanding of how to use the cat command in Linux for reading, displaying, and concatenating files. With various options and the ability to handle multiple files, the cat command is a versatile tool for managing file contents in Linux.

  1. How to use cat command in Linux:

    • Display the contents of a file using the cat command.
    cat file.txt
    
  2. Concatenating files with cat in Linux:

    • Combine and display the contents of multiple files.
    cat file1.txt file2.txt > combined.txt
    
  3. Print multiple files with cat command:

    • Print the contents of multiple files to the terminal.
    cat file1.txt file2.txt
    
  4. Appending files with cat in Linux:

    • Append the contents of one file to another.
    cat file1.txt >> file2.txt
    
  5. Redirecting cat output to a file:

    • Save the output of cat to a new file.
    cat file.txt > newfile.txt
    
  6. Displaying line numbers with cat:

    • Show line numbers while displaying file contents.
    cat -n file.txt
    
  7. Using cat to create and view text files:

    • Create and view the contents of a new text file using cat.
    cat > newfile.txt
    
  8. Concatenating non-text files with cat:

    • Combine and display the contents of binary or non-text files.
    cat image1.jpg image2.jpg > combined.jpg
    
  9. Removing duplicate lines with cat:

    • Use cat with sort to remove duplicate lines in a file.
    cat file.txt | sort -u
    
  10. Concatenate and create new files with cat command:

    • Combine files and create a new one using cat.
    cat file1.txt file2.txt > newfile.txt