The cat
command – a tool so quintessentially Linux that any foray into the terminal feels incomplete without it. From displaying text files to concatenating and creating them, cat
stands as a testament to the power and simplicity of Linux commands. In this blog, we’ll dive deep into the cat
command, exploring its capabilities with examples from a real-world Ubuntu terminal.
What is the cat command in Linux?
The cat command, short for “concatenate,” is a command-line utility in Unix-like operating systems, including Linux. It is used to display, create, and concatenate files. The cat command allows users to display the contents of a file or create a new file by concatenating multiple files. It is commonly used in shell scripts, batch files, and Linux terminals.
Displaying text files
The most basic and commonly used function of cat
is to display the contents of text files. Here’s a simple example:
$ cat myfile.txt Hello, this is a sample text file.
In this example, cat
outputs the contents of myfile.txt
to the terminal. Simple, right? But oh, so powerful in its simplicity.
Concatenating multiple files
Where cat
really shines is in its ability to concatenate multiple files. Let’s say we have two files, file1.txt
and file2.txt
, with the following contents:
$ cat file1.txt This is file 1. $ cat file2.txt This is file 2.
To concatenate these files and display their combined contents, we use:
$ cat file1.txt file2.txt This is file 1. This is file 2.
Even better, you can redirect the output to create a new file:
$ cat file1.txt file2.txt > combined.txt $ cat combined.txt This is file 1. This is file 2.
Redirecting standard input to a file
Here’s a feature of cat
I find particularly handy: creating a new file by redirecting standard input. Simply use cat
with the >
operator and type your input:
$ cat > newfile.txt This is a new file created by redirecting standard input.
Press Ctrl+D
to signify the end of input, and voilà, you’ve created a new file with your desired content.
Displaying line numbers
One of my favorite cat
tricks is displaying line numbers with the -n
option. This is especially useful when reviewing scripts or code:
$ cat -n myfile.txt 1 Hello, this is a sample text file.
It’s a small touch, but when you’re debugging or reviewing code, it’s invaluable.
Viewing files with line numbers excluding blank lines
Adding to our toolkit, the -b
option with cat
displays line numbers but skips the blank lines. This feature is particularly useful for code files where blank lines are used for readability but don’t need numbering. Here’s how you can use it:
$ cat -b codefile.txt 1 #!/bin/bash 2 # Sample script with blank lines 3 4 echo "Hello, world!"
Notice how the blank line between the comments and the echo command is not numbered? It’s a subtle feature but great for keeping the focus on the actual code.
Appending file contents to another file
Another powerful use of cat
is appending the content of one or more files to the end of another file. This is done using the >>
operator. For example, if you want to add file2.txt
to the end of file1.txt
, you would use:
$ cat file2.txt >> file1.txt
This command doesn’t output anything to the terminal, but if you cat file1.txt
, you’ll see that file2.txt
‘s contents have been added to the end of file1.txt
.
Using cat with pipes
The true power of Linux commands often shines through their combinability, and cat
is no exception. By using pipes (|
), you can direct the output of cat
into another command for further processing. A common scenario is piping cat
output to grep
to search for specific text. For instance:
$ cat combined.txt | grep "file 1" This is file 1.
This command sequence searches for “file 1” in the contents of combined.txt
and displays only the matching line.
Reverse concatenating files
A somewhat unconventional use of cat
, but worth mentioning, is reverse concatenating files. This isn’t a direct feature of cat
but can be achieved with the help of other commands like tac
(the reverse of cat
). While tac
is technically its own command, it’s interesting to note how it complements cat
:
$ tac file1.txt file2.txt > reversed.txt $ cat reversed.txt This is file 2. This is file 1.
Here, tac
reverses the order of lines from the input files and combines them into reversed.txt
.
My two cents
Using cat
to display large files can be less than ideal. It dumps everything into your terminal without pagination, making it hard to navigate through extensive content. In such cases, tools like less
or more
might be more appropriate.
Conclusion
The cat
command is a staple in the Linux world, offering a simple yet powerful way to work with text files. Whether you’re concatenating files, creating new ones, or just displaying content, cat
has got you covered. And though it might not be the best tool for every job (looking at you, large files), its versatility and ease of use make it an indispensable part of any Linux user’s toolkit.