cat command in Linux with Examples



Name

cat - concatenate files and print the content on standard output.

Synopsis

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

Description

Concatenate FILE(s) to standard output. When no FILE is specified, or when FILE is -, this command reads from standard input.

Options

Tag Description
-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 (never more than one single blank line)
-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

Examples

1. Display contents of file. /etc/group

$ cat /etc/group
root:x:0:
daemon:x:1:
bin:x:2

2. Create a file with cat command. We will create a file named 'sample.txt' with cat command.

# cat > sample.txt
When you press Enter key, this command would wait for input from the user, the user can type the desired text, once the text typing is complete, press Enter key then press Ctrl+D (hold down Ctrl Key and type ‘d‘) to exit.

# cat > sample.txt
COVID-19 is a infectious diseases.
It is caused by a virus.
There is no antiviral treatment for COVID-19.
Ctrl+D (hold down Ctrl Key and type ‘d‘) to exit.

# cat sample.txt 
COVID-19 is a infectious disease.
It is caused by a virus.
There is no antiviral treatment for COVID-19.

To display content of a file covid.txt.

# cat covid.txt
Management involves the treatment of symptoms, supportive care, isolation, and experimental measures. 


The World Health Organization (WHO) declared the COVID 19 outbreak a public health emergency of international concern (PHEIC) on 30 January 2020 and a pandemic on 11 March 2020. 
Local transmission of the disease has occurred in most countries across all six WHO regions.

3. To concatenate two files sample_file.txt and covid.txt into one file

# cat sample.txt covid.txt > combined_file.txt
# cat combined_file.txt
COVID-19 is a infectious disease.
It is caused by a virus.
There is no antiviral treatment for COVID-19.
Management involves the treatment of symptoms, supportive care, isolation, and experimental measures. 


The World Health Organization (WHO) declared the COVID19 outbreak a public health emergency of international concern (PHEIC) on 30 January 2020 and a pandemic on 11 March 2020. 
Local transmission of the disease has occurred in most countries across all six WHO regions.

4. To put content of a file in a variable, we can use backtic command syntax to store the output from cat command. We can verify the contents of the variable with echo command.

$ variable_content=`cat sample.txt`

$ echo $variable_content
COVID-19 is a infectious disease. It is caused by a virus. There is no antiviral treatment for COVID-19.

5. Display line numbers in file use -n option.

$ cat -n covid.txt
1	Management involves the treatment of symptoms, 
2	supportive care, isolation, and experimental measures. 
3	
4	
5  The World Health Organization (WHO) declared the COVID 19 
6	outbreak a public health emergency of international concern (PHEIC) 
7	on 30 January 2020 and a pandemic on 11 March 2020. 
8  Local transmission of the disease has occurred 
9	in most countries across all six WHO regions.

6. Use -b option to display line numbers only for non-blank output lines.

$ cat -b covid.txt
1	Management involves the treatment of symptoms, 
2	supportive care, isolation, and experimental measures. 


3  The World Health Organization (WHO) declared the COVID19 
4	outbreak a public health emergency of international concern (PHEIC) 
5	on 30 January 2020 and a pandemic on 11 March 2020. 
6  Local transmission of the disease has occurred 
7	in most countries across all six WHO regions.

You can see the after line number 2, there is a blank line, but it is not numbered.

7. Use -s option to suppress repeated adjacent blank lines; output just one empty line instead of several lines.

$ cat -n -s covid.txt
1	Management involves the treatment of symptoms, 
2	supportive care, isolation, and experimental measures. 
3	
4  The World Health Organization (WHO) declared the COVID19 
5	outbreak a public health emergency of international concern (PHEIC) 
6	on 30 January 2020 and a pandemic on 11 March 2020. 
7  Local transmission of the disease has occurred 
8	in most countries across all six WHO regions.

You can see, one extra blank line after line number 2 has been squeezed out from the output.

8. Use -T option to display TAB characters represented as ^I in the text output.

# cat -T covid.txt 
Management involves the treatment of symptoms, 
supportive care, isolation, and experimental measures. 


^IThe World Health Organization (WHO) declared the COVID19 
outbreak a public health emergency of international concern (PHEIC) 
on 30 January 2020 and a pandemic on 11 March 2020. 
^ILocal transmission of the disease has occurred 
in most countries across all six WHO regions.

9. Use -E option to display a ‘$’ after the end of each line.

# cat -E sample.txt 
COVID-19 is a infectious disease.$
It is caused by a virus.$
There is no antiviral treatment for COVID-19.$

10. Concatenate a file to an existing file using >> (double greater than) symbol. Here contents of file 'sample.txt' will be appended to the end of file 'covid.txt'.

$ cat sample.txt >> covid.txt 

$ cat covid.txt 
Management involves the treatment of symptoms, 
supportive care, isolation, and experimental measures. 


The World Health Organization (WHO) declared the COVID19 
outbreak a public health emergency of international concern (PHEIC) 
on 30 January 2020 and a pandemic on 11 March 2020. 
Local transmission of the disease has occurred 
in most countries across all six WHO regions.
COVID-19 is a infectious disease.
It is caused by a virus.
There is no antiviral treatment for COVID-19.	
Advertisements