cat: The Concatenate CommandThe cat command, short for concatenate, is one of the most fundamental tools in the Unix/Linux command-line toolbox. It’s widely used to read, display, and combine the contents of files, either for quick inspection or for more complex tasks when integrated with other utilities. Lightweight and versatile, cat is indispensable for system administrators, developers, and power users alike.
For me, cat is like a utility knife—it’s simple, reliable, and often the first thing I reach for when I need to interact with files. Whether checking logs, merging files, or passing data to other commands, cat is a critical part of my workflow.
cat?cat is included in virtually every Unix-like system, ensuring it’s always available.cat operates directly within the terminal, making it lightweight and fast.cat is a cornerstone of many more complex workflows.cat [OPTIONS]... [FILE]...
Display the Contents of a File
cat filename
Example:
cat /etc/hosts
View Multiple Files
cat file1 file2
This outputs the content of file1 followed by file2.
Show Line Numbers
cat -n filename
Adds line numbers to the output, useful for debugging scripts or configuration files.
Quickly Create a File
cat > newfile
Type your content directly into the terminal and press Ctrl + D to save.
Append to an Existing File
cat >> existingfile
Add content to an existing file. Press Ctrl + D to exit.
Merge two or more files into one:
cat file1 file2 > combinedfile
This creates a new file, combinedfile, containing the content of both files.
Redirect Output
cat file1 > newfile
Overwrites newfile with the contents of file1.
Append Output
cat file1 >> existingfile
Adds file1’s content to existingfile.
Pipe Output
Combine cat with other commands for advanced processing:
cat file | grep pattern
Example:
cat /var/log/syslog | grep error
Concatenate Files in Reverse Order
Use tac (reverse cat) to display the contents of a file from the last line to the first:
tac filename
View a File with Less
For large files, pipe cat to less for easier navigation:
cat largefile | less
Cheat Sheet Commands:
Start with cat to familiarize yourself with a file before diving deeper using tools like grep or sed.
Line Number Debugging:
Use cat -n to identify the exact lines causing issues in configuration files or scripts.
Combine and Backup:
Use cat to merge multiple log files for analysis or to back up configurations:
cat conf1.conf conf2.conf > backup.conf
cat /etc/os-release
Displays system version information.
cat log1.log log2.log > combined.log
cat /var/log/auth.log | grep "Failed password"
Filters the authentication log for failed password attempts.
cat is TimelessDespite its simplicity, cat remains one of the most frequently used commands in Unix/Linux environments. It’s the kind of tool that becomes second nature, seamlessly integrated into countless workflows. Mastering cat is like learning to walk before you run—it’s foundational, yet endlessly useful.
With its versatility and simplicity, cat proves that sometimes the most basic tools are also the most indispensable. Whether you're just starting your Linux journey or you’re a seasoned sysadmin, cat will always have a place in your command-line toolbox.
Return to the Table of Contents for more guides and tutorials.