Linux: cat command

来源:互联网 发布:c语言算法大全 编辑:程序博客网 时间:2024/05/18 12:40

About cat

cat stands for “catenate.” It reads data from files, and outputs their contents. It is the simplest way to display the contents of a file at the command line.


Purposes to use cat command under UNIX or Linux

1) Display text files on screen.
2) Copy text files.
3) Combine text files.
4) Create new text files.


cat command Syntax

cat filenamecat options filenamecat file1 file2cat file1 file2 > newcombinedfile

cat command options

-A, --show-all      equivalent to -vET-b, --number-nonblank   number nonempty output lines; overrides -n-e          equivalent to -vE-E, --show-ends     display "$" at end of each line-n, --number        number all output lines-s, --squeeze-blank suppress repeated empty output lines-t          equivalent to -vT-T, --show-tabs     display TAB characters as ^I-v, --show-nonprinting  use ^ and M- notation, except for LFD and TAB--help          display a help message and exit--version       output version information and exit

Examples

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/bashbin:x:1:1:bin:/bin:/sbin/nologinnarad:x:500:500::/home/narad:/bin/bash

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 everybodyHi world,

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 test2hello everyone, how do you do?

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

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.txt1  "Heal The World"2  There's A Place In3  Your Heart4  And I Know That It Is Love5  And This Place Could6  Be Much7  Brighter Than Tomorrow8  And If You Really Try9  You'll Find There's No Need10  To Cry11  In This Place You'll Feel12  There's No Hurt Or Sorrow

Display $ at the End of File

In the below, you can see with -e option that ‘isshowsattheendoflineandalsoinspaceshowing’ if there is any gap between paragraphs. This options is useful to squeeze multiple lines in a single line.

# cat -e testhello everyone, how do you do?$$Hey, am fine.$How's your training going on?$$

Display Tab separated Lines in File

In the below output, we could see TAB space is filled up with ‘^I’ character.

# cat -T testhello ^Ieveryone, how do you do?Hey, ^Iam fine.^I^IHow's your training ^Igoing on?Let's do ^Isome practice in Linux.

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 test2This is test fileThis is test1 file.This is test2 file.

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

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

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 < test2This is test2 file.

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

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

Reference

http://www.tecmint.com/13-basic-cat-command-examples-in-linux/
http://www.computerhope.com/unix/ucat.htm

0 0