20

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.

4 Answers 4

14

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.

2
  • Redirection can very well be used with multiple files as you can combine it with process and command substitution: < <(cat file[12]) tr as well as <<<$(cat file[12]) tr are both perfectly valid ways to redirect multiple files. Of course you need cat for that as well, just that piping is not the only way.
    – dessert
    Jan 3, 2018 at 12:05
  • @dessert if you have to be pedantic, that redirection still uses only a single file - the file representing the command's stdout (or stdin, if the other way around). Bash doesn't support true multi-file redirection like zsh does.
    – muru
    Jan 3, 2018 at 12:19
12

cat is one of the most frequently used commands on Unix-like operating systems. It has three related functions with regard to text files:

  1. displaying them
  2. combining copies of them
  3. creating new ones.
  4. 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:

6
  • 1
    Re 3: cat isn't creating those files - the shell is when you used redirection. You might have as well done echo foo > file1.
    – muru
    Jul 1, 2015 at 6:09
  • @muru You are totally right, but it still a use of cat command
    – Maythux
    Jul 1, 2015 at 6:13
  • 1
    No, it isn't. You are attributing to cat what is actually being done by something else. For example, doing less file1 > file2 (or grep '.*' file1 > file or awk 1 file1 > file2 or ...) has the same effect. Ditto for 4. These are just misuses.
    – muru
    Jul 1, 2015 at 6:14
  • I know I could use it just > file and give same result, but can't I consider it valid to do that with cat?!
    – Maythux
    Jul 1, 2015 at 6:15
  • 1
    You can, if you specify that cat isn't creating the file. Saying cat creates or copies the file is just plain false.
    – muru
    Jul 1, 2015 at 6:16
1

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.

0

Here is another way for file creation using shell redirection (or piping) and cat:

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 (tee -a), executed by the command sudo:

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 cat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.