cat Command in Linux

The cat command in Linux is used to display, combine, and create text files. It's short for “concatenate,” and is one of the most frequently used commands for working with file contents directly from the terminal.

Basic Syntax

cat [options] [filename(s)]

Common Uses

Useful Options

Example

$ cat notes.txt
Linux is fun!
Learning commands.

$ cat -n notes.txt
     1  Linux is fun!
     2  Learning commands.

Best Practices

Use cat for quick reading and file concatenation. For long files, tools like less or more may be better for scrolling. Always redirect output to avoid overwriting unintentionally.

cat Command in Linux – Concatenate and Display File Content

The cat command is one of the most frequently used commands in Linux and UNIX systems. Short for “concatenate,” it is primarily used to read and combine files, displaying their contents on the standard output. It's fast, efficient, and incredibly flexible, making it an essential part of any Linux user’s toolkit.

Basic Syntax

cat [options] [file...]
  

The command works with one or multiple files and outputs the results to the terminal screen unless redirected.

Common Use Cases

Examples of Using the cat Command

1. View a Single File

cat filename.txt

This will output the contents of filename.txt to the terminal.

2. Concatenate Multiple Files

cat file1.txt file2.txt

This command displays the contents of both files, one after the other.

3. Redirect Output to Another File

cat file1.txt file2.txt > combined.txt

Combines the contents of two files into a new file combined.txt.

4. Create a New File with cat

cat > newfile.txt

After executing this command, you can type the file’s contents directly into the terminal. Press Ctrl+D to save and exit.

5. Append to an Existing File

cat additional.txt >> existing.txt

This appends the contents of additional.txt to existing.txt.

Useful Options

Example with Options:

cat -n filename.txt

This will display the file with line numbers.

Tips and Best Practices

cat Command vs. less and more

While cat is ideal for quickly viewing content, less and more allow for paginated viewing:

Use in Scripting

The cat command is frequently used in bash scripts for tasks like:

Security Tips

Common Errors

Alternatives to cat

While cat is popular, you can also use:

The Keywords and Phrases

Final analysis

The cat command is a simple yet powerful Linux utility that every user and developer should understand. Whether you’re viewing logs, merging files, or writing scripts, it serves as a quick go-to solution. Mastering cat not only enhances your command line efficiency but also sets the stage for more advanced shell scripting techniques.

cat Command in Linux – Complete Guide with Examples

The cat command in Linux is one of the most widely used and powerful text-processing utilities. Short for “concatenate,” the cat command is typically used to display file contents, combine multiple files, create new files, and redirect file output. Whether you're a beginner just exploring the Linux terminal or a seasoned sysadmin writing shell scripts, mastering the cat command is essential for efficient file handling in Unix-based systems.

What is cat in Linux?

The cat command is a standard utility used to read, concatenate, and display the contents of text files. It works with standard input and output, making it highly scriptable and compatible with other commands through piping and redirection.

Basic Syntax

cat [options] [file1] [file2] ...

If no filename is given, cat reads from standard input.

1. Display the Contents of a File

cat filename.txt

This prints the content of filename.txt to the terminal window.

2. View Multiple Files Together

cat file1.txt file2.txt

This concatenates and displays the content of both files, one after another.

3. Create a New File Using cat

cat > newfile.txt

Type the content and press Ctrl+D to save and exit.

4. Append Content to an Existing File

cat >> existingfile.txt

This opens the file for appending. Type your content and press Ctrl+D when done.

5. Redirect Output to Another File

cat file1.txt > copy.txt

This copies the contents of file1.txt into copy.txt.

6. Combine Files into One

cat chapter1.txt chapter2.txt > fullbook.txt

Joins multiple files into a single new file.

7. Number All Output Lines

cat -n filename.txt

Adds line numbers to each line of output.

8. Number Non-Empty Lines Only

cat -b filename.txt

Same as -n but ignores blank lines.

9. Show End of Lines

cat -E filename.txt

Displays $ at the end of each line, useful for identifying trailing whitespace.

10. Display Tabs as ^I

cat -T filename.txt

Helps visualize tab characters.

11. Suppress Repeated Empty Lines

cat -s filename.txt

Removes consecutive blank lines and displays only one.

12. Concatenate Binary Files

You can use cat to combine binary files (e.g., media files), but use it cautiously:

cat part1.iso part2.iso > full.iso

13. Use cat with Pipe

Send output of cat to another command:

cat filename.txt | grep "error"

This searches for the word "error" in the file.

14. Use cat in Shell Scripts

cat is often used in bash scripts to read files or write content to them:


#!/bin/bash
cat >> log.txt << EOF
This is a new entry logged on $(date)
EOF
  

15. Read From Standard Input

cat

Type some text and press Ctrl+D to echo it back.

16. Compare cat vs less vs more

cat is great for short files, but for long files use less or more to scroll page-by-page:

less largefile.txt

17. View File Size with cat

cat won’t show file size directly, but you can count bytes using:

cat filename.txt | wc -c

18. Colorize Output with Syntax Tools

Use bat (a better alternative to cat) for syntax-highlighted viewing:

sudo apt install bat
bat script.sh

19. Use cat with Special Devices

You can use cat to interact with system devices and kernel entries:

cat /proc/cpuinfo

This shows your CPU details.

20. Use cat with Here Documents

Here documents allow you to pass multi-line text to a file or command:


cat << EOF > newfile.txt
Line 1
Line 2
EOF
  

21. View Non-Printable Characters

cat -v filename.txt

This helps detect special or problematic characters in files.

22. Use cat to Read Logs

cat is useful for quickly inspecting logs:

cat /var/log/syslog | tail -n 20

23. Combine cat with tee to View and Save

cat config.txt | tee backup.txt

This displays content on screen and saves a copy simultaneously.

24. Common Errors with cat

25. Security Tips

Final analysis

The cat command is one of the most versatile and fundamental tools in the Linux command line toolbox. From viewing text files and joining multiple documents to writing scripts and piping output into other commands, cat is simple yet powerful. By understanding its full capabilities, you can streamline many routine Linux operations and increase productivity in file handling, scripting, and debugging.

Related Topics

See Also