cat

concatenate files and print on the standard output

Syntax

cat [OPTION]... [FILE]...

Description

Concatenate FILE(s), or standard input, to standard output.

Options

-b, --number-nonblank
              number nonempty output lines

-E, --show-ends
              display $ at end of each line

-s, --squeeze-blank
              suppress repeated empty output lines

-T, --show-tabs
              display TAB characters as ^I

-v, --show-nonprinting
              use ^ and M- notation, except for LFD and TAB

Example 1

Display content of a file to standard output.

[eleiss@eleiss ~]$ cat india 
India is a great country. Jai Hind.
[eleiss@eleiss ~]$

The command reads the content of a file given as an argument and prints on standard output.

Example 2

Read content of multiple files and print to standard output.

[eleiss@eleiss ~]$ cat quote 
most debugging problems are fixed easily; identifying the location of the problem is hard
[eleiss@eleiss ~]$ cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/zsh
[eleiss@eleiss ~]$ cat quote /etc/shells 
most debugging problems are fixed easily; identifying the location of the problem is hard
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/zsh
[eleiss@eleiss ~]$

It is simple. Just keep on writing all the file names you want to read. The content of all the files get displayed on standard output.

Example 3

Create a file using cat command.

[eleiss@eleiss ~]$ cat > computer
Computer is not as much intelligent as the human brain is.
[eleiss@eleiss ~]$ cat computer 
Computer is not as much intelligent as the human brain is.
[eleiss@eleiss ~]$

Using the > symbol, we can redirect the standard output to a file resulting in creation of a new file. Press Ctrl + d when content to the file is over.

Example 4

Append to a file using cat command.

[eleiss@eleiss ~]$ cat computer 
Computer is not as much intelligent as the human brain is.
[eleiss@eleiss ~]$ cat >> computer
Not even a supercomputer is.
[eleiss@eleiss ~]$ cat computer 
Computer is not as much intelligent as the human brain is.
Not even a supercomputer is.
[eleiss@eleiss ~]$ 

Appending to a file is another purpose of cat command.

Example 5

Copy a file using cat command.

[eleiss@eleiss ~]$ cat india 
India is a great country. Jai Hind.
[eleiss@eleiss ~]$ cat india > country
[eleiss@eleiss ~]$ cat country 
India is a great country. Jai Hind.
[eleiss@eleiss ~]$

File before > symbol is source file and file after > symbol is the destination file. If the destination file does not exist, then a new file is created. If destination file already exists, then it is truncated before copying.

Example 6

Concatenate two files

[eleiss@eleiss ~]$ cat computer 
Computer is not as much intelligent as the human brain is.
Not even a supercomputer is.
[eleiss@eleiss ~]$ cat india 
India is a great country. Jai Hind.
[eleiss@eleiss ~]$ cat india computer >> newfile
[eleiss@eleiss ~]$ cat newfile 
India is a great country. Jai Hind.
Computer is not as much intelligent as the human brain is.
Not even a supercomputer is.
[eleiss@eleiss ~]$

In the above command the two files "computer" and "india" are concatenated into a new file "newfile"


You may write to resources@eleiss.com for any Linux related queries.

advertisement