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 <file>

# To concatenate several files into the target file:
cat <file1> <file2> > <target_file>

# To append several files into the target file:
cat <file1> <file2> >> <target_file>

# To number all output lines:
cat -n <file>

# To display non-printable and whitespace characters (with 'M-' prefix if non-ASCII):
cat -v -t -e <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