One of the most available day-to-day Linux commands is cat, a simple tool for viewing files. Or is it? It turns out, there’s a lot more to cat than you may realize. From the basic to the sublime, here are some of the many ways you can use cat.
1
Combining Several Files
I’ll start here, even though it’s probably not the most common use for cat, because the tool’s original purpose was to concatenate files. That “cat” in the middle of “concatenate” is where the name comes from. If you’re unfamiliar with this term, it means “to join together,” so cat is all about joining files.
Let’s say you have a file, foo, that looks like this:
An example file
With a couple of lines
And you also have a second file, bar, looking like this:
This is just another example
You can combine the two by running cat and passing those filenames as arguments:
cat foo bar
In response, you’ll see the full contents of the first file, followed by the full contents of the second; cat has joined the two:
This might not seem particularly useful, but you’ll probably find a need to join files at some point or another. You might be merging log files or combining several chapters of a text into one single work. You might also join files for easier transport or storage, although the tar command is usually a better fit for such a task.
2
Displaying a Single File
Wondering what happens if you pass just a single file argument to cat? Thankfully, the tool handles this case with a sensible default behavior, rather than generating an error.
cat foo
With just a single file argument, cat outputs the contents of that file. Note that it will do this all at once, without stopping, so you’ll need to scroll your terminal to view files longer than one screen.
Be careful when cat’ing a file. Text files are fine, but an executable file will just look like gibberish if you print it in your terminal, and it may even mess up your display (temporarily).
This use of cat is perfect for when you want a quick look at the contents of a text file, particularly if it’s on the smaller side. But you’ll find it less convenient for longer files, or files that you wish to explore in more detail. In such cases, a pager like more or less will do the trick.
With no arguments, cat will repeat whatever it sees on standard input. Running it as cat will cause the program to wait for input, print that input, and repeat until it receives an end-of-file signal/character (Ctrl + D). See later to find out how this can actually be useful!
3
Adding Line Numbers to a File
If you’re a programmer or you’re working collaboratively, it can be useful to refer to line numbers in a file. All text editors should include this feature, but for quickly viewing line numbers in a file, cat is another option:
cat -n foo
The -n option numbers lines of output, starting at 1.
Different versions of cat behave differently. The GNU coreutils 9.4 version (Ubuntu 24.04) numbers each line of output, increasing by 1 each time. On macOS 26.0, however, cat numbers each input file individually, resetting the count with each file.
The cat command also supports a -b option, which numbers non-blank output lines. This can be useful in some cases, but you should avoid it for programming, since tools like compilers will always report line numbers, including blank lines.
4
Combining Two Files and Storing Them as a Third
The real utility of cat becomes clear when you combine it with the power of the Linux shell, especially features for redirection and piping. For example, instead of sending output to the screen for immediate viewing, you can send it elsewhere.
One of the most common cases is to combine two files and store them as a third:
cat foo bar > hum
This pattern is so useful that you’ll probably memorize it without even trying. The output redirection character, >, causes all output to be sent elsewhere, in this case to a named file. The file will be created if it doesn’t already exist, and overwritten if it does.
5
Creating or Updating a Text File
Using the same output redirection, you can create simple text files with nothing more than your terminal and cat. This may not be the easiest approach, but it’s probably the most efficient, and it can be useful in certain environments; your favorite text editor may not always be available.
To create a text file interactively, start by running cat with no arguments and redirect its output to a file of your choice, e.g.:
cat > newfile.txt
Your terminal will print a new line without a leading prompt, indicating that it’s in input mode. Anything you type here will be shown in your terminal and inserted into the file, until you press Ctrl + D at the beginning of a line. This signals EOF (end of file), at which point cat will end and return control to the shell.
Similarly, you can use the >> operator to append to a file:
cat >> notes
You could use these methods to create a simple to-do file or take notes.
6
Piping a File to a Command
Lastly, the most controversial use of cat is the so-called “useless use of cat.” Many beginners (and, to be fair, more experienced users too) fall into a habit of using this pattern:
cat filename | command
For example, cat file | grep searchword or cat file | tr ‘!’ ‘.’.
There’s really nothing wrong with this, but some people are very keen to point out that there are better alternatives. Commands like grep accept filename arguments, so grep searchword file does the equivalent of piping cat’s output, but it’s slightly more efficient with less typing and fewer processes. Some commands, like tr, don’t operate on filenames, but you can still have them act on the contents of a file using redirection:
command < filename
This time round, it’s the less-than symbol for input redirection, but the concept is just the same.
These cat-free equivalents can also be more readable, making them preferable for use in scripts or documentation. However, do bear in mind that there really isn’t anything wrong with the occasional use of cat in this way. The difference in resource usage is minimal on today’s hardware, and using cat like this can make it easier to build a command chain one piece at a time.
The most important takeaway is that file redirection can be just as useful as piping. It’s easy to overlook, but this feature is one of Linux’s strengths, and it’s worth getting familiar with for all those occasions when you’ll really need it.