cat

cat is one of the simplest tools in the Unix/Linux toolbox. It is very widely used and can do a lot more than the basic description of it would have you believe. We will start with the basic operation of cat, which is short for “concatenate.” For example, the command cat file1 file2 will display the contents of all of the files passed as arguments.

cat file1
this is file1
it has four lines.
the second line is blank.
$ cat file2
this is file2
it has six lines in all (three blank),
of which this is the sixth.
$ cat file1 file2
this is file1
it has four lines.
the second is blank.
this is file2
it has six lines (three blank)
of which this is the sixth.
$
note.ai

The files do not have to be text files; cat can work on binary files just as well.

This may not seem a particularly useful feature, and indeed, it is probably the least common usage of the cat command! This section looks at a few scripts that use various different features of the cat utility. When designing a shell script, it can be useful to be aware of basic features such as these because using system tools to manipulate file contents for you is much more efficient and easier than doing it yourself in the shell script. Therefore, familiarity with some of these less widely used features can make shell scripting a much easier process.

Numbering Lines

When dealing with lines of code, configuration files, or many other text ...

Get Shell Scripting: Expert Recipes for Linux, Bash, and More now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.