In this tutorial, you’ll explore how to use the cat command in Linux. cat is commonly used to display the contents of one or more text files, combine files, and create new files by appending the contents of one file to the end of another file.
The cat command is one of the most used commands in Linux. The name of the cat command comes from its functionality to concatenate files. You can read, append, and write the contents of the file to standard output. If no file is specified or if the input file name is specified as a single hyphen (-), it is read from standard input.
Options
Tag | Description |
---|---|
-A, –show-all | equivalent to -vET |
-b, –number-nonblank | number nonempty output lines override -n |
-e | equivalent to -vE |
-E, –show-ends | display $ at end of each line |
-n, –number | number of all output lines |
-s, –squeeze-blank | suppress repeated empty output lines (never more than one single blank line) |
-t | equivalent to -vT |
-T, –show-tabs | display TAB characters as ^I |
-u | (ignored) |
-v, –show-nonprinting | use ^ and M- notation, except for LFD and TAB |
–help | display this help and exit |
–version | output version information and exit |
cat command syntax
Before we explain how to use the cat command, let’s first review the basic syntax.
Cat utility expressions take the following form:
cat [options] [FILE_NAMES]
- Options – Chat options. Use cat –help to see all available options.
- FILE_NAMES – Zero or more file names.
Displaying File Contents
The most basic and common usage of the cat command is to read the contents of files.
For example, the following command will display the contents of the /etc/lsb-release file on the terminal:
cat /etc/lsb-release
Create a file with cat
To create a file, we’ll use the cat command input stream and then redirect the cat command’s output to a file using the Linux “>” redirection operators.
Specifically, to write to a file using the cat command, we enter this command in our terminal:
cat > file.txt
We will see that once again the terminal is waiting for our input. However, this time it will not echo the text we typed. This is because we requested that the output be redirected to the file.txt file instead of the standard output stream.
Enter some text in the Terminal followed by CTRL+D to complete the command:
cat > file.txt This is a file. This is a new line.
The file.txt file will now contain the two lines we entered. To check our result, we can again use the cat command:
cat file.txt This is a file. This is a new line.
So! We wrote to a file using the cat command.
Add text to a file using Cat
One thing we should note in the previous example is that it will always overwrite the file.txt file.
If we want to append to an existing file, we can use the “>>” operator:
cat >> file.txt
This is an attached line.
To verify that the last command added the file, we examine the contents of the file:
cat readme.txt This is a file. This is a new line. This is an attached line.
Here we have it. The line we enter is appended to the end of the file instead of replacing the entire document.
Conclusion
We show you how to use the cat command in Linux. The cat command can display, combine and create new files. For more information on the cat command, see the man page for the cat command.
If you have any questions please leave them in the comments below