Cyberithub

20 Popular Examples of cat command in Linux/Unix for Beginners

Table of Contents

Advertisements

In this article, I will take you through the different usages of cat command in Linux. cat command is also abbreviated as "concatenate".  It is the most popular opensource tool in Linux and Unix based Systems to show/view contents of the files. You can also perform operations like search, redirect, append etc with the help of other tools like grep, more, less etc in Linux/Unix. You can also use cat command in regular expressions with awk and sed command in Linux. We will try to see all the important examples and usage of cat command in Linux/Unix.

Syntax

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

Advertisements

 

20 Popular Examples of cat command in Linux/Unix for Beginners 1

Examples of cat command in Linux/Unix

Also Read: Best Steps to install Perl on RedHat/CentOS 7

Example 1: How to check cat command version

If you want to check the current cat command version then you need to use cat --version command as shown below. As you can see from below output current cat command version is 8.22.

Advertisements
[root@localhost ~]# cat --version
cat (GNU coreutils) 8.22
Copyright (C) 2013 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 Torbjörn Granlund and Richard M. Stallman.

--version : output version information and exit.

NOTE:

Advertisements
Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

Example 2: How to check the contents of a File using cat command in Linux/Unix

If you want to check the contents of a file then you need to simply use the filename with cat command as shown below. In this example, we are trying to view the contents of testfile.txt using cat testfile.txt command.

[root@localhost ~]# cat testfile.txt
This is cyberithub
This is CYBERITHUB

Example 3: How to display Line number in the output using cat command in Linux/Unix

If you want to display Line number in the output then you need to use -n option as shown below. In this example, we are trying to display the Line number of testfile.txt content output by using cat -n testfile.txt command.

Advertisements
[root@localhost ~]# cat -n testfile.txt
1 This is cyberithub
2 This is CYBERITHUB

-n : number all output lines. More on cat Man Page.

Example 4: How to display $ at the end of each Line using cat command in Linux

If you want to display $ at the end of each line of the file output then you need to use -E option as shown below. In this example, we are trying to add $ at the end of each line of the testfile.txt output using cat -E testfile.txt command.

[root@localhost ~]# cat -E testfile.txt
This is cyberithub$
This is CYBERITHUB$

-E : display $ at end of each line.

Example 5: How to number all non-empty output Lines using cat command in Linux/Unix

If you want to number all non-empty output Lines of a file then you need to use -b option as shown below. In this example, we are trying to show all non-empty output lines of testfile.txt file using cat -b testfile.txt command.

[root@localhost ~]# cat -b testfile.txt
1 This is cyberithub
2 This is CYBERITHUB

-b : number nonempty output lines, overrides -n. More on cat Man Page.

Example 6: How to Suppress repeated empty output Lines using cat command in Linux

If you want to suppress repeated empty output Lines then you need to use -s option as shown below. In this example, we are trying to suppress empty output Lines from testfile.txt using cat -s testfile.txt command.

[root@localhost ~]# cat -s testfile.txt
This is cyberithub
This is CYBERITHUB

-s : suppress repeated empty output lines.

Example 7: How to View Contents of Multiple Files using cat command in Linux/Unix

If you want to view the contents of multiple files in a single cat command then you need to provide all the file name as shown below. In this example, we are trying to view the contents of testfile.txt and hosts file using cat testfile.txt hosts as shown below.

[root@localhost ~]# cat testfile.txt hosts
This is cyberithub
This is CYBERITHUB

test@192.168.0.106
root@192.168.0.105

Example 8: How to fill all the TAB Spaces with ^I using cat command in Linux

If you want to fill all the TAB Spaces with ^I in the output then you need to use -T option as shown below. In this example, we are trying to fill ^I in every TAB spaces of the output of testfile.txt file using cat -T testfile.txt command.

[root@localhost ~]# cat -T testfile.txt
This^Iis cyberithub
This^Iis CYBERITHUB

-T : display TAB characters as ^I

Example 9: How to Replace Contents of one file with another file contents

If you want replace complete contents of a file with another file contents then you need to use redirection operator > as shown below. In this example, we are trying to replace contents of hosts file with the contents of testfile.txt using cat testfile.txt > hosts command.

[root@localhost ~]# cat testfile.txt > hosts
[root@localhost ~]# cat hosts
This is cyberithub
This is CYBERITHUB

Example 10: How to Append Content of a file to another file 

If you want append complete contents of a file into another file then you need to use append operator >> as shown below. In this example, we are trying to append the contents of testfile.txt file to the hosts file using cat testfile.txt >> hosts command.

[root@localhost ~]# cat testfile.txt >> hosts
[root@localhost ~]# cat hosts
test@192.168.0.106
root@192.168.0.105
This is cyberithub
This is CYBERITHUB

Example 11: How to Run multiple cat commands in a Single Line

If you want to run multiple cat commands in a Single Line, then you need to use the semicolon(;) to separate out the cat commands as shown below. In this example, we are trying to run two different cat commands to show the contents of testfile.txt and hosts files using cat testfile.txt;cat hosts command.

[root@localhost ~]# cat testfile.txt;cat hosts
This is cyberithub
This is CYBERITHUB

test@192.168.0.106
root@192.168.0.105

Example 12: How to redirect File Contents to Standard Input

If you want to redirect all the file contents to Standard Input then you need use < operator as shown below. In this example, we are trying to show the contents of testfile.txt on Standard Input using cat < testfile.txt command.

[root@localhost ~]# cat < testfile.txt
This is cyberithub
This is CYBERITHUB

