Networking/Computing Tips/Tricks

The 'cat' [short for “concatenate“] command is one of the most frequently used commands in Linux and other operating systems. The cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files.

The command is available in Linux, Windows Power Shell, and MAC-OS.

Here is the Windows usage screen for cat:

winpscat

Here is a Linux cat help screen:

ubuntucathelp

We understand there are lots of options, now let's look at some usage examples.

1. The most basic and repetitive use of cat is to display the contents of a file.  If you are using Linux, the following example will show contents of /etc/passwd file:

# cat /etc/passwd

linuxcat1

2. You can use cat to create a simple file.  Let's create a file called 'example1':

# cat >example1

Now simply enter the information you want in the file followed by the CTRL+D (hold down Ctrl Key and type ‘d‘) to exit. The information you typed will be written into the example1 file.

linuxcat2

You can verify this using the following command:

# cat example1

 linuxcat3

3. We can take that a step further and view content of multiple files where 'example1' and 'example2' are files:

# cat example1 example2

linuxcat4

4. Another way to display multiple files is to use the ';' between cat commands:

# cat example1; cat example2; cat example3

linuxcat5

5. Sometimes the output of displaying a file runs off the screen.  Managing how much information the cat command displays is simple.  The cat command comes with the ability to control the screen output with 'more' or 'less':

# cat bigfile.txt | more

linuxcat6

One screen worth of the file will be shown at a time with the "More" word at the bottom.  Just hit the space bar to display the next screen.

# cat bigfile.txt | less

This time the ":" will be displayed.  Just hit 'Q' to quit.

6. In the old days, line numbering was important in programming.  Today, sometimes when helping someone find a particular item, it is helpful to have line numbers in the display.  The cat command can use the '-n' option to display line numbers:

# cat -n bigfile.txt

linuxcat7

7. You can use cat to redirect output from the terminal window (default output) to say another file.  This is called the redirection operator.  This is done with the ‘>‘ (greater than) symbol. Be careful here, as you may overwrite an existing file!!

# cat example1 > example3

Essentially we have created a copy of example1 in example3.

8. OK - so what is we had an existing example3 file and we wanted to append example1 contents onto example3?  The cat command supports this with the ‘>>‘ (double greater than) symbol. 

# cat example1 >> example3

9. Taking that a step further, what is we wanted to send contents of multiple files and create a single file?  The cat command does this as follows:

# cat example1 example2 example3 > example4

These files are added in order.

10. Taking that process even further, what if we wanted to do the same thing as #9 but this time sort the contents?  The cat command can do this easily with piping to the 'sort' process:

# cat example1 example2 example3 | sort > example4

11. OK - what if we wanted to create a file with some set of commands we use regularly and that as input?  The cat command does this with the ‘<‘ (less than symbol):

# cat < example5

 

Great stuff.  We hope we have you off and running with the cat command.  We certainly did not cover every option, and encourage you to learn more about this powerful little command. 

What other uses of the cat command would you add to our list?  Add a comment below!

Comments powered by CComment

Find by Tag

4G Networks 5G Networks 6LoWLAN 6LoWPAN 802.11 802.11ah 802.11ax 802.11ay 802.11az Ad-Hoc Addressing Analysis Ansible Apple Architecture ARP Assessment AToM Automation Baseline BGP Bloom's Taxonomy Bluehost BPF Briefings Cable Capture Filter cat CellStream Cellular Central Office Cheat Sheet Chrome Cisco Cloud CMD Company Policy Computer Consulting Data Center Data Networking Dependencies DHCPv6 Display Filter DNS Documentation dumpcap Earth Earthquakes ECMP Ethernet Ethics Etiquette Evaluation Field Operations Five Monkey Rule G-MPLS Gauge GeoIP GNS3 Google GQUIC Hands-On History Home Network ICMP ICMPv6 IEEE 802.11p IEEE 802.15.4 India Internet IoT IPv4 IPv6 IRINN IS-IS L2VPN L3VPN LDP LifeNet Linux LLN LoL M-BGP MAC Macro Microsoft Milky Way mininet Monitoring MPLS mtr Multicast Murphy Name Resolution Netcat NetMon netsh Networking nmap NSE Observations OLPC Online School OpenFlow OSPF OSPFv2 OSPFv3 OSX OTT Parrot PIM Policy POTS POTS to Pipes PPP Profile Project Management PW3E QoS QUIC Railroad Remote Desktop Requirements Resume Review RIP Routig Routing RPL RSVP Rural SDN Security Service Provider Small Business SONET Speed SSL Status Storms Subnetting Support SYSCTL T-Shark TCP TCP/IP Telco Telecom 101 Telecommunications Telephone Testing Tools Traceroute Traffic Engineering Training Travel Tunnel Ubuntu Utility Video Virtualbox Virtualization VoIP VRF VXLAN Wi-Fi Windows Wireless Wireless 5G Wireshark WLAN Writing Zenmap ZigBee

Twitter Feed