The cat (short for “concatenate“) command is one of the most frequently used commands in Linux that comes pre-installed in most Linux distribution systems and is primarily used to display the content of existing files.
Moreover, the cat command can be utilized by the user to concatenate multiple files, create new files, append content to existing files, view the content of a file, and redirect output in the terminal or files.
The cat command can also be used to format the output of the file with the help of different options, such as adding numbers before each line of the file’s content.
Additionally, it can execute in combination with other commands to perform various tasks including providing page navigation and conversion of file format to binary or hexadecimal.
In this article, we are going to find out the handy use of cat commands with their examples in Linux.
Table of Contents
Cat Command Syntax
The cat command can accept multiple options and file name arguments as shown:
$ cat [OPTION]... [FILE]...
Let’s understand the above syntax:
- [OPTION] – Users can provide multiple options to alter the behavior of the command. The options start with a hyphen
("-")
, such as"-E"
is used to display line ends and"-n"
to display numbers before lines. - [FILE] – The file argument specifies the file which will be manipulated by the command. However, users can provide names of multiple files separated by space.
"cat --help"
command in your Linux terminal:$ cat --help
Let’s explore different examples to harness the power of cat command.
1. Display Contents of a File in Linux
The basic functionality of the cat command is to display the content of an existing file in Linux. For that purpose, provide the name of the file with no option as shown.
$ cat Documents/tecmint1.txt
Here in the command, the content of the file “tecmint1.txt” which is located in the “Documents” directory will display.
2. Display Contents of Multiple Files in Linux
The cat command can also be utilized to show the contents of more than one file by providing the file names separated by space as shown:
$ cat tecmint1.txt tecmint2.txt
In the above output, we can see the contents of both files in the terminal. The first two lines are of file “tecmint1.txt”, whereas the last line of the output is the content of the “tecmint2.txt” file.
3. Create a File with Cat Command
The user can create a new file and save content in it with the ">"
symbol (known as the “output redirection operator”) will redirect the output of the command to the file specified by the filename “Tecmint_tutorial.txt” as shown.
$ cat > Tecmint_tutorial.txt
After executing the command, an indicator will blink in the new line. Write the content for the file and press the “CTRL + D”
keys to save and exit the file:
You can verify the file’s creation by using the ls command and use the cat command to view the content of the newly created file:
$ ls $ cat Tecmint_tutorial.txt
4. Append Text to a File in Linux
One of the benefits of the cat command is that it can append the content to an existing file using the ">>"
symbol (known as “append redirection operator”) will append/combine the additional content to an existing file “Tecmint_tutorial.txt“.
$ cat >> Tecmint_tutorial.txt
Type or paste the content that you want to append to this file and press the "CTRL + D"
keys:
Now let’s verify if the content has been appended to the existing contents stored in the file:
$ cat Tecmint_tutorial.txt
5. Copy File Content to Another File in Linux
Sometimes, the user wants to create a copy of the contents stored in a file into a new file for different purposes, such as backup. Here, ">"
operator will read the content of the “Tecmint_tutorial.txt” file sequentially and will place it into a new file named “New_file.txt“.
$ cat Tecmint_tutorial.txt > New_file.txt
The next step is that you verify if the new file has been created successfully by running:
$ ls $ cat New_file.txt
6. Append Contents of Multiple Files Into One File on Linux
As mentioned earlier, the cat command can be utilized for concatenation purposes. Let’s run the command to concatenate/merge contents of the “tecmint1.txt” and “tecmint2.txt” files and store the result in a new file named “cat_tecmint.txt”:
$ cat tecmint1.txt tecmint2.txt > cat_tecmint.txt
The above command will read the content of the “tecmint1.txt” and “tecmint2.txt” files and will write them in a new file “cat_tecmint.txt”.
Moving forward, we need to verify whether the new file stores the concatenated content of both files or not:
$ ls $ cat cat_tecmint.txt
7. View File Content with Line Endings
The user can also use the "-E"
option to view the EOL (End of Line) character in the contents of the file. The EOL characters are known as non-printing characters and they are represented by the dollar ("$")
symbol.
$ cat -E cat_tecmint.txt
The expected output will show the "$"
symbol at the end of each line of the content.
8. List Contents of All Specified File Types
The cat command can use the "*"
wildcard character to list the content of all files available in the current directory. Additionally, you can also specify any particular file type such as ".txt"
followed by a wildcard character to show the content of all “txt” files available in the directory.
$ cat *.txt
The output portrays the content of all “txt” files one after one.
9. Display Line Numbers in File
If you want to show line numbers prior to each line of the file’s content, use the "-n"
option to display the line number in the output without any changes to the original content of the file.
$ cat -n Fruits.txt
The expected output will show line numbers before each line.
10. Print Line Numbers of Multiple Files
The cat command with the "-n"
option can also work on multiple files by concatenating the contents of multiple files and adding numbers prior to each line of combined output.
$ cat -n Fruits.txt veg.txt
Here in the command, the content “Fruits.txt” and “veg.txt” files will combine, and then the "-n"
option will add line numbers at the beginning of each line of the output.
11. Show File Contents with Tab Characters
The "-T"
option can display the tab spaces characters "^I"
in the output which are known as non-printing characters.
$ cat -T tabfile
The output contains the tab characters in place of tab spaces.
12. View File Contents with More Command
Some files contain a lot of content that does not entirely fit in the output screen of the terminal. If we use the simple cat command to display the content of such files, the output does not indicate that more content is available and the user needs to scroll down to see it.
$ cat tutorial.txt
Here in the output, we can see only some of the actual content.
To resolve this issue, you can use the pipe "|"
symbol that helps in using the output of one command as the input to another command, in this case, it is the “more” command that offers page navigation at the end of the file.
$ cat tutorial.txt | more
13. View File Contents with Less Command
You can also use less commands to view the contents of a file in a scrollable and searchable manner using the keys.
$ cat tutorial.txt | less
14. Suppress Repeated Empty Lines in Output
In some cases, the user leaves repeated empty lines by mistake instead of a single empty line. However, the cat command can be utilized to suppress repeated empty lines from the content of a file with the help of the "-s"
option.
$ cat -s tutorial.txt
The output has only single empty lines, all repeated lines are suppressed successfully.
15. Append File Content to End of Another File
The cat command can append the content of a file at the end of another file by using the ">>"
symbol (known as “append redirection operator”).
$ cat Fruits.txt >> veg.txt $ cat veg.txt
The output shows that the contents of both files are appended in the “veg.txt” file.
16. Display File Contents in Reversed Order
To display the file’s content in reverse order, use the tac command, which is also known as ‘cat’ backward that displays the last line first, then the second last, and so on.
$ tac Weekdays.txt
The output displays the content of the “Weekdays.txt” in reverse order.
17. Show File Content in Binary Format
The cat command can be used in combination with the “xxd” utility along with the "-b"
option to convert the contents of the file into the binary format.
$ cat Weekdays.txt | xxd -b
Let’s break down the above command:
|
– The pipe symbol(|)
will give the output of the cat command to the command (xxd -b).- xxd – It is a utility that will convert content into a hexadecimal representation.
-b
– This option is used with xxd to specify the binary output format instead of the default hexadecimal format.
The output portrays the file’s content in binary format and the original format side by side.
18. Show File Content in Hexadecimal Format
To convert the content of a file into the hexadecimal format, the user can utilize the “hexdump” utility as shown below:
$ cat Weekdays.txt | hexdump -C
Here in the command, the pipe symbol is joining both commands whereas the “hexdump” command will convert the content into hexadecimal format. Additionally, the "-C"
option will show the ASCII representation alongside the hexadecimal values.
The output shows the converted content of the file in hexadecimal format successfully.
19. Display Specific Lines of File in Linux
The cat command can be combined with the sed command to display a specific range of lines from a file that matches the defined pattern from the file.
$ cat Weekdays.txt | sed -n '3,6p'
Here in the above command, the sed command will get the output of the cat command as input with the help of the pipe symbol. Then the sed command with the option "-n"
and pattern “3,6p” will print lines 3 to 6 from that input.
20. Sort the Contents of File Alphabetically
The user can utilize the cat command with the sort command to alphabetically sort the lines of content as shown.
$ cat -v veg.txt | sort
The output displays the alphabetically sorted content of the file.
21. Display End of File Marker in File
The cat command can be utilized with the “here document” which aids the user to input the content into the file and set the page end marker.
The “here document” is denoted by the "<<"
symbol followed by a delimiter "EOF"
, which allows the user to input multiple lines of text directly from the terminal and save them into a file.
The input process is terminated by entering the specified delimiter on a new line.
$ cat > month.txt << EOF
The output saved the input in the file “month.txt” and terminated the input process when “EOF” was entered.
22. View CPU Info in Linux
The cat command can also display the content of a virtual file named “cpuinfo”, which contains information about the CPU processor, model name, cache size, number of cores, and other details of the CPU.
$ cat /proc/cpuinfo
That is it for this blog, I hope you understood the usage of the cat command in Linux. You may refer man page of the cat command if you want to know more options.
$ man cat
Conclusion
This article displayed 20 examples of the cat command in Linux to concatenate, display, and create files. Additionally, the cat command can be utilized with other Linux commands to perform more advanced operations like sorting content or converting the content into other file formats.
#4. Skip cat, and go directly to the paginator.
more song.txt
less song.txt
Hi, sir how to create multiple files in cat command (remaining examples very nice and thanks and you are doing very well lot of thanks…
[edyst]>>> Used the cat command successfully!
[edyst]>>> Used the tac command successfully!
[edyst]>>> You used the head command correctly!
[edyst]>>> You used the tail command correctly
[edyst]>>> You copied the first 6 lines of avengers.txt successfully!
[edyst]>>> You copied the last 3 lines of avengers.txt successfully!
[edyst]>>> You reversed the contents of avengers.txt correctly
[edyst]>>> Task executed successfully!
whats the input ?
Thanks for this informations
The tecmint website is good for freshers…
We can use parameters more and less. They’re not parameters, you’re piping the “cat” output to those commands. Big difference.
How do I redirect a file to multiple files? For instance. I want to split 1 file into 10.
Thank you!
@Ying,
You can use the following example split command to split your large file into multiple files.
what will the output of the following command?
I think it will give an error :/ ==> because of “ls -l” there is no such file or directory
If not please aware us too.
Thanks, man!!!
How can I append an md-file to another md-file in a sub-folder? I tried “
cat %U >> subfolder-name/%u
“, but I get this error message “cannot create subfolder-name//path/to/md-file/md-file.md: Directory nonexistent“.Can you help me?
Very useful for my exams . Thank you.
Hi @Ravi,
This is a really good stuff for beginners, like me by the way. About cat command, I have a quite different question: When I use that within a bash shell script that captures and display some output, how do I close the command processing? I mean, after running the script, I needed to press Enter or type
Ctrl+C
/Ctrl+D
, and I would like to have the script terminated after the run.the lines I’ve entered in my script are:
Thank you anyway for this teaching!
@Lenardo,
At the end of your shell script, set exit 0 to close the shell script as shown.
#ZombieRessurection – please do not spread bad practices
“cat | more file” is not smart as “cat file file | sort > newfile” isn’t
use
less file
or
sort file file > newfile
for selecting rows comfortable use sed instead:
will print lines 4-6 from
Sir I am a Linux newbie, can you please say the ways to learn all Linux commands.
12. Redirecting Multiple Files Contain in a Single File
still the type in contain
Thanks. Just a little help about spelling of a word used frequently. “Contains”. I think you want “Contents”.
@Kent,
Oops what a big mistake and was not realized or pointed by anyone so far, thanks a ton Kent for notifying us, we’ve replaced the word contains with content in the writeup..really thankful to you..
will you teach me how to do this on windows please
@Dan,
You mean on Windows 10 with Bash on it? If you have Bash installed on your Windows, all cat command examples shown here will work without any errors..
Hi Ravi thanks.
How can I select lines (i.e. rows) 10 to line 15 from a given file, let, ABC.txt, using cat command:
Suppose I use the following command for getting the first 20 lines and 10 columns of the file ABC.txt which contains a table
Looking forward.
what is the difference between 12th and 13th questions is there are same
@Srujan,
Points 12 and 13 are same, but the 13 example will sort the content by order, for more about sort command read our guides here:
14 Useful ‘sort’ Command Examples – Part 1
7 Interesting ‘sort’ Command Examples – Part 2
yeah but if test1 file contains 3 or more lines then test2 file contains 3 or more lines then how it sort the output of two files into test3
@Srujan,
That you need to test it out yourself and see, how it sort the files with correct order…
can somebody please explain this code below – the line that starts with cat. it seems like the code does not work since our report still prints as portrait, which may be the default printer setup for orientation. we even manually triggered the code but it still did not work. your help is appreciated. thanks so much…
ISP_PRNTR=mcsprt ; export ISP_PRNTR
#Extract the last 3 characters (file extension)
ext=`echo $1|awk -F . ‘{print $NF}’`
#Extract the file name without extension
file=`basename $1`
file=`echo $file|sed ‘s/\.[^.]*$//’`
cat ${1} | /usr/bin/acroread -toPostScript | lp -d $ISP_PRNTR -o landscape
From the code:
This part of the code has not been used in the last command that prints the file, though it only deals with the file name which should not affect the layout during printing.
The variable file has not been used in command;
Instead the original filename, $1 has been used.
It could be a problems of printer specific options that are required.
Thanks so much…
while viewing large file using cat command. how to move immediately to end of the file and how to exit from the file. I used Cntrl+d. But couldn’t exit. and If I need to edit the file, how can I edit using command [vi].
@Satish,
The cat command will list the all content of the file (entire file) on the screen itself you don’t need to scroll up or down. If the file is too long, it quickly moves past you on the screen. To prevent this, use the following command:
If you want to edit the file, open the file with vi editor, press ‘i’ to insert mode for editing.
If you still don’t understand, I suggest you to read our Vi/Vim tricks guides here.
https://www.tecmint.com/vi-editor-usage/
https://www.tecmint.com/learn-vi-and-vim-editor-tips-and-tricks-in-linux/
https://www.tecmint.com/how-to-use-vi-and-vim-editor-in-linux/
displays the contents of an entire file on the screen
cat test* >>some.txt
thank u so much for sharing this ! It’s very useful.
i think cat test
and
cat < test both are same right?
if any difference please tell me…..
They are two very different methods of achieving the same result.
‘cat file’ takes file as an argument, opening the file and displaying its contents
‘cat < file' opens file, and redirects its contents to stdin, so the cat command will take the contents of the file as if they were being typed in by a keyboard.
To better understand what's going on, try this:
in terminal 1:
$ mkfifo test
$ cat test
In terminal 2, start typing. Every time you hit enter, you will see the text you just typed appear in terminal 1. Hit Ctrl+D and it will kill both the running cats.
I don’t know why it deleted half my text, but let’s try this again.
Terminal 1 should have a cat command, and terminal 2 should also have a cat command. If you don’t see two cat commands, then a bot is filtering my comment.
Terminal 1:
$ mkfifo test
$ cat test
Now you can start typing into terminal 2.
Ah, I figured out what’s going on: It thinks I’m trying to type HTML.
One more time:
T1:
$ mkfifo test
$ cat < test
T2:
$cat > test
Moderators: If you see this chain of self-replies, please fix it and edit the original reply to convey what I’m trying to convey.
If you didn’t already figure out what’s going on, I used < and > to generate < and >, otherwise, “this <te>xt” becomes “this xt” because it is interpreted as a tag.
Also, the ability to edit comments would be much welcomed. I hate to mess up a discussion thread with a million self-replies that try to figure out why X didn’t work.
Thanks
Excellent information for Linux users..
Can you please elaborate point number 11
#11 is total B.S.
‘cat < file' does not take 'file' as an argument.
What 'cat < file' does, is use the contents (not "contains") of file as the input for cat, taking the place of the keyboard. It is functionally equivalent to 'cat file', and is thus completely redundant.
you are absolutely right. I wasted my time on that command. #11 is crap
Q. Hi All, Can you show me what is the command or terminal to use with this question “Search lines which contains alpha-numeric words( combination of alphabets and number) and copy those lines is sorted order to /root/lines (output should not contain any blank lines)”. for example I have a file called Searchline.txt. thank you.
Why “cat test; cat test1; cat test2” ?
Why not “cat test test1 test2” ?
Why “cat song.txt | more” instead of “more song.txt”?
why can’t “cat test >>some.txt*”
Sorry it is cat test* >>some.txt