cat command in Linux / Unix with Examples

cat command

I am a new Linux and Unix system user. How do I use cat command on Linux or Unix-like operating systems? Can you provide basic examples and syntax usage for cat command?

The cat (short for concatenate) command is one of the most frequently used flexible commands on Linux, Apple Mac OS X, Unix, *BSD (FreeBSD / OpenBSD / NetBSD) operating systems.

The cat command is used for:

  1. Display text file on screen
  2. Read text file
  3. Create a new text file
  4. File concatenation
  5. Modifying file
  6. Combining text files
  7. Combining binary files


Basic file operation on a text file such as displaying or creating new files.

cat command syntax

The basic syntax is as follows:

cat filename


cat > filename


cat [options] filename


cat file1
cat > file2
cat file3 | command
cat file4 | grep something

cat command in Linux with examples

The cat command examples usage for linux unix users
It is a standard Unix program used to concatenate and display files. The cat command display file contents to a screen. Also, you can use cat command for quickly creating a file. The cat command can read and write data from standard input and output devices. Please note that some of the following option will only work with GNU version of the cat command.

Display A File With cat Command

To view a file, enter:

cat filename
cat /path/to/file
cat /etc/passwd

Sample outputs:

..hplip:x:109:7:HPLIP system user,,,:/var/run/hplip:/bin/false
vivek:x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bash
haldaemon:x:111:119:Hardware abstraction layer,,,:/var/run/hald:/bin/false
mysql:x:113:121:MySQL Server,,,:/var/lib/mysql:/bin/false

Create A File With cat Command

To create a file called “foo.txt”, enter:
cat >foo.txt
Type the following text:

This is a test.
Unix is the best.

You need to press [CTRL] + [D] i.e. hold the control key down, then tap d. The > symbol tells the Unix / Linux system that what is typed is to be stored into the file called foo.txt (see stdout for more information). To view a file you use cat command as follows:
cat foo.txt

Viewing A Large File With cat Command And Shell Pipes

If the file is too large to fit on the computer scree, the text will scroll down at high speed. You will be not able to read. To solve this problem pass the cat command output to the more or less command as follows:

cat bigfile | more
cat bigfile | less

The more and less command acts as shell filters. However, you can skip the cat command and directly use the Linux / Unix more & less command like this:

more bigfile
less bigfile

How To Combine Two Or More Files Using cat Command

You can combine two files and creates a new file called report.txt, enter:

cat score.txt names.txt > report.txt
cat report.txt

How To Append Data To A Text File

To append (add data to existing) data to a file called foo.txt, enter:

cat >>foo.txt

Type the text:

A champion is someone who gets up, even when he can't

Task: Number All Output Lines

Type the following command:

cat -n filename
cat --number filename

Sample outputs:

Unix / Linux Number All C Progam Code Ouput Lines

Fig.01: Number all output lines with cat command

How To View Non-printing Characters

To display TAB characters as ^I, enter:
cat -T filename
To display $ at end of each line, enter:

cat -E filename
cat --show-ends filename

Use ^ and M- notation, except for LFD and TAB and show all nonprinting:

cat -v filename
cat --show-nonprinting filename

To show all, enter:
cat -A fileName

cat -vET fileName
Sample outputs:

Unix / Linux cat: View Non-printing Characters Such as Tabs, New Line, End Of Line

Fig.02: Unix / Linux cat command: View Non-printing Characters

Viewing All Files

You can simply use the shell wildcard as follows:
cat *
To view only (c files) *.c files, enter:
cat *.c
Another option is bash for loop, or ksh for loop:

