What is 'cat' used for?
Solution 1:
cat
's primary purpose is to concatenate files. cat file1 file2 ...
will show the contents of file
, file2
and the others one after the other, as if the contents were in a single file. See the manpage:
NAME
cat - concatenate and print files
It is meant for usage where either:
-
a target command cannot read from files and you need to pass multiple files to it. An example is the
tr
utility. Ordinarily, with one file, you'd do:tr < file
But with multiple files, redirection can't be used, so you have to do:
cat file1 file2 ... | tr
- a target command can read from multiple files, but its behaviour may change when it's given multiple files. An example is
wc
, which prints the counts for each file, along with the filenames, where you might have wanted just the total, without a filename.
Remember that most commands you encounter (grep
, sed
, awk
, sort
, ...) can read files perfectly fine.
If you want to view the contents of a file, use a pager - less
and more
are both eminently capable of presenting files for viewing, and are far more convenient to use.
Solution 2:
cat
is one of the most frequently used commands on Unix-like operating systems. It has three related functions with regard to text files:
- displaying them
- combining copies of them
- creating new ones.
- Copy files
cat's general syntax is:
cat [options] [filenames] [-] [filenames]
Reading Files
The most common use of cat is to read the contents of files, and cat is often the most convenient program for this purpose. All that is necessary to open a text file for viewing on the display monitor is to type the word cat followed by a space and the name of the file and then press the ENTER key. For example, the following will display the contents of a file named file1:
cat file1
Concatenation
The second role of cat is concatenation. (This is the source of cat's curious name.) There is no effect on the original files.
For example, the following command will concatenate copies of the contents of the three files file1, file2 and file3:
cat file1 file2 file3
The contents of each file will be displayed on the monitor screen. This output could just as easily be redirected using the output redirection operator to another file, such as file4, using the following:
cat file1 file2 file3 > file4
File Creation
Thanks to @muru comment : cat is capable of create new files depending on the shell redirection feature and not itself
For small files this is often easier than using vi, gedit or other text editors. It is accomplished by typing cat followed by the output redirection operator and the name of the file to be created, then pressing ENTER and finally simultaneously pressing the Ctrl & d keys.
For example, a new file named file1 can be created by typing
cat > file1
then press ENTER and simultaneously press the Ctrl & d keys.
PS1: If a file named file1 already exists, it will be overwritten
PS2: you can append to exited file using append operator >>
example cat >> file1
Copy Files
The cat command can also be used (depending on shell redirection feature) to create a new file and transfer to it the data from an existing file. Example: make a copy of file oldfile.txt
:
cat oldfile.txt > newfile.txt
References:
- Linux and Unix cat command
- The cat Command
- cat (Unix)
- HowTo: Use cat Command In Linux / UNIX
Solution 3:
It can be used to show the contents of a file. Eg: cat "my file.txt"
. You can also do man cat
to show you the details about what cat
can be used for.