cat Linux command for create, view or concatenate files

Main picture of the post: cat linux command for create view or concatenate files.

The cat command is another basic and frequently used from Linux and it’s very useful when it comes to view files, create new ones, modify or combine them.

The cat command syntax

The syntax of the command is the following:

cat [OPTION]… [FILE]…

These are some of the options admitted by cat:

-E: Display $ at end of each line

T : Display TAB characters as ^I

-b : Number nonempty output lines, overrides -n

A : Show all. Equivalent to -vET

Some of the options mentioned above and others can be combined in the same cat command line.

Create a file and write the content into it with cat

By running the command in the following way, using the output redirector “>”, you can create a file and write content into it.

$ cat > new_file
This is a cat command test. It won't return the prompt until Ctrl^D or Ctrl^C is pressed
$ ls -l
total 8
-rw-rw-r--. 1 fse fse   89 Apr 12 23:01 new_file
-rw-rw-r--. 1 fse fse 2331 Apr 12 22:55 test

While cat is running, you can continue to write content into the target file until you press “Ctrl^D”, which will stop the command and write what you typed into the file. In the other hand, if “Ctrl^C” is pressed instead, the command will stop but nothing will be written on any file.

Print the content of a file with different options

The most frequent use case is to print the content of the whole file with cat command:

$ cat new_file 
This is a cat command test. It won't return the prompt until Ctrl^D or Ctrl^C is pressed

You can add the “-E” to mark the end lines with a “$” symbol:

$ cat -E new_file 
This is a cat command test. It won't return the prompt until Ctrl^D or Ctrl^C is pressed$

Use “-T” to mark the TABs of the file with “^I”:

$ cat -T new_file 
This is a cat command test. It won't return the prompt until ^ICtrl^D or Ctrl^C is pressed

By using “-b”, the cat command will mark the number of nonempty lines of the file:

$ cat -b new_file 
     1  This is a cat command test. It won't return the prompt until    Ctrl^D or Ctrl^C is pressed

The “-A” option will result the same as combining “-T” and “-E”:

$ cat -A new_file 
This is a cat command test. It won't return the prompt until ^ICtrl^D or Ctrl^C is pressed$

Using cat in script to create and write a file with multiple lines

This could be an interesting way to run cat command when using it in a script, for creating a config or settings file with multiple lines instead of echo command:

$ cat > multiple <<EOL
> cat with
> multiple
> lines
> in
> script
> EOL

The resulting file will look like:

$ cat multiple 
cat with
multiple
lines
in
script

To do so, at the end of the command it needs the “<<EOL” sequence and at the final of the content “EOL” with a new line.

Append or add new text at the end of a file

To append new text to the end of an existing file, the output redirector “>>” will be used:

$ cat >> new_file 
This is the new text

Again, to stop the cat command and write down the new text press Ctrl+d. The result will be:

$ cat new_file 
This is a cat command test. It won't return the prompt until    Ctrl^D or Ctrl^C is pressed
This is the new text

It is also possible to append the content of a file at the end of another file:

$ cat file1 
world
$ cat file2 
hello
$ cat file1 >> file2 
$ cat file2 
hello
world

Create a new file with the content of two or more files appended

It is possible to print multiple files using the same cat command line and append the output to a new file:

$ cat text1 text2 text3
Hi
there
!
$ cat text1 text2 text3 > appended
$ cat appended 
Hi
there
!

Location or full path of cat command

The binary of the cat command is normally located in /bin/cat:

$ which cat
/bin/cat
$ ls -l /bin/cat
-rwxr-xr-x. 1 root root 54080 Nov 16  2020 /bin/cat