How to Use Cat Command in Linux? [Explained with Examples]

Ayushi Trivedi 03 Apr, 2024 • 3 min read

The cat command stands as a robust tool in Linux, empowering users to effortlessly create, view, and concatenate files. It holds a pivotal role in the toolkit of any Linux user, offering a pathway to heightened productivity. You can also learn about Linux file systems here. This blog delves into the multifaceted use cases of the cat command, providing clear examples to facilitate a profound understanding of its effective utilization.

What is Cat Command?

In Linux-like operating systems, the ‘cat’ command, which stands for “concatenate,” is a useful tool for displaying the contents of one or more files sequentially. Although it can be used to concatenate multiple files and display the aggregate output, it is most usually used to view the contents of text files. `cat` can also be used to add material to already-existing files or to create new ones.

Basic Syntax

cat [options] [file(s)]
  • cat: The command name.
  • [options]: Optional flags to modify the command’s behavior (see below).
  • [file(s)]: One or more file names to read. If no files are specified, cat reads from standard input (usually the keyboard).

Common Options

  • -n: Numbers each line of output.
  • -b: Numbers only non-blank lines.
  • -s: Squeezes consecutive blank lines into a single newline.
  • -v: Displays non-printing characters (e.g., tabs, newlines) as visible symbols.
  • -E: Displays a dollar sign ($) at the end of each line.
  • -T: Displays tab characters as ^I.

Also Read: Start Using Crontab In Linux: Syntax Tutorial

Practical Implementation

1. Viewing File Contents

Quickly display the contents of a text file:

cat my_file.txt

2. Creating New Files

Create a new file and input text directly:

cat > new_file.txt

(Type text, then press Ctrl+D to save and exit.)

3. Combining Files

Concatenate multiple files into a single file:

cat file1.txt file2.txt file3.txt > combined_file.txt

4. Appending to Files

Add content to the end of an existing file:

cat new_content.txt >> existing_file.txt

5. Numbering Lines

Display a file’s content with numbered lines:

cat -n code.py

6. Viewing Non-Printing Characters

Reveal hidden characters like tabs and newlines

cat -v configuration.txt

7. Piping to Other Commands

Send file contents to other commands for further processing:

cat log_file.txt | grep "error"

8. Creating Temporary Files

Generate a quick temporary file for testing or scripts:

cat > temp_file.txt << EOF

>This is some temporary content.

>EOF

9. Viewing System Information

Read from system devices or files representing hardware:

cat /proc/cpuinfo  # View CPU information

cat /dev/random   # Generate random data

Also Read: Started with Linux File System

Conclusion

This blog has meticulously navigated through the diverse applications of the cat command in Linux. From viewing file contents to concatenating files and creating new ones, the versatility of the cat command has been unveiled. By mastering this command, Linux users can significantly enhance their productivity, making it an indispensable asset in their toolkit.

Key Takeaways

  • The cat command serves as a versatile tool for viewing, concatenating, and creating files in Linux.
  • Utilize cat to effortlessly display file contents in the terminal.
  • Leverage the command for concatenating the contents of multiple files into a single, cohesive file.

Frequently Asked Questions?

Q1. How to cat Linux into file?

A. To cat Linux into a file: You can use the cat command followed by the redirection operator (> or >>) to send the output of a command or file into another file. For example, to create a new file named “output.txt” containing the contents of “input.txt”, you would use:
cat input.txt > output.txt

Q2. What is cat symbol in Linux?

A. The cat symbol in Linux: In Linux, the > symbol is used as a redirection operator with the cat command to redirect the output of a command or file into another file. It is used to overwrite the contents of the target file. The >> symbol is also used for redirection but to append the output to the end of the target file.

Q3. What is the cat command in C?

A. Cat command in C: In C programming, there isn’t a direct equivalent to the cat command. However, you can achieve similar functionality by using file input/output functions such as fopen, fread, and fwrite to read the contents of a file and write them to another file.

Q4. How do I edit a file in cat command?

A. Editing a file with cat command: The cat command is primarily used to display the contents of files, not to edit them directly. However, you can use cat in combination with other commands or text editors to achieve editing tasks indirectly. For example, you can use cat to display the contents of a file, make modifications in a text editor, and then save the changes.

Ayushi Trivedi 03 Apr 2024

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers