| title | cat Linux Command |
|---|---|
| description | cat command is used to create, read & concatenate files in Linux/ Unix machines |
| created | 2019-08-21 |
| updated | 2022-10-05 |
cat command is used to create, read & concatenate files in Linux/ Unix machines.
Lets create a file with name file1.txt and put following three lines of content into it.
First you need to run the following command to create a file
cat >file1.txtWhen you press enter after the above command, it waits for the file content input. You can give multi line input and once you are done you need to press CTRL + D to save and exit.
Following command shows the content of given file
cat file1.txtFollowing command shows content from file1.txt followed by content from file2.txt
cat file1.txt file2.txtFollowing command copies content from file1.txt to file2.txt
cat file1.txt > file2.txtFollowing command appends content of file1.txt to the end of file2.txt
cat file1.txt >> file2.txtFollowing command displays content of file1.txt in reverse order
tac file1.txtFollowing command displays content of -dashfile
cat -- "-dashfile"cat "filename" | morecat "filename1" "filename2" "filename3" > "merged_filename"Following command prefixes line numbers before each line from content
cat -n file1.txtFollowing is an example for that
$ cat -n file1.txt
1 line-1
2 line-2
3 line-3
4 line-4If a file having a large number of content that won’t fit in the output terminal and the screen scrolls up very fast, we can use parameters more and less with the cat command as shown below.
cat song.txt | more
cat song.txt | less| Option | Description |
|---|---|
-e |
Shows $ at the end of the content |
-t |
To see tabs in file, this shows tabs with ^I |
-s |
Removes repeated empty lines in content |
-E |
Highlights the end of line |