13 Basic Cat Command Examples in Linux
FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/
The cat (short for “concatenate“) command is one of the most frequently used command in Linux/Unix like operating systems. cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files. In this article, we are going to find out handy use of cat commands with their examples in Linux.

13 Basic Linux Cat Commands
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 cat command.
- # 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. In out next article we will cover more advanced cat commands. Please share it if you find this article useful through our comment box below.
13 Basic Cat Command Examples in Linux的更多相关文章
- 13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法
Cat (串联) 命令是Linux/Unix开源系统中比较常用的一个命令.我们可以通过Cat命令创建一个或多个文件,查看文件内容,串联文件并将内容输出到终端设备或新的文件当中,这篇文章我们将会以实例的 ...
- 15 Basic ‘ls’ Command Examples in Linux
FROM: http://www.tecmint.com/15-basic-ls-command-examples-in-linux/ ls command is one of the most fr ...
- 18 Tar Command Examples in Linux
FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ...
- 15 Practical Grep Command Examples In Linux / UNIX
You should get a grip on the Linux grep command. This is part of the on-going 15 Examples series, wh ...
- 【转】12 TOP Command Examples in Linux
12个top命令 1. # top 2. # top,后输入shift+O,在“Current Sort Field:”中选左边的field对应的字母进行排序. 3. # top -u tecmint ...
- 15 Linux Split and Join Command Examples to Manage Large Files--reference
by HIMANSHU ARORA on OCTOBER 16, 2012 http://www.thegeekstuff.com/2012/10/15-linux-split-and-join-co ...
- 12 Linux Which Command, Whatis Command, Whereis Command Examples
This Linux tutorial will explain the three "W" commands. The three "W"s are what ...
- 13个Cat命令管理文件实例汇总
在Linux系统中,大多数配置文件.日志文件,甚至shell脚本都使用文本文件格式,因此,Linux系统存在着多种文本编辑器,但当你仅仅想要查看一下这些文件的内容时,可使用一个简单的命令-cat. c ...
- 13个Cat命令管理(显示,排序,建立)文件实例
在Linux系统中,大多数配置文件.日志文件,甚至shell脚本都使用文本文件格式,因此,Linux系统存在着多种文本编辑器,但当你仅仅想要查看一下这些文件的内容时,可使用一个简单的命令-cat. c ...
随机推荐
- POJ 2186 受欢迎的牛 Tarjan基础题
#include<cstdio> #include<algorithm> #include<cstring> #include<vector> #inc ...
- latex公式测试
https://katex.org/docs/supported.html 1.x_n $x_n$ 2 . \sum $\sum$ \sum_{i=1}^n $\sum_{i=1}^n $ 3.\bi ...
- java中的URLEncoder.encode对应JS中用decodeURIComponent,js和java编码,解码
用get请求传中文,经常搞到乱码,这几天搞搞这个东西,总结一下,以方便以后处理这类的问题. Java代码中的URLEncoder.encode方法和JS的encodeURIComponent功能差不多 ...
- webpack最佳入门实践系列(5)
9.路径相关 原来我们打包的东西都存放到了dist目录下,并没有进行分类存储,乱成一团,这一节我们就要处理一下打包的路径,让打包后的目录看起来更加优雅 9.1.代码准备 我们先建立起这样一个目录结构 ...
- ztree 2.0 节点搜索
最近做了一个简单的功能,树节点的搜索.ztree的版本的2.0,3.X以上的版本请百度解决办法. 1.在setting中增加 function getFontCss(treeId, treeNode) ...
- poj 2079 Triangle (二维凸包旋转卡壳)
Triangle Time Limit: 3000MS Memory Limit: 30000KB 64bit IO Format: %I64d & %I64u Submit Stat ...
- 用vs2008和vs2005创建win32 console application
http://blog.sina.com.cn/s/blog_4900be890100s735.html 对于经常使用vc6.0的人来说,创建一个win32 console application很简 ...
- v4l2 spec 中文 Ch01【转】
转自:http://blog.csdn.net/wuhzossibility/article/details/6638245 目录(?)[-] Chapter 1 通用APICommon API El ...
- CentOS系统最小化安装没有wget解决方案
-bash: wget: command not found的两种解决方法 今天给服务器安装新LNMP环境时,wget 时提示 -bash:wget command not found,很明显没有安装 ...
- HDU 2546.饭卡-动态规划0-1背包
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...