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

Ayushi Trivedi Last Updated : 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.

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details