for f in /source/project10/*.pl
   echo "***** [Start $f ] ****"
   cat -n "$f"
   echo "***** [End $f ] ****"

OR same using the ksh shell:

for f in $(ls /source/project10/*.pl)
        print "*** [Start $f ] ****"
        cat  "$f"
        print "*** [End $f ] ****"

Print Files

You can directly send a file to to the printing device such as /dev/lp
cat resume.txt > /dev/lp
On modern systems /dev/lp may not exists and you need to print a file using tool such as lpr:
cat resume.txt | lpr
lpr resume.txt

Joining Binary Files

You can concatenate binary files. In good old days, most FTP / HTTP downloads were limited to 2GB. Sometime to save bandwidth files size were limited to 100MB. Let us use wget command to grab some files (say large.tar.gz was split to 3 file on remote url):
wget url/file1.bin
wget url/file2.bin
wget url/file3.bin

Now combine such files (downloaded *.bin) with the cat command easily:

cat file1.bin file2.bin file3.bin > large.tar.gz
### extract it 
tar -zxvf large.tar.gz

Another example with the rar command under Unix and Linux:

### First combine the files, and use the > shell redirection output to put the DVD image in a file ###
cat file.rar.001 file.rar.002 file.rar.003 file.rar.004 file.rar.005 > dvd.rar
## next unrar it ##
unrar e dvd.rar
## enjoy dvd ##
mplayer myfile.avi

Fooling Programs

You can use the cat command to fool many programs. In this example bc thinks that it is not running on terminals and it will not displays its copyright message. The default output:
bc -l
Samples session:

bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 

Now try with the cat command:
bc -l | cat
Samples session:


Testing Audio Device

You can send files to sound devices such as /dev/dsp or /dev/audio to make sure sound output and input is working:

cat filename >/dev/dsp
cat >/dev/audio

You can simply use the following command for recording voice sample and play back with it cat command:

dd bs=8k count=4 </dev/audio >
cat >/dev/audio

Gathering Linux System Information

### To see CPU information ###
cat /proc/cpuinfo
### To see memory information ###
cat /proc/meminfo
### To see Linux kernel version ### 
cat /proc/version

Display Large Blocks of Textual Data In A Script

You can use a here document for displaying large blocks of textual data in a script such as help:

  cat <<HELPEOF
   opt1 : Do this
   opt2 : Do that

Another working example:

# Author: Vivek Gite <
# -----------------------------------------------
#Set default to my-dev-box
# Now switch to prod 
[[ $HOSTNAME == "" ]] && BASEDIR="/etc/nixcraft/nginx/lb2"
[[ $HOSTNAME == "" ]] && BASEDIR="/etc/nixcraft/nginx/lb1"
 Usage $0
  -e | --enable:        Enable the nginx-chroot environment
  -E | --upgrade:       Upgrade bind and libs in the nginx-chroot environment
  -p | --php:           Enable the php-cgi in the nginx-chroot environment
  -P | --phpupgrade:    Upgrade the php-cgi in the nginx-chroot environment
  -i | --info:          Display the php-cgi and nginx environment information such as version, users, connections etc
        local uid=$(id -u)
        [[ $uid -ne 0 ]] && { echo "Only root may enable the nginx-chroot environment to the system."; exit 2; }
## function code removed to keep script short and sweet ##
# Make sure only root run this script
# Load local hooks
[ -f "${_hooks}" ] && . ${_hooks}
# Load os specifc paths
source ${_profile}
# Main logic 
case $1 in
    -e|--enable)    enable_nginix_chroot ;;
    -E|--upgrade)   upgrade_nginx_chroot;;
    -p|--php)       enable_php_cgi_nginx_chroot;;      
    -P|--phpupgrade)  upgrade_php_cgi_nginx_chroot;;      
    -i|--info)          get_nginx_chroot_info;;      
    *)            usage; exit 9999;

Print Files In Reverse

No cat cannot print in reverse order but cat command can concatenate and print files in reverse:

tac fileName
cat fileName | tac
tac <<<"$myFileName"

See “how to use a here documents to write data to a file in bash script” for more info.

cat command options

From the cat(1) command man page:

Option Meaning
-A Equivalent to -vET.
-b Number nonempty output lines.
-e Equivalent to -vE.
-E Display $ at end of each line.
-n Number all output lines.
-s Suppress repeated empty output lines.
-t Equivalent to -vT.
-T Display TAB characters as ^I.
-v Show nonprinting i.e. use ^ and M- notation, except for LFD and TAB.
--help Display this help and exit.
--version Output version information and exit.

Related media

This tutorial is also available in a quick video format:

See also
  • cat(1) Linux/Unix command man page

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 13 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
13 comments… add one
  • msl Jun 11, 2012 @ 11:33

    It is really *very* stupid to user “cat foo | grep bar” or “cat foo | less”. Instead you should use “grep pattern file” or “less file”.

  • joanna Sep 26, 2012 @ 9:48

    Maybe it’s stupid to use it this way but it’s good to show the wide range of options.

  • joanna Sep 28, 2012 @ 11:49

    Maybe it’s stupid but here are shown examples of cat. And in this case it’s absolutely right to show the possibilitys.

  • barton Dec 30, 2012 @ 11:15

    Helpful, although more so when things like
    cat /etc/passswd
    are spelled correctly (read “tested”)

  • RANGA SANTHOSH Jul 25, 2013 @ 8:45

    this is a nice way to represent for the people who really dont know the linux simple commands and basics

  • Poola Surendrareddy Aug 16, 2013 @ 10:45

    I have one problem in my system forgot the password how to recurring

  • Von Chanthy Jan 25, 2014 @ 4:17

    I assignment (Standard I/O and Pipes in red hat) I want any help.

  • UTTAM Jun 28, 2016 @ 15:35

    how to save file in Linux using cat..
    please reply me

    • fg Sep 14, 2016 @ 16:18

      1. cat > filename.txt
      2. type your text
      3. ctrl+D

  • coderkid Nov 21, 2016 @ 11:49

    how do you show the contents of a file. whilst highlighting a specific word everytime it comes up. really need some help i am inexperienced and this would benefit me massively.

    • Joachim Dec 2, 2016 @ 11:12

      Hi coderkid,
      Show contents of file:
      cat filename
      less filename

      Highlighting a special word:
      cat filename | grep word

  • Tiborr Jun 11, 2017 @ 10:40

    Hey guys can you give me a help with this question ?

    In the context of a linux operating system, which command can be used to display the syntax and parameters for a specific command?

    • 🐧 Vivek Gite Jun 11, 2017 @ 14:42

      man command_name
      commandname -h
      commandname --help
      cat --help
      man cat

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum