In any operating system, users work with files and perform operations such as creating, deleting, and reading from files. In Linux, the situation is not any different.
Whereas you can work with files using the graphical user interface in a Linux operating system, most Linux users prefer interacting with the operating system from the terminal.
As such, Linux has commands that allow users to work with files right from the terminal. One such command is the cat command.
From the Linux man pages, the cat command is used to concatenate files and print file contents to the standard output. Linux man pages are documentation files that are meant to help users understand Linux commands and how they are used.
To access the man page for any Linux command, you simply enter man <command_name>. For instance, to see the man pages for the cat command, execute the following command in the terminal:
What the cat Command is Used For
As noted earlier, the cat command can be used to print out the content of a file to the screen. Its other functionality is concatenating files and outputting their content. This means that if you provide the cat command with multiple files, it will concatenate their content and output their content to the screen.
To put it simply, the cat command will put together the contents in multiple files and output them to the screen. In such an operation, however, the cat command will not create any new file, and the original files being used in the cat command will remain unchanged.
The cat command is also used in redirection. Redirection refers to how users can alter the source of standard input and the destination for standard output and standard error. Standard input, standard output, and standard error are the standard streams used for communication in Linux.
By default, the standard input is the keyboard, and standard output and standard errors are displayed on the screen by default.
Redirection, therefore, means instead of getting the standard input from a keyboard which is the default, we can redirect and get the input from a file or any other source.
When it comes to the standard output and the standard error, which are displayed on the screen by default, through redirection, you can have these outputs written to a file.
cat Command Syntax
The syntax for the cat command is as follows:
cat [OPTION]... [FILE]...
In Linux man pages, anything listed inside square brackets is optional. Therefore, in the syntax above, it is not a must for the cat command to have an OPTION or even a File as an argument. It can have either of the two, both of them or none at all.
Ellipsis(…) are used to show that one or more of the preceding operands or arguments are allowed. Therefore, the cat command can take one or multiple OPTIONs or FILEs.
FILE represents files we want the cat command to concatenate and display. OPTION represents flags that can be added to the cat command to modify its behavior. Options that can be used with the cat command are shown below:
The cat command works by taking the file(s) passed to it and outputting their contents to the standard output, which is the terminal window. In case there are multiple files passed to it, their contents are concatenated and then displayed on the terminal window.
The cat command will also behave differently depending on the flag passed to it. For instance, passing the -n flag will make the cat command number all the output lines. Depending on how you want it to behave, you can pass any of the flags that can be used with the cat command.
How to Use the cat Command
To use the cat command, simply enter the command and the name of the file(s) you want to concatenate and read for example. To output the contents of a file called rainbow.txt, execute the following command.
You can also add a flag to modify the behavior of the cat command. For instance, you can number all the output lines using the -n flag. In our example above, we can number all the output lines in rainbow.txt by executing the following line:
cat -n rainbow.txt
The outputs from the two commands are shown below:
To combine the flags or options that can be used with the cat command, simply write them one after the other, or you can space them out. Either of the two works. The -E flag shown below is used to add a dollar sign($) at the end of the output lines.
This can be useful when displaying currency values. The various ways you can combine multiple flags with the cat command are shown below:
To concatenate and display multiple files using the cat command, simply write the file names one after the other, separated by spaces. For instance, to concatenate two files named rainbow.txt and primary.txt execute the following line:
cat rainbow.txt primary.txt
The output of the command is shown below
The two files rainbow.txt and primary.txt will remain unchanged after this operation. Note that the order in which the files appear in the command determines how the contents will be displayed. Contents are displayed from the first file to the last. You can also add flags when concatenating and displaying multiple files, as shown below:
A final thing to note about the cat command is that in the event that no file is provided to the command or a dash(-) is provided, the cat command reads from the standard input instead of a file. The standard input is the keyboard.
Therefore, the cat command will simply display whatever a user enters through the keyboard. To see this in practice, execute the command:
The command will take you to a blinking cursor awaiting input from the keyboard. Enter any text, then press Enter. Whatever you type will be outputted to the standard output as shown below:
Press CTRL + C to exit and stop passing input to the cat command.
We can also use the cat command to concatenate and output the content of all files of a particular type in the active directory. This is done using an asterisk(*) wildcard. For instance, to out all the contents of the text files(.txt) in a particular directory, we’d execute the code below:
The output of the command is shown below:
Examples of cat Command
Aside from the standard uses of the cat command, the functionality of the cat command can be augmented through redirection. As mentioned earlier, the cat command outputs to the terminal screen by default.
However, through redirection, we can redirect its output to another file instead of the output being displayed on the screen.
The output redirection operator in Linux is > (the greater than symbol, also called chevron).
For instance, if we execute the line:
The contents of rainbow.txt we’ll be output to the screen.
However, if we execute
cat colors.txt > rainbow.txt
The content of rainbow.txt will not be displayed on the screen. Instead, they will be redirected and written to the colors.txt file. This can be an excellent way of copying content from one file to another.
The default behavior of output redirection in Linux is that it overwrites all the content in the file we are redirecting to. For instance, in the example above, all the content in rainbow.txt will be overwritten by the content from colors.txt. To keep the existing content in the file you are redirecting to, and append the new content to it, use >> when redirecting.
Notice below that colors.txt now has the colors that were found in rainbows.txt after redirecting the output of rainbows.txt to colors.txt using >>
To take the functionality of the cat command even further, we can combine redirection and the cat command’s default behavior of reading from the standard input when no file is provided in order to use the cat command to create a file and write to it.
If you execute the code below
cat > dark.txt
We will be redirecting to dark.txt. Since we did not provide a file whose content will be redirected to dark.txt, the cat command will expect input from the keyboard.
Therefore enter any text you want to store in dark.txt, press Enter to go to the next line, and press CTRL + D when done to save what you’ve typed to the file you’re redirecting to.
Since we are redirecting to a file that does not exist, dark.txt will automatically be created, and what we typed through the keyboard will be saved in the file. This way, we can use the cat command to create files and write to them at the time of their creation.
The output of the operation is shown below:
The cat can also be used to add text to the end of a file. Using dark.txt, we can append more colors to it by executing:
cat >> dark.txt
You can then add additional information by typing through the keyboard and then pressing CTRL + D to save and append the new content to dark.txt as shown below:
The cat command can also be used to concatenate several files into one file. The syntax for such an operation is shown below
cat file1 file2 file3 > file4
The content of file1, file2, and file3 will be concatenated together and written to file4. You can also use >> in case you don’t want to overwrite the content in file4. If file$ does not exist, it will be created automatically.
An example of this is shown below:
To write the contents of colors.txt and primary.txt to allColors.txt, we execute
cat colors.txt primary.txt > allColors.txt
You can also append the content from one or multiple files to another file using the cat command. This is done by using >> when redirecting the output. This avoids overwriting what is already stored in the other file. For instance, to append the content of primary.txt to colors.txt, execute the code below:
cat primary.txt >> colors.txt
The result of this operation is shown below:
The cat command is a very useful Linux command that allows users to concatenate and display the content of files. The command has multiple flags that can be added to it to change how it behaves.
Through redirection, the functionality of the cat command can be greatly augmented, allowing users to create and copy files, concatenate multiple files into one file, and append to an existing file. To learn more about the cat command, visit its man pages.
You may also explore some Linux Commands for system administrators.