The cat command (short for “Concatenate”) is one of the most frequently used commands in operating systems like Linux / Unix. The cat command allows users to create one or more files, view file contents, join files, and redirect output in a terminal or file.
In this article, please read along Make tech easier Learn how to use the cat command with examples in Linux.
General syntax
cat [OPTION] [FILE]...
Specific uses with the Cat command (with examples)
1. Display the contents of the file
In the example below, the command will display the contents of the file / etc / passwd.
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
narad:x:500:500::/home/narad:/bin/bash
2. View the contents of multiple files in Terminal
In the example below, the command will display the contents of the file kiểm TRA and test1 in Terminal.
# cat test test1
Hello everybody
Hi world,
3. Create the file using the Cat command
The post will create a file named test2 with the command below:
# cat >test2
Wait for input from the user, enter the desired text and press CTRL + D (keep key Ctrl and enter d) to exit. The text will be written in the file test2. You can view the contents of the file with the following cat command.
# cat test2
hello everyone, how do you do?
4. Use the Cat command with more and less options
If the file has a large amount of content that does not fit the output terminal and the screen scrolls up very quickly, you can use the parameters. more and less with the cat command as follows:
# cat song.txt | more
# cat song.txt | less
5. Display line number in file
With option -nYou can see the line number of the song.txt file in the output terminal:
# cat -n song.txt
1 "Heal The World"
2 There's A Place In
3 Your Heart
4 And I Know That It Is Love
5 And This Place Could
6 Be Much
7 Brighter Than Tomorrow
8 And If You Really Try
9 You'll Find There's No Need
10 To Cry
11 In This Place You'll Feel
12 There's No Hurt Or Sorrow
6. Display $ at the end of the file
In the section below you can see with options -e, $ is displayed at the end of a line and in a space between paragraphs. This option is useful for squeezing multiple lines into one line.
# cat -e test
hello everyone, how do you do?$
$
Hey, am fine.$
How's your training going on?$
$
7. Show tab-separated lines in the file
In the output below, you can see the TAB space filled with character ^ I.
# cat -T test
hello ^Ieveryone, how do you do?
Hey, ^Iam fine.
^I^IHow's your training ^Igoing on?
Let's do ^Isome practice in Linux.
8. Display multiple files at the same time
In the example below, there are 3 files test, test1 and test2. You can view the contents of those files as shown above. Remember to separate each file with semicolon.
# cat test; cat test1; cat test2
This is test file
This is test1 file.
This is test2 file.
9. Use standard output with the redirect operator
You can redirect the standard output of a file to a new file other than an existing file with the> symbol. Be careful, the existing content’s test1 will be overwritten by the content of the file kiểm TRA.
# cat test > test1
10. Append the standard output with the redirect operator
Append content in existing file with symbol >>. Here, the content of the test file will be added to the end of the test1 file.
# cat test >> test1
11. Standard input redirection with redirect operator
When you use redirection with standard input <, command to use file test2 as input to a command and the output will be displayed in a terminal.
# cat < test2
This is test2 file.
12. Redirect multiple outputs to a single file
This will create a file called test 3 and all output will be redirected to the newly created file.
# cat test test1 test2 > test3
13. Arrange the content of multiple files in a single file
This will create a file test4 and the output of the cat command is sorted for sorting and the results will be redirected to a newly created file.
# cat test test1 test2 test3 | sort > test4
This article has covered the basic commands that can help you discover the cat command.
Wish you successful application!
Source link: The Cat command in Linux
– https://techtipsnreview.com/