Linux Cat Command Usage and Examples
This post will guide you how to use cat command in Linux system. How do I use cat command to print the content of a given file under CentOS/RHEL/Ubuntu Linux.
NAME
cat – concatenate files and print on the standard output
SYNOPSIS
cat [OPTION]... [FILE]...
DESCRIPTION
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.
Linux Cat Command Examples
Dispaly a File with Cat command
If you want to print contents of a given file, you can use the cat command. For example, To show contents of /etc/passwd file, type the following command:
# cat /etc/passwd
Outputs:
devops@devops-osetc:~/working$ cat /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
View Contents from multiple files
If you want to print contetns of two or more fiels in Linux, you can use the following command:
# cat fio1.txt fio2.txt
Create a New file with Cat command
If you want to create a new file, you can also use the cat command to achieve it. Just type the following command to create a new file called fio.txt:
# cat >fio.txt
View A Large File with Cat Command
If you have a large file and you want to print the content of that file, and you can use the cat command with Less or more command, type:
# cat large.txt | less
Or
# cat large.txt | more
Display Line Numbers in File with Cat Command
If you want to print the contents of file with line numbers, and you can use cat command with -n option, type:
# cat -n /etc/passwd
Outputs:
devops@devops-osetc:~/working$ cat -n /etc/passwd 1 root:x:0:0:root:/root:/bin/bash 2 daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin 3 bin:x:2:2:bin:/bin:/usr/sbin/nologin 4 sys:x:3:3:sys:/dev:/usr/sbin/nologin 5 sync:x:4:65534:sync:/bin:/bin/sync 6 games:x:5:60:games:/usr/games:/usr/sbin/nologin
Appending Data to A File with Cat command
If you want to append data into an existing file, you can use the following command:
# cat >> fio.txt
Outputs:
devops@devops-osetc:~/working$ cat >>fio.txt sdfskdf sdkfsdkjf lsdfsjdf devops@devops-osetc:~/working$ devops@devops-osetc:~/working$ cat fio.txt sdfskdf sdkfsdkjf lsdfsjdf