I've seen the cat command come up in some online stuff and I was curious as to what
cat did and what it's for in the context of someone who is just doing some simple command line stuff.
cat's primary purpose is to concatenate files.
cat file1 file2 ... will show the contents of
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
trutility. 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 (
sort, ...) can read files perfectly fine.
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]
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:
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
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
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
cat oldfile.txt > newfile.txt
Here is another way for file creation using shell redirection (or piping) and
cat << EOF > foo.sh #!/bin/sh echo 'Hello World!' EOF
cat << EOF will output the following lines until the string
EOF is encountered again. Then
>>) will redirect the output to the file
foo.sh. Alternatively, if you need to create a file as root, you can pipe the output to the command
tee -a), executed by the command
cat << EOF | sudo tee foo.sh && sudo chmod +x foo.sh #!/bin/sh HELLO='Hello World!' echo "\$HELLO" EOF
- The above will make the file executable (
chmod +x) if it is successfully created (
- The special meaning of some characters (as
$) should be escaped (with
\) within the output of