Cat command:
Linux cat command is a simple tool to open files in Linux. With cat command you can read the file content, create files, append content to already existing files, copy files etc. It is very helpful command to check the file content easily which will display on the screen. Below we will check the multiple examples for this cat command in Linux.
Linux cat command syntax:
cat [options] <file_name_1>
1. Create a file
To create a file use ‘>’ symbol with cat command. After writing to the file press Ctrl + D to save the file.
# cat > reports.txt
This is line 1
This is line 2
This is line 3
2. Append data to already existing file
To append data to the already existing file content use ‘>>’ with cat command. After writing to the file press Ctrl + D to save the file.
# cat >> reports.txt
Adding new line
Adding another line
3. To Display contents of the file
Display the file contents to the standard output
# cat reports.txt
This is line 1
This is line 2
This is line 3
Adding new line
Adding another line
4. Copy one file content to another file
We can copy one file contents to another file by using ‘>’ symbol in cat command. After this ‘sample.txt’ file contains data of reports.txt file
# cat reports.txt > sample_data.txt
If the destination file contains any data then this copy will override that existing data.
5. Append one file content to another file
To append source file data to the destination file use ‘>>’ in cat command. Source file data will be appended at the end of the destination file.
# cat reports.txt >> sample_data.txt
6. Append multiple files content to single file
To append multiple files content to single file use ‘>>’ in cat command. We can mention multiple file names in the cat command to append data to single destination file. With below command data from reports.txt, sample_data.txt and countries.txt will be appended to updated_data.txt file
# cat reports.txt sample_data.txt countries.txt >> updated_data.txt
7. Show end of the line in file
To display the end of the line with ‘$’ symbol so that we can find the end of the line. It will be useful to check any spaces in empty lines
# cat -A reports.txt
This is line 1$
This is line 2$
This is line 3$
$
8. Display line numbers for non-empty lines in file
Display the line numbers on every non empty output lines
# cat -b reports.txt
1 This is line 1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
9. Display line numbers for all lines in file
# cat -n reports.txt
1 This is line 1
2 This is line 2
3 This is line 3
4
5 This is line 4
6 This is line 5
10. Replace repeated multiple empty lines with single empty line
If a file contains multiple repeated empty lines then with -s option you can replace those multiple lines with single empty line.
# cat reports.txt
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
In above file we have multiple repeated empty lines. With below command we can replace those with single empty line.
# cat -s reports.txt
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
11. Show tab spaces in a file
In a file if tab spaces are available those will be shown as ^I with -t option in cat command.
# cat reports.txt
This is line 1
This is line 2 and adding some empty space here
This is line 3
# cat -t reports.txt
This is line 1
This is line 2^Iand adding some empty space here
This is line 3
12. Display multiple files contents
We can display multiple files content by using single cat command. All files contents will be shown in the standard output in a combined format.
# cat reports.txt sample_data.txt countries.txt
This is line 1
This is line 2
This is line 3
New line 1
New line 2
London
America
India
Russia
13. Display multiple files contents using wildcard characters in file names
For reading multiple files contents use wildcard (like *.txt, *.csv etc) characters instead of specifying all file names in cat command.
Below command will display contents of all files ending with .txt
# cat *.txt
Below command will display contents of all files ending with .csv
# cat *.csv
Below command will display contents of all files irrespective of file extensions. The wildcard character *.* will match all the files.
# cat *.*
14. Display file content using ‘more’ and ‘less’ options
If the file content is huge and you want to read the file page by page by pressing enter use more or less options with cat command. By default more and less options will display the content which will be sufficient to display on the screen based on the screen size. To come out from more or less viewing positions press ‘q’.
# cat reports.txt | more
# cat reports.txt | less
For more information on cat command please visit cat command manual site here.