How the cat Command works on Linux
cat, short for concatenate, is used on Linux and Unix-based systems like MacOS for reading the contents of a file, concatenating the contents with other files, and for creating new, concatenated files. It's also frequently used to copy the contents of files.
The syntax for
cat is shown below, where
x is the file name, and
[OPTIONS] are optional settings which alter how
cat [OPTIONS] x
Getting the contents of a file using cat on Linux or MacOS
cat command along with one file name, we can get the entire text content of a file. For example, the below command will output the content of
my-file.txt into the terminal:
Similarly, we can see the contents of many files by separating them with a space. For example, the below line takes the content of
my-new-file.txt, merges the content, and shows it in terminal:
cat my-file.txt my-new-file.txt
Getting the contents of files with line numbers on Linux or MacOS
We can use the option
-n to show line numbers. For example, the following command merges our two files,
my-new-file.txt, and outputs the content with line numbers side by side. This is pretty useful for comparing files.
cat -n my-file.txt my-new-file.txt
The output will look something like this:
1 Content from my-file.txt 1 Some more content from my-new-file.txt
Concatenating two files into a new file on Linux and MacOS
Since concatenate can output the contents of two files, we can use
> again to merge two files into a totally new file. The below example takes my-file.txt and my-new-file.txt, merges their content, and puts it into a new file called my-combined-file.txt:
cat my-file.txt my-new-file.txt > my-combined-file.txt
Putting Content from one file into another with Linux or MacOS
If all we want to do is put the contents of one file at the end of another, we can instead use
>>. For example, the below command will take the content from my-file.txt, and put it at the end of my-new-file.txt, thus merging both files into my-new-file.txt:
cat my-file.txt >> my-new-file.txt
Note: if you use
> with the
-n option, the line numbers will also be merged into your new concatenated file!
Creating an empty file on Linux or MacOS with cat
Since it's so easy to create files with
cat, we often use it to make new files. For example, the below code will create a blank file called my-file.txt, as we are concatenating a blank string into it:
cat > my-file.txt
How to show nonprintable characters on Linux or MacOS
Some documents or files may contain nonprintable characters. These are used to signal to applications how a file should be formatted - but they can sometimes mess up the format of files. To show nonprintable characters when using
cat, we can use the
-v option. This will show all nonprintable characters using caret notation, so that we can view them easily.
cat -v my-file.txt
Non printable characters are signals for things like character encoding. You can find a full list of nonprintable, along with their caret notation which cat uses, here.
All options for cat on Linux or MacOS
There are a bunch of other options which help us use
cat to get the ouputs we want. We've already discussed
-n for getting line numbers, and
-v for nonprintable characters, but here are the others:
-b- numbers only non empty output lines, overriding
-E- displays a
$at the end of every line.
-s- suppresses repeated, empty lines.
-T- displays tabs as
^I, so as to easily discriminate them from spaces.
-A- equivalent to writing
More Tips and Tricks for Linux
- How the alias Command works on Linux
- How to Remove Empty and Non Empty Directories in Linux
- How the cd command works in Linux
- How to Rename Files in Linux and MacOS Terminal
- Find all files containing a string or text on Linux and Mac
- How to make a Symbolic Link on Linux
- Speed up your Website by Converting your Images to WebP from Terminal
- How the find Command works on Linux
- How the cat Command works on Linux
- How the cp Command works on Linux