Menu

cat command in Linux – Mastering the ‘cat’ command in Linux

Written by Jagdeesh | 2 min read

Introduction

Linux, being one of the most powerful and flexible operating systems, offers a wide range of commands for users to efficiently manage and navigate files. One such essential and versatile command is ‘cat’, short for ‘concatenate’. In this blog post, we’ll dive deep into the ‘cat’ command, exploring its various use cases and providing examples to help you become a master at using it.

1. Displaying the Contents of a File

The most basic use of the ‘cat’ command is to display the contents of a file. The syntax is pretty straightforward:

python
cat filename

For example, to display the contents of a file named ‘example.txt’, simply run:

python
cat example.txt

2. Concatenating Multiple Files

As the name suggests, the ‘cat’ command can be used to concatenate multiple files and display their contents as a single output. Here’s the syntax:

python
cat file1 file2 ... fileN

For example, to concatenate and display the contents of two files named ‘file1.txt’ and ‘file2.txt’, run:

python
cat file1.txt file2.txt

3. Creating a New File

You can use the ‘cat’ command to create a new file and write content to it. The syntax is as follows:

python
cat > new_file

For example, to create a new file named ‘new_file.txt’, run:

python
cat > new_file.txt

After executing the command, you can start typing the contents for the new file. To save and exit, press ‘Ctrl + D’.

4. Appending File Contents

The ‘cat’ command can also be used to append the contents of one or more files to another file. Here’s the syntax:

python
cat file1 file2 ... fileN >> target_file

For example, to append the contents of ‘file1.txt’ and ‘file2.txt’ to ‘target_file.txt’, run:

python
cat file1.txt file2.txt >> target_file.txt

5. Displaying Line Numbers

To display line numbers alongside the contents of a file, use the ‘-n’ option with the ‘cat’ command. The syntax is:

python
cat -n filename

For example, to display line numbers while displaying the contents of ‘example.txt’, run:

python
cat -n example.txt

6. Suppressing Repeated Empty Lines

To suppress the output of multiple consecutive empty lines into a single empty line, use the ‘-s’ option with the ‘cat’ command. The syntax is:

python
cat -s filename

For example, to display the contents of ‘example.txt’ with suppressed repeated empty lines, run:

python
cat -s example.txt

Conclusion

The ‘cat’ command is a simple yet powerful tool for managing files in Linux. From displaying and concatenating file contents to creating new files, appending contents, and formatting output, the ‘cat’ command has got you covered. With the examples provided in this blog post, you should now have a strong foundation for using the ‘cat’ command effectively. Happy coding!

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master Linux — Hands-On
Join 5,000+ students at edu.darkorange-mallard-189514.hostingersite.com
Explore Course
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science