CAT

cat

Date Published:
Last Modified:

Overview

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

$ cat >filename.txt

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.

$ cat filename.txt

To Append To A File

$ cat >>filename.txt

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.

$ cat /dev/null > filename.txt

Warning: This will delete all the contents of a file, without prompt.

To Output Line Numbers

Use the -n switch.

$ cat -n filename.txt

Append/Concatenate Files To Another

You can use redirection (>) to append the contents of one file into another.

$ cat file1 > file 2

You can list multiple files all at one to concatenate them into a single output file.

$ cat file1 file2 file3 > file4

Like this page? Upvote with shurikens!

Tags:

comments powered by Disqus