EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

CAT Command in Linux

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Linux Tutorial » CAT Command in Linux

CAT-COMMAND-IN-LINUX

Introduction to CAT Command in Linux

The cat command is termed “concatenate” which is one of the widely used commands in Unix/Linux & Apple Mac OS operating system. This command has different application like create single/ multiple files, view the content of the file, concatenate file and redirect their output to the terminal or file. The cat command displays the file content to a screen. Cat command concatenates standard inputs to the standard output. When there is no FILE or when FILE is – then it read the standard input? It performs three main roles related to manipulation of text files: creating them, displaying them & combining them.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

cat OPTIONS FILE_NAME

Options

  • A: It is used to show all the content of the file.
  • b: This option is used to give a number with nonblank/nonempty output lines and overrides the option -n.
  • e: It is almost equal to option -vE
  • E: It is will display the end of the file.
  • n: This option will give a number to each line is the file in ascending order.
  • s: This option will suppress the multiple empty output lines.
  • t: This option is similar to option  -vT
  • T: It is used to display the special Tab characters like ^I
  • u: It is used to ignore the things.
  • v: It is used to show non-printable characters used ^ and M notations.
  • help: This option is used to get detailed information about the cat command.
  • version: Using this option we can know the version information of cat command.

Examples to Implement in CAT Command in Linux

Let us discuss with the example to clarify our understanding:

Example #1

Create a New File: Using Cat command we can easily create a file. Let us create a file with name Testfile.txt and write the content “This is my First command in Linux.

Command: Cat > Testfile.txt

After adding the content press Cntrl + D to exit the shell and write the content to file.

CAT Command in Linux -1

Example #2

How to Display the Content of a File: In order to show the content of the file, we can use cat command simply and pass the name of the file/files which we want to view. The output of the file will be printed on the standard output and can be viewed on the terminal.

Command: cat Testfile.txt

Output:

CAT Command in Linux -2

If the contents of a file are very long, then also all the content will be written to the terminal. In such cases, it will be challenging to discover or identify all the contents. When searching for particular content grep will be a good option.

Popular Course in this category
Kali Linux Training (3 Courses, 3+ Projects)3 Online Courses | 3 Hands-on Projects | 21+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,494 ratings)
Course Price

View Course

