How do I use cat command on Linux or Unix-like operating systems?
cat is one of the most frequently used flexible commands on Linux, Apple Mac OS X, Unix, *BSD (FreeBSD / OpenBSD / NetBSD) operating systems.
- View A File
- Create A File
- Cat and Pipes
- Combine Files
- Append To A File
- Number All Lines
- Non-printing Characters
- View Large Number Of Files
- Print Files
- Joining Binary Files
- Fooling Programs
- Testing Audio Device
- Gathering Linux System Information
- Display Large Blocks of Textual Data In A Script
- Print Files In Reverse
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.
Syntax:
The syntax is as follows:
cat file1 cat > file2 cat file3 | command cat file4 | grep something
Task: Display A File
To view a file, enter:
cat filename cat /path/to/file cat /etc/passwd
Sample outputs:
root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh .... ... ..hplip:x:109:7:HPLIP system user,,,:/var/run/hplip:/bin/false vivek:x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bash bind:x:110:118::/var/cache/bind:/bin/false haldaemon:x:111:119:Hardware abstraction layer,,,:/var/run/hald:/bin/false sshd:x:112:65534::/var/run/sshd:/usr/sbin/nologin mysql:x:113:121:MySQL Server,,,:/var/lib/mysql:/bin/false
Task: Create A File
To create a file called "foo.txt", enter:
cat >foo.txt
Type the following text:
This is a test. Unix is the best. <control-D>
You need to press [CTRL] + [D]
cat foo.txt
Task: 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
Task: Combine Two Or More Files
You can combine two files and creates a new file called report.txt, enter:
cat score.txt names.txt > report.txt cat report.txt
Task: 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 <control-D>
Task: Number All Output Lines
Type the following command:
cat -n filename cat --number filename
Sample outputs:
Task: 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
OR
cat -vET fileName
Sample outputs:
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:
#!/bin/bash for f in /source/project10/*.pl do echo "***** [Start $f ] ****" cat -n "$f" echo "***** [End $f ] ****" done
OR same using the ksh shell:
#!/bin/ksh for f in $(ls /source/project10/*.pl) do print "*** [Start $f ] ****" cat "$f" print "*** [End $f ] ****" done
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
OR
lpr resume.txt
Joining Binary Files
You can concatenate binary files. In old good days most ftp / http downloads were limited to 2GB. Sometime to save bandwidth files size were limited to 100MB. You can combine such files with cat 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'. 4+5 9 quit
Now try with the cat command:
bc -l | cat
Samples session:
4+5 9 quit
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 recording.au >/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 >testing123.au cat testing123.au >/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
Usage:
opt1 : Do this
opt2 : Do that
HELPEOF
Another working example:
#!/bin/bash # Author: Vivek Gite <http://www.cyberciti.biz/ # ----------------------------------------------- #Set default to my-dev-box BASEDIR="/home/vivek/projects/bash/nginx-keepalived/chroot" # Now switch to prod [[ $HOSTNAME == "lb2.nixcraft.net.in" ]] && BASEDIR="/etc/nixcraft/nginx/lb2" [[ $HOSTNAME == "lb2.nixcraft.net.in" ]] && BASEDIR="/etc/nixcraft/nginx/lb1" _profile="$BASEDIR/redhat.conf" _etc_files="$BASEDIR/redhat.etc.files.conf" _etc_dirs="$BASEDIR/redhat.etc.dirs.conf" _hooks="$BASEDIR/hooks.sh" usage(){ cat<<EOF 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 EOF } rootuser(){ 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 ## enable_nginix_chroot(){ : } upgrade_nginx_chroot(){ : } enable_php_cgi_nginx_chroot(){ : } upgrade_php_cgi_nginx_chroot(){ : } get_nginx_chroot_info(){ : } # Make sure only root run this script rootuser # 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; esac
Print Files In Reverse
No cat cannot print in reverse order but tac command can concatenate and print files in reverse:
tac fileName cat fileName | tac tac <<<"$myFileName"
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop















{ 4 comments… read them below or add one }
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”.
Maybe it’s stupid to use it this way but it’s good to show the wide range of options.
Maybe it’s stupid but here are shown examples of cat. And in this case it’s absolutely right to show the possibilitys.
Helpful, although more so when things like
cat /etc/passswd
are spelled correctly (read “tested”)
Thanks.