cat is short for 'concatenate' i.e. combining two or more files but it does way more than that! The cat command is also used for creating and viewing text files. We will see some real life cat command usage with practical examples.
cat [options] [file]
|-b||Omits line numbers for blank space in the output.|
|-e||A $ character will be attached at the end of each line prior to a new line.|
|-n||Display line numbers for all the output lines.|
|-s||If the output has multiple empty lines it replaces it with one empty line.|
|T||Displays the tab characters in the output.|
|-A||Show all (combine -T and -e)|
|v||displays nonprinting characters as if they were visible, except for tabs and the end of line character.|
Usage with Examples:
1. Create a new file and add text to it.
when you're done writing text press Ctrl+d, it marks the end of file. Note: If a file named example already exists then all its contents will be overwritten. Here we are actually redirecting the standard output to a file which in our case is example.
$ cat > example just some text bla bla bla
2. Display an existing file.
Now, lets view the contents of the example file we just created. just use the filename as the paramerter to cat.
$ cat example just some text bla bla bla
3. Append more text into a existing file
Here, the text will be added (appended) at the end of the file and not overwritten.
$ cat >> example adding some more text
4. Concatenate several files and display
this will combine two or more files and show it in standard output (terminal)
$ cat file1 file2
5. Concatenate several files and copy the output to another file
here, we use redirection to copy the output to another file.
$ cat file1 file2 > file3
6. Use more and less parameters
For viewing large files with cat, the content will not fit in the terminal and scroll down very fast. we can combine cat with other command like more or less. Use the pipe '|' to combine it.
now, simply use the up/down arrow key to scroll the file.
$ cat file | less
$ cat file | more
7. Display Numbered line in the output of a file
here, we use the -n option. There are many more options (look in the above table)
$ cat -n file1 1 There's a lady who's sure all that glitters is gold 2 And she's buying a stairway to heaven 3 4 When she gets there she knows, if the stores are all closed 5 With a word she can get what she came for
8. Display Numbers on for non-blank lines only in the output of a file
$ cat -b file1 1 There's a lady who's sure all that glitters is gold 2 And she's buying a stairway to heaven 3 When she gets there she knows, if the stores are all closed 4 With a word she can get what she came for
The Tabs in the file are replaced by '^I' character.
$ cat -T file def counter(string): ^Ic=0 ^Ifor x in string: ^I^Ic+=1 ^Ireturn c def sayhello: ^Iprint 'Hello'
10. Display $ at the end of each line
so, with this help of $ at the end of each line, we can identify whitespaces at the end of each line. we use the -e option here.
$ cat -e file4 just some random text $ bla bla bla$ $ some more bla bla$
Go ahead and try more options with cat command (see the table above).
And, if you know any useful cat command that you use a lot, please comment below.