cat Command in Linux with Examples
cat Command
cat command is used to view the contents in the files. It cannot modify the original file but the contents can be moved to a new file.
Syntax
$ cat < operand> < file-name>
Display Contents of File
In the below example, it will show the contents of doc1
[user1@linuxhelp Desktop]$ cat doc1
Hi everyone....
Have a nice day!!!
To view Contents of Multiple Files
The following command can be used to view the contents of multiple files.
Example
[user1@linuxhelp Desktop]$ cat doc1 doc2
Hi everyone....
Have a nice day!!!
Linux is an opensource
Linux is free of cost and secure
Create a File with Cat Command
The following command can be used to create a new file.
Example
[user1@linuxhelp Desktop]$ cat > file1
linux
ubuntu
After writing the contents press CTRL+D (hold down Ctrl Key and press ‘ d‘ ) to exit. The text will be written in file1. You can see contents of file with following cat command.
[user1@linuxhelp Desktop]$ cat file1
linux
ubuntu
Display numbers of all the line in a file
The following command can be used to display the line number of each line in the file
Example
[user1@linuxhelp Desktop]$ cat -n doc2
1 Linux is an opensource
2
3
4
5
6 Linux is free of cost and secure
Use Standard Output with Redirection Operator
The following command can be used to redirect the output of a file into a new file with ‘ > ‘ (greater than) symbol.
The existing contents of file2 will be overwritten by contents of file1.
Example
[user1@linuxhelp Desktop]$ cat file1 linux Ubuntu [user1@linuxhelp Desktop]$ cat file1 > file2 [user1@linuxhelp Desktop]$ cat file2 linux Ubuntu
Here, the output of file2 is same as file1
Appending Standard Output
Appends in existing file with ‘ > > ’ (double greater than) symbol. Here,
Example
[user1@linuxhelp Desktop]$ cat > file2 fedora unix [user1@linuxhelp Desktop]$ cat file1 > > file2 [user1@linuxhelp Desktop]$ cat file2 fedora unix linux ubuntu
Redirecting Multiple Files Contain in a Single File
It will create a new file called file3 and all the contents of mentioned file will be redirected in that newly created file.
Example
[user1@linuxhelp Desktop]$ cat file1 file2 > file3
[user1@linuxhelp Desktop]$ cat file3
linux
ubuntufedora
unix
linux
ubuntu
Here, the contents of file1 and file2 are moved to file3.
Sorting the files
Sorting the contents of multiple files into a new single file and displays the output. First let we create some new files.
Example
[user1@linuxhelp Desktop]$ cat > file1 linux ubuntu [user1@linuxhelp Desktop]$ cat > file2 fedora unix [user1@linuxhelp Desktop]$ cat > file3 centos linux [user1@linuxhelp Desktop]$ cat file1 file2 file3 | sort > file4 [user1@linuxhelp Desktop]$ cat file4 centos fedora linux linux ubuntu unix
Here, all the three file’ s content are redirected to another new file and is sorted in alphabetic order. In the output you can see the three empty lines as it is in file1, file2, file3
Inserting $ at the end of the line and at the empty lines
This command inserts $ symbol at the end of the each line and the empty lines which is done using the option ' -e' . This can also be done using the ' -E' option.
Example
[user1@linuxhelp Desktop]$ cat -e doc2
Linux is an opensource$
$
$
$
Linux is free of cost and secure$
Displaying ' ^I' in the place of tab spaces
This operation is performed using the ' -T' option. It is helpful in detection of tab spaces in large files.
Example
[user1@linuxhelp Desktop]$ cat -T doc2
Linux is an^Iopensource
Linux is free of cost and^Isecure
Linux is free of cost and^Isecure
Viewing contents of a file in the reverse order
This command displays the output of the file in the reverse order, that is from bottom line to the top line
First let us checkout the contents in normal using cat command
Example
[user1@linuxhelp Desktop]$ cat doc2 Linux is an opensource Linux is free of cost and secure Now we are using the reverse command [user1@linuxhelp Desktop]$ tac doc2 Linux is free of cost and secure Linux is an opensource
You can see the difference between the normal car command and reversed cat command.
Displaying only the line number that carries text
In this command we use the option ' -b' . It is similar to the option -n but it just ignores the empty lines and displays number in ascending order.
Example
[user1@linuxhelp Desktop]$ cat -b doc2
1 Linux is an opensource
2 Linux is free of cost and secure
Displaying both the $ and ^I using a single option
This option gives the output as a combination both the -e and -T option
Example
[user1@linuxhelp Desktop]$ cat -A doc2
Linux is an^I opensource$
$
$
$
Linux is free of cost and^I secure$
Suppresses the empty lines and displays the contents of the file
The squeeze blank option ' -s' is used to suppresses the empty lines and shrinks the page size and displays the output.
Example
[user1@linuxhelp Desktop]$ cat -s doc2
Linux is an opensource
Linux is free of cost and secure
Displaying the version information about the cat
This command shows the output of the version information about the cat command and exits.
Example
[user1@linuxhelp ~]$ cat --version
cat (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later < http://gnu.org/licenses/gpl.html> .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Torbjorn Granlund and Richard M. Stallman.
Comments ( 0 )
No comments available