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的更多相关文章

  1. 13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法

    Cat (串联) 命令是Linux/Unix开源系统中比较常用的一个命令.我们可以通过Cat命令创建一个或多个文件,查看文件内容,串联文件并将内容输出到终端设备或新的文件当中,这篇文章我们将会以实例的 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 【转】12 TOP Command Examples in Linux

    12个top命令 1. # top 2. # top,后输入shift+O,在“Current Sort Field:”中选左边的field对应的字母进行排序. 3. # top -u tecmint ...

  6. 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 ...

  7. 12 Linux Which Command, Whatis Command, Whereis Command Examples

    This Linux tutorial will explain the three "W" commands. The three "W"s are what ...

  8. 13个Cat命令管理文件实例汇总

    在Linux系统中,大多数配置文件.日志文件,甚至shell脚本都使用文本文件格式,因此,Linux系统存在着多种文本编辑器,但当你仅仅想要查看一下这些文件的内容时,可使用一个简单的命令-cat. c ...

  9. 13个Cat命令管理(显示,排序,建立)文件实例

    在Linux系统中,大多数配置文件.日志文件,甚至shell脚本都使用文本文件格式,因此,Linux系统存在着多种文本编辑器,但当你仅仅想要查看一下这些文件的内容时,可使用一个简单的命令-cat. c ...

随机推荐

  1. [codeforces] 25E Test || hash

    原题 给你三个字符串,找一个字符串(它的子串含有以上三个字符串),输出此字符串的长度. 先暴力判断是否有包含,消减需要匹配的串的数量.因为只有三个字符串,所以暴力枚举三个串的位置关系,对三个串跑好哈希 ...

  2. 洛谷 P2609 [ZJOI2012]数列 解题报告

    P2609 [ZJOI2012]数列 题目描述 小白和小蓝在一起上数学课,下课后老师留了一道作业,求下面这个数列的通项公式: A(0)=0 A(1)=1 A(2i)=A(i) (对于任意 i>0 ...

  3. git bash 如何进入某个盘的特定文件夹

    下面是用gitbash进入E:\the fifth version of ecard\epayment这个文件夹下 命令如下图所示:

  4. 通过OpenGL ES在iOS平台实践增强现实

    http://www.cnblogs.com/elvisyzhao/p/3398250.html 本文采用OpenGL ES 1固定渲染管线实现,目标为在设备拍摄到的现实世界中,绘制世界坐标轴,并根据 ...

  5. Objective-C类族和工厂模式

    http://www.cocoachina.com/ios/20141124/10296.html 相信大家都了解GoF的<Design Patterns>中提到的23种设计模式,其中将常 ...

  6. Linux getcwd()的实现【转】

    转自:http://www.cnblogs.com/alan-forever/p/3721908.html 通过getcwd()可以获取当前工作目录. 1 #include <unistd.h& ...

  7. Python’s super() considered super!

    如果你没有被Python的super()惊愕过,那么要么是你不了解它的威力,要么就是你不知道如何高效地使用它. 有许多介绍super()的文章,这一篇与其它文章的不同之处在于: 提供了实例 阐述了它的 ...

  8. IBM Security AppScan Standard WEB扫描工具

    IBM Security AppScan Standard是一款著名的web漏洞扫描工具, 可以设定登录账户,录制登录 扫描完成后可以生成报告,生成的报告非常详细

  9. python每日一类(3):os和sys

    os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...

  10. python每日一类(1):pathlib

    每天学习一个python的类(大多数都是第三方的),聚沙成金. -------------------------------------------------------------------- ...