Related Courses
Linux Training Program (16 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

Example #3

Writing the Content of Any File to a New File: In order to write the contents of a file to a new file “shell redirection” will be used with the cat command. In the below example suppose that the file Testfile.txt has a single line with ‘This is my First Command in Linux’ and write this into File_1.txt.

Command:

cat Testfile.txt > File_1.txt
cat File_1.txt

Output:

Writing the Content of Any File to a New File

Note: New File_1.txt is created and the content of Testfile.txt is copied to the File_1.txt.In case if the File_1.txt already exists then it will use the existing file at the path

Example #4

Append the Content of a File to Another File: If we need to append the content of any file to another file, the shell redirection will be used. For example, Consider we have two files Shop.txt and Location.txt. The file Shop.txt contains two lines.

Command:

cat Shop.txt
Retail Shop
Wine Shop

The file Location.txt  file also contains two lines:

Command:

cat Location.txt
Manali,HP
Indore,MP

** Now we will use shell redirection(>>) to append the content of the two files:

Command: cat Shop.txt >> Location.txt

Command: cat Location.txt

Output:

CAT Command in Linux -4

Example #5

Merging of Multiple Files Into Unified File: In order to combine multiple files into a single file, cat command will be used with shell redirection. The below-mentioned command will combine all files with the *.txt extension inside a directory into one file in the same or different directory.

Note: * is a special character in Linux/Unix which means all.

Command: cat *.txt > merge.txt

Command: cat merge.txt

Output:

Merging of Multiple Files Into Unified File

You can remember the content combined from Testfile.txt,File_1.txt,Shop.txt and Location.txt as all have the .txt extension.

Example #6

Adding Line Number to the Cat Output: To add line number to the output of cat command we will use the -n option. It will prepend the numerical value to the start of each line.

Command: cat -n merge.txt

Output:

CAT Command in Linux -6

You can see the numbers are given to each line in ascending order.

Example #7

Showing the End of the Line: If we want to know the end of lines in a file then we will use cat command with the -E option. It will append a “$” character to the end of the lines. This option is useful to check when there is trailing space in the file. For example, let us use the same merge.txt.

Command: cat -E merge.txt

Output1:

Showing the End of the Line

In the above output, there is no space at the end of the line.

Now let us add some lines with space then we will see a tab space between the end line and “$”

Output2:

CAT Command in Linux -8

Example #8

Squeezing or Compressing of Blank Lines: In order to squeeze/compress blank lines in a file, we can use a cat with the -s option. It will squeeze the blank line then suppress repeating empty output lines. Suppose we have file line.txt, which contains the below data.

  • File is squeezed
  • File is Compresses
  • File is Suppressed
  • File is squeezed

Command:cat -s line.txt

Output:

CAT Command in Linux -9

We can see that duplicate records have been removed.

Example #9

Showing Tabs in the File: To show tab we can use cat with  -T option. It will show tabs as ^I.

File tabs.txt contains:

  • 1 tab
  • 2 tab

Command: cat -T tabs.txt

Output:

Showing Tabs in the File

Example #10

Version

Command: cat --version

Output:

Version

Recommended Articles

This is a guide to CAT Command in Linux. Here we discuss the syntax and 10 different examples to implement cat command in Linux with proper codes and output. You can also go through our other related articles to learn more –

  1. Tar Command in Linux
  2. GCC Command in Linux
  3. IP Command in Linux
  4. Chown Command in Linux

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
Linux Tutorial
  • Linux Commands
    • CAT Command in Linux
    • PS command in Linux
    • Mkdir Command in Linux
    • rmdir command in Linux
    • mv command in Linux
    • Linux rm Command
    • CHMOD Command in Linux
    • Zip Command in Linux
    • Linux Route Command
    • SCP Command in Linux
    • du Command in Linux
    • Less Command in Linux
    • Copy Command in Linux
    • Head Command in Linux
    • Help Command in Linux
    • Linux History Command
    • hostname Command in Linux
    • GREP Command in Linux
    • Linux Curl Command
    • Linux List Users
    • Linux List Directories
    • Linux Directory Permissions
    • Find Directory Linux
    • Linux User Permission
    • Linux Add User
    • Linux Delete User
    • Linux uniq
    • Linux Filter Commands
    • Linux sort Command
    • Linux Sort by Size
    • Remove dir Linux
    • Linux tr Command
    • Linux Touch Command
    • Linux More Command
    • Linux Cal Command
    • Linux OD
    • Linux Zcat
    • Linux tracepath
    • Linux Sleep
    • Linux bg Command
    • Linux WC
    • Linux Read
    • Linux Write
    • Linux Rename Command
    • Linux Free Command
    • Linux tac
    • Linux Shred
    • Linux wget
    • Linux Date
    • Linux Untar
    • Linux JQ
    • Linux Xargs
    • Linux Mount Command
    • Linux mdadm
    • Linux comm
    • Linux tee Command
    • Tail Command in Linux
    • Linux gzip
    • Linux Directory Structure
    • Linux Alias Command
    • Linux Inode
    • Linux Ping
    • Linux Watch Command
    • Linux Split Command
    • Linux Size
    • Linux Network Stack
    • Linux Diff Command
    • Vim Command in Linux
    • Linux nslookup
  • Basic
    • Introduction to Linux
    • What is Linux
    • Careers in Linux
    • How to Install Linux
    • How to Install Ubuntu
    • How to Install Ubuntu Server
    • How To Install Debian
    • Advantage Of Linux
    • What is Raid in Linux
    • What is Raid 5?
    • Raid 5 vs Raid 6
    • Careers in Linux Administration
    • What is Shell in Linux?
    • Bash Shell in Linux
    • Linux Emulator
    • Linux Operators
    • Linux Apps
    • Linux Keyboard Shortcuts
    • Linux Crontab
    • What is Raid Storage
    • What is Vmware
    • What is RedHat
    • What is Debian
    • Uses of Ubuntu
    • Career In RedHat
    • Linux Distributions
    • Linux From Scratch
    • Linux Format
    • Cheat Sheet Linux
  • Advanced
    • Install VMware
    • Install GRUB
    • Chown Command in Linux
    • Linux Network Command
    • Configuring Linux Network Environment
    • DNS Configuration in Linux
    • Linux Users
    • Create User in Linux
    • DHCP Server in Linux
    • FTP Server in Linux
    • Linux Regular Expression
    • IP Command in Linux
    • Set IP Address in Linux
    • Tar Command in Linux
    • Dynamic Host Configuration Protocol
    • Linux Kill Signals
    • Linux File Permissions
    • Linux File System
    • Linux Create File
    • Linux Concatenate Files
    • Linux Join
    • Linux Process Management
    • Linux Proxy Server
    • Linux System Commands
    • Linux System Logging
    • NFS in Linux
    • Linux LVM
    • Linux Repository
    • Samba Server in Linux
    • Top Commands in Linux
    • Types of Shells in Linux
    • Shell Script Types For Linux
    • GCC Command in Linux
    • Monolithic Kernel
    • YARN Package Manager
    • Linux Sed Replace
    • Linux List Groups
    • Linux Container
    • Linux Find File by Name
    • Create Folder in Linux
    • Linux Move Folder
    • Linux Error Codes
    • Linux Automount
    • Linux HugePages
  • Kali Linux
    • Install Kali Linux
    • Kali Linux Uses
    • Kali Linux Commands
    • Kali Linux Repository
    • Kali Linux Packages
    • Kali Linux root password
    • Kali Linux Alternative
  • Interview Questions
    • Linux Interview Questions
    • Linux System Administration Interview
    • Red Hat Interview Questions

Related Courses

Kali Linux Training

Linux Training Course

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Kali Linux Training Learn More