Printing Data of Every File in a Directory with One Command

A Guide to using cat and Wildcards

Kacper Bąk
2 min readApr 14, 2023

--

Background

Printing the data of every file in a directory can be a useful task in many situations. For example, if you want to quickly review the contents of multiple files in a directory, or if you want to search for specific patterns in multiple files at once, this command can save you a lot of time and effort.

The command I’ll be using for this task is the cat command, which is a standard Unix utility that is used to concatenate and display the contents of files. The cat command takes one or more filenames as arguments, and it outputs the contents of those files to the standard output.

By using a wildcard character (*) as the filename argument to cat, I can tell it to concatenate the contents of all files in the current directory and display them on the screen. This allows us to print the data of every file in a directory with a single command.

Implementation

Let’s now dive into the implementation details of this command. I’ll start by discussing how to use the cat command to concatenate and display the contents of multiple files, and then I’ll show how to use a wildcard character to specify all files in a directory.

Concatenating multiple files with cat

The cat command is very simple to use for concatenating multiple files. To concatenate two or more files, simply specify their filenames as arguments to cat, separated by spaces.

Here’s an example of how to concatenate two files, file1.txt and file2.txt:

cat file1.txt file2.txt

When you run this command, the contents of file1.txt will be displayed first, followed by the contents of file2.txt.

Using a wildcard to specify all files in a directory

Let's see how we can use a wildcard to specify all files in a directory.

The wildcard character (*) can be used to match any sequence of characters in a filename. When used in a filename argument to cat, it matches all files in the current directory that have any filename.

For example, if you have three files in your current directory, file1.txt, file2.txt, and file3.txt, you can concatenate them all with the following command:

cat *

When you run this command, the contents of all three files will be displayed in the order they appear in the directory listing.

Putting it all together

I can put it all together to print the data of every file in a directory with a single command.

Here’s the final command:

cat *

When you run this command, the contents of all files in the current directory will be displayed in the order they appear in the directory listing.

--

--