title | description | created | updated |
---|---|---|---|
cat Linux Command |
cat command is used to create, read & concatenate files in Linux/ Unix machines |
2019-08-21 |
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.txt
When 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.txt
Following command shows content from file1.txt followed by content from file2.txt
cat file1.txt file2.txt
Following command copies content from file1.txt to file2.txt
cat file1.txt > file2.txt
Following command appends content of file1.txt to the end of file2.txt
cat file1.txt >> file2.txt
Following command displays content of file1.txt in reverse order
tac file1.txt
Following command displays content of -dashfile
cat -- "-dashfile"
cat "filename" | more
cat "filename1" "filename2" "filename3" > "merged_filename"
Following command prefixes line numbers before each line from content
cat -n file1.txt
Following is an example for that
$ cat -n file1.txt
1 line-1
2 line-2
3 line-3
4 line-4
If 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 |