Cat command, a short form of “concatenate” is one of the most important and frequently used on Linux/Unix like operating systems. It is use to create single or multiple files, view the content of the file, concatenates files and redirect output in any file or on the terminal. In this article, we see the uses of cat command with their examples in Linux.
cat [OPTION] [FILE]...
Suggested Reading: Linux sed command Structure
Display Contents of File
To display the content of the file, you can use the cat command with the filename.
In the below example, It will show the content of /etc/password file.
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin satish:x:500:500::/home/satish:/bin/bash
View Contents of Multiple Files in terminal
To display the content of multiple files, you can use the cat command with multiple filenames.
In below example, it will display contents of test01 and test02 file in terminal.
# cat test01 test02
Hello everybody Hi world
Create a File with Cat Command
You can create a file called test02 file with below command
# cat > test02
The command waits for input from the user, type desired text and press CTRL+D. It will be writing text into the file test02.
You can see the content of the file with the following cat command.
# cat test02 hello everyone, how do you do?
Display Line Numbers in File
You can use the “-n” option with cat command to see the content of the file with lines number.
Using the below command, you can see the content of test01.txt file with line numbers.
# cat -n test01.txt 1 Hello Everyone 2 I am Satish Kumar 3 A Computer Engineer 4 Founder of LinuxConcept 5 Here to help you 6 Sharing Linux Pratical Knowladge 7 With Everyone.
Use Cat Command with More & Less Options
If file having the large number of content that won’t fit in output terminal and screen scrolls up very fast, we can use parameters more and less with cat command as shown below example.
# cat movies-list.txt | more # cat movies-list.txt | less
Copy the contents of one file to another file
The cat command can use to copy the content of one file to another file.
In the below example, it copies the content of file text01 to file text02.
# cat text01 > text02
The output redirection operator (>) use to flush the destination file before moving new content. If you want to move new content with existing content in the destination file, use another redirection operator (>>) which is use to appends the contents into the destination file.
Use cat command to highlight line-ends
You can use the “-e” option with cat command to display “$” at the end of the line and in the space between the paragraphs—this option we use to squeeze multiple lines in a single line.
Check below example for better understanding:
# cat -e test hello everyone, how do you do?$ $ I am fine.$ Hows your learning going?$ $
Display Lines in File which are Tab separated
To display TAB separated lines in a file, you can use the cat command with the “-T” option.
In the below example, you can see the TAB space filled with ‘^I’ character.
hello ^Ieveryone, how do you do? I ^Iam fine. ^I^IHow’s your learning ^Igoing? Let’s do ^Isome practice in Linux Server.
Display Multiple File’s contents at Once
You can use the semicolon (;) separator with several cat command as shown in below example here we display the content of three files named as following text01, text02, and text03.
# cat text01; cat text02; cat text03 This is text01 file This is text02 file. This is text03 file.
Use Input with Redirection Operator with cat Command
The cat command can use with the standard redirect operator (<) to get the input for the command.
In the below example, cat command gets the input from file text01 and the output shown in the terminal.
# cat < text01 This is text01 file.
Concatenate multiple file contents in a single file
You can use the cat command to redirect content of multiple files into a single file.
In the below example, it redirects content of files text01, text02, and text03 into the file text04.
# cat text01 text02 text03 > text04
Sorting Multiple File’s content and redirect output in a Single File
In the below example, it creates a file text04 and output of the cat command piped to sort and result get redirect to file text04.
# cat text01 text02 text03 | sort > test4
Use cat to display non printing characters
You can use the cat command to display non-printable characters in a file by using the “-v” option.
In below example, I am showing non-printable characters of file test123.txt.
# cat -v test123.txt
The “-A” option of cat command
The “-A” option is doing a job as the combination of the “-v”, “-E” and “-T” option. If you have some specific requirements where need to use these three options then you can use only the “-A” option which do the job of all three options, as shown in below example:
# cat -A test.txt