Menu

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

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:

cat filename

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

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:

cat file1 file2 ... fileN

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

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:

cat > new_file

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

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:

cat file1 file2 ... fileN >> target_file

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

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:

cat -n filename

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

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:

cat -s filename

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

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!

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