Skip to main content

The cat command reads files sequentially, writing them to the standard output.

# To print the contents of a file to the standard output:
cat <path/to/file>

# To concatenate several files into an output file:
cat <path/to/file1 path/to/file2 ...> > <path/to/output_file>

# To append several files to an output file:
cat <path/to/file1 path/to/file2 ...> >> <path/to/output_file>

# To copy the contents of a file into an output file without buffering:
cat -u </dev/tty12> > </dev/tty13>

# To write 'stdin' to a file:
cat - > <path/to/file>

# ---

# read `file1` and write output to `file2`
cat file1 > file2

# concatenate copies of the contents; sort and save them to `file4`
cat file1 file2 file3 | sort > file4

# create `file` and edit contents form term
cat > file1 # NOTE: Ctrl+D to end edit and close file

# To display contents with line numbers (blank lines excluded)
cat -b file

# create an apache virtual host with cat here doc
cat << EOF > httpd-vhosts.conf

####### Virtual Host setup for $1 ###########

<VirtualHost www.$1 $1>
ServerName www.$1
ServerAdmin $2
DocumentRoot $docroot/$1
ErrorLog logs/$1/error_log
TransferLog logs/$1/access_log
</VirtualHost>

<Directory $docroot/$1>
Options Indexes FollowSymLinks Includes
AllowOverride All
order allow,deny
allow from all
</Directory>

EOF