via: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/
General Syntax
cat [OPTION] [FILE]...
1. Display Contains of File
In the below example, it will show contains of /etc/passwd file.
# 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 Contains of Multiple Files in terminal
In below example, it will display contains of test and test1 file in terminal.
# cat test test1 Hello everybody Hi world,
3. Create a File with Cat Command
We will create a file called test2 file with below command.
# cat >test2
Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d‘) to exit. The text will be written in test2 file. You can see contains of file with following catcommand.
# cat test2 hello everyone, how do you do?
4. Use Cat Command with More & Less Options
If file having large number of contains that won’t fit in output terminal and screen scrolls up very fast, we can use parameters more and less with cat command as show above.
# cat song.txt | more # cat song.txt | less
5. Display Line Numbers in File
With -n option you could see the line numbers of a file song.txt 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 File
In the below, you can see with -e option that ‘$‘ is shows at the end of line and also in space showing ‘$‘ if there is any gap between paragraphs. This options is useful to squeeze multiple lines in a single line.
# cat -e test hello everyone, how do you do?$ $ Hey, am fine.$ How's your training going on?$ $
7. Display Tab separated Lines in File
In the below output, we could see TAB space is filled up with ‘^I‘ character.
# 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 Once
In the below example we have three files test, test1 and test2 and able to view the contains of those file as shown above. We need to separate each file with ; (semi colon).
# cat test; cat test1; cat test2 This is test file This is test1 file. This is test2 file.
9. Use Standard Output with Redirection Operator
We can redirect standard output of a file into a new file else existing file with ‘>‘ (greater than) symbol. Careful, existing contains of test1 will be overwritten by contains of test file.
# cat test > test1
10. Appending Standard Output with Redirection Operator
Appends in existing file with ‘>>‘ (double greater than) symbol. Here, contains of test file will be appended at the end of test1 file.
# cat test >> test1
11. Redirecting Standard Input with Redirection Operator
When you use the redirect with standard input ‘<‘ (less than symbol), it use file name test2 as a input for a command and output will be shown in a terminal.
# cat < test2 This is test2 file.
12. Redirecting Multiple Files Contain in a Single File
This will create a file called test3 and all output will be redirected in a newly created file.
# cat test test1 test2 > test3
13. Sorting Contains of Multiple Files in a Single File
This will create a file test4 and output of cat command is piped to sort and result will be redirected in a newly created file.
# cat test test1 test2 test3 | sort > test4
This article shows the basic commands that may help you to explore cat command. You may refer man page of cat command if you want to know more options.
相关推荐
opencv 3.2 installed in ~/opencv/opencv-3.2.0 sudo su mkdir build cd build 将ippicv_linux_20151201.tgz替换opencv-3.1.0/3rdparty/ippicv/downloads/linux-*目录下的同名文件 cp protobuf-cpp-3.1.0.tar.gz /...
Oracle Database 11gR2 (linux.x64_11gR2_examples.zip) 适用于Oracle Database 11g 第 2 版 Examples
基于arm S3C2440 linux内核下的AD采样驱动程序,并将其转换成电压值
Perfession Linux Programming examples
Telerik_UI_for_WPF_2015_1_0225_examples 示例源代码
了解_linux_process_examples 理解 Linux 进程的例子
Simulink®_for_Teaching_DSP_Examples
ros2_planning_system_examples
In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner ...
Solutions to the problems presented on the video series of how to git gud at dynamic programming
Part 13 Appendixes__________________________________ 674 38页的部份文档 Optimal Flexible Architecture The OFA is a set of recommendations from Oracle Corporation aimed at simplifying management of...
UIQ33beta_SDK_Symbian_Examples UIQ3的SDK实例
100-2217_getting_started_examples_oldesttbl_GettingStarted_tinyb
英文版 Data_Visualisation_with_R_100_Examples 100个案例 解析R可视化
GS2K_HTTP_HTTPS_EAP_UG_001213_HTTP_HTTPS_and_EAP_Examples_Guide Serial-to-WiFi AT Commands Configuration Examples To Create HTTP, HTTPS, and EAP Connections User Guide
SFS2X Basic Examples unity3d flash SFS2X_Basic_Examples包含unity3d和flash的源代码
spark_python_ml_examples, Spark 2.0 python 机器学习示例 Spark python-机器学习示例这个库是 Apache Spark 示例系列的一部分,旨在演示如何用Spark支持的不同编程语言实现机器学习解决方案的实现。 Java是唯一未...
This project contains few basic examples of [RabbitMQ Web STOMP plugin] usage. Once installed the server will bind to port 15670 and serve few static HTML files on port 15670 (e.g. ...
FLEX 基础教程代码实例,比较初级好用的代码,很适合初学者
symbin s60手机程序开发实例 carbide c ++ yuanchengxu