Situatie
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.
Solutie
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.
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.
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.
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.
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.
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.
Leave A Comment?