The cat command in Linux is one of the most commonly used and versatile tools available in the Unix-like command-line environment. Short for “concatenate,” cat is primarily used to read, display, and combine text files. Whether you're a seasoned system administrator or a Linux newbie, understanding how to use the cat command effectively can streamline your file management tasks.

What is the cat Command in Linux?

The cat command in Linux allows users to view the contents of files, combine multiple files into one, or even create new text files directly from the command line. It's lightweight, straightforward, and extremely useful when working with text-based files and scripts.

Basic Syntax

The general syntax of the command is:

This command can be used with one or multiple filenames and can be combined with various flags to modify its output or behavior.

Common Uses of cat

Displaying File Content

To view the content of a file in the terminal, you can simply use:

This command displays the entire contents of filename.txt without opening it in a text editor.

Creating a New File

You can use cat to create a new file by typing:

After entering the command, type your content and press Ctrl + D to save and exit.

Appending to a File

To add content to an existing file, use:

This opens the file for appending. Once done, press Ctrl + D again to save.

Concatenating Files

Combine two or more files into one using:

This merges file1.txt and file2.txt into a new file called combined.txt.

Line Numbering

To display file contents with line numbers:

Why Use cat?

The cat command is especially useful when scripting or when you need to quickly inspect files. It's a faster alternative to opening a file in a text editor and can be easily combined with other commands via piping.

Final Thoughts

Mastering the cat command in Linux is an essential step toward efficient file handling on the command line. For an in-depth guide with practical examples and advanced usage, explore the full tutorial at Vultr Docs. Whether you're automating tasks or managing logs, cat is a command-line staple every Linux user should know.