Example 13: How to display non-printing characters using cat command in Linux/Unix

If you want to display non-printing characters then you need to use -v option as shown below. In this example, we are trying to display the non-printing characters of ls command using cat -v /usr/bin/ls command. This will turn the non-printable character to printable character.

[root@localhost ~]# cat -v /usr/bin/ls
^?ELF^B^A^A^@^@^@^@^@^@^@^@^@^B^@>^@^A^@^@^@$C@^@^@^@^@^@@^@^@^@^@^@^@^@M-hM-C^A^@^@^@^@^@^@^@^@^@@^@8^@ ^@@^@^^^@^]^@^F^@^@^@^E^@^@^@@^@^@^@^@^@^@^@@^@@^@^@^@^@^@@^@@^@^@^@^@^@M-x^A^@^@^@^@^@^@M-x^A^@^@^@^@^@^@^H^@^@^@^@^@^@^@^C^@^@^@^D^@^@^@8^B^@^@^@^@^@^@8^B@^@^@^@^@^@8^B@^@^@^@^@^@^\^@^@^@^@^@^@^@^\^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^A^@^@^@^E^@^@^@^@^@^@^@^@^@^@^@^@^@@^@^@^@^@^@^@^@@^@^@^@^@^@M-^LM-^U^A^@^@^@^@^@M-^LM-^U^A^@^@^@^@^@^@^@ ^@^@^@^@^@^A^@^@^@^F^@^@^@(M-#^A^@^@^@^@^@(M-#a^@^@^@^@^@(M-#a^@^@^@^@^@M-X^R^@^@^@^@^@^@M-x^_^@^@^@^@^@^@^@^@ ^@^@^@^@^@^B^@^@^@^F^@^@^@M-(M--^A^@^@^@^@^@M-(M--a^@^@^@^@^@M-(M--a^@^@^@^@^@^@^B^@^@^@^@^@^@^@^B^@^@^@^@^@^@^H^@

Example 14: How to check cat command output with more option in Linux/Unix

If you want cat command output to be loaded completely and then navigate page by page then you might think about using more command with cat as shown below. In this example, we are loading entire contents of sshd_config file and then navigating page by page using cat /etc/ssh/sshd_config | more command.

[root@localhost ~]# cat /etc/ssh/sshd_config | more
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

Example 15: How to Check cat command output with less option in Linux/Unix

If you do not want cat command output to be loaded completely and only load page by page then you might think about using less command instead of using more command with cat as shown below. In this example, we are loading contents of sshd_config file page by page and navigating it  using cat /etc/ssh/sshd_config | less command.

[root@localhost ~]# cat /etc/ssh/sshd_config | less
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

Example 16: How to use -A option instead of -vET option with cat command in Linux

If you do not want to use -vET option with cat command then you have an alternative of using only -A option as shown below. In this example, we are trying to use -A option with cat command for testfile.txt file using cat -A testfile.txt command.

[root@localhost ~]# cat -A testfile.txt
This^Iis cyberithub$
This^Iis CYBERITHUB$
$

-A : equivalent to -vET

Example 17: How to remove duplicate Lines from a File in Linux/Unix

If you want to remove duplicates Lines from a File in Linux/Unix Based systems then you need to use sort and uniq commands with cat command in Linux. In this example, we are using below testfile.txt file which contains duplicate Lines.

[root@localhost ~]# cat testfile.txt
This is cyberithub
This is CYBERITHUB
This is cyberithub

Now when you use sort and uniq command with cat command then you will be able to remove the duplication as shown below.

[root@localhost ~]# cat testfile.txt | sort | uniq

This is cyberithub
This is CYBERITHUB

Example 18: How to display First Column of Every Line using awk with cat command in Linux/Unix

If you want to display the first column of every Line in testfile.txt then you need to use awk command with it as described below. In this example we are trying to display first column of every Line of testfile.txt using cat testfile.txt | awk '{ print $1 };' command.

[root@localhost ~]# cat testfile.txt | awk '{ print $1 };'
This
This
This

Example 19: How to display only first Line using sed with cat command in Linux/Unix

If you are planning to display only the first Line of a file then you need to use sed command with cat command in Linux. In this example, we are trying to display first Line of testfile.txt using cat testfile.txt | sed -n 1p command.

[root@localhost ~]# cat testfile.txt | sed -n 1p
This is cyberithub

Example 20: Check all Other Options of cat command in Linux

If you want to check all the other options available with cat command in Linux then you need to use cat --help command as shown below.

[root@localhost ~]# cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.

-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
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit

With no FILE, or when FILE is -, read standard input.

Examples:
cat f - g Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'cat invocation'

 

 

 

Popular Recommendations:-

Understanding Kafka Console Producer and Consumer in 10 Easy Steps

Popular firewalld examples to open a port on RedHat/CentOS 7

8 Most Popular mkdir command in Linux with Examples

26 Useful Firewall CMD Examples on RedHat/CentOS 7

Using 3 Steps – How to create an IAM User and attach policy in AWS

10 Best s3cmd Command Examples for AWS Cloud Administrators

Popular Apache Kafka Architecture Explained Using 4 Basic Components

5 Easy Steps to recover LVM2 Partition , PV , VG , LVM metadata in Linux

For Beginners: Create an EC2 Instance in AWS with 7 Easy Steps

Install AWS CLI – AWS Command Line Interface

Install AWS CLI Version 2 – AWS Command Line Interface

Leave a Comment