cat
cat
(short for concatenate), is a basic terminal-based Linux program for manipulating text-based files. It is commonly used to quickly output the contents of a text file to the screen, so that you can read it without having to open an editor.
Some of the following examples use .txt
file extensions to indicate they contain textual information, but remember that this is optional, there is no notion of a “file extension” in Linux.
To Create A File
Now type the text you want in the file. Press CTRL-D
to exit and save the file. Warning: This will overwrite pre-existing files.
Output A File To The Screen
This basic command will print the contents of the text file to the terminal.
To Append To A File
Now type the text you want in the file. Press CTRL-D
to exit and save the file. Warning: This will overwrite pre-existing files.
Empty A File
This trick works by redirecting the null output to your file, which clears it’s contents.
Warning: This will delete all the contents of a file, without prompt.
To Output Line Numbers
Use the -n
switch.
Append/Concatenate Files To Another
You can use redirection (>
) to append the contents of one file into another.
You can list multiple files all at one to concatenate them into a single output file.