DZone
Open Source Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Open Source Zone > How the cat Command Works on Linux

How the cat Command Works on Linux

Let's learn how the cat command works on Linux

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Mar. 19, 22 · Open Source Zone · Tutorial
Like (4)
Save
Tweet
2.33K Views

Join the DZone community and get the full member experience.

Join For Free

cat, short for concatenate, is used on Linux and Unix-based systems like MacOS for reading the contents of a file, concatenating the contents with other files, and for creating new, concatenated files. It's also frequently used to copy the contents of files.

The syntax for cat is shown below, where x is the file name, and [OPTIONS] are optional settings which alter how cat works.

Shell
 
shell Copycat [OPTIONS] x


Getting the Contents of a File Using cat on Linux or MacOS

Using the cat command along with one file name, we can get the entire text content of a file. For example, the below command will output the content of my-file.txt into the terminal:

Shell
 
cat my-file.txt


Similarly, we can see the contents of many files by separating them with a space. For example, the below line takes the content of my-file.txt, and my-new-file.txt, merges the content, and shows it in terminal:

Shell
 
cat my-file.txt my-new-file.txt


Getting the contents of files with line numbers on Linux or MacOS
We can use the option -n to show line numbers. For example, the following command merges our two files, my-file.txt, and my-new-file.txt, and outputs the content with line numbers side by side. This is pretty useful for comparing files.

Shell
 
cat -n my-file.txt my-new-file.txt


The output will look something like this:

Plain Text
 
1   Content from my-file.txt     1  Some more content from my-new-file.txt


Concatenating Two Files Into a New File on Linux and MacOS

Since concatenate can output the contents of two files, we can use > again to merge two files into a totally new file. The below example takes my-file.txt and my-new-file.txt, merges their content, and puts it into a new file called my-combined-file.txt:

Shell
 
cat my-file.txt my-new-file.txt > my-combined-file.txt


Putting Content From One File Into Another With Linux or MacOS

If all we want to do is put the contents of one file at the end of another, we can instead use >>. For example, the below command will take the content from my-file.txt, and put it at the end of my-new-file.txt, thus merging both files into my-new-file.txt:

Shell
 
cat my-file.txt >> my-new-file.txt


Line Numbers

Note: if you use >> or > with the -n option, the line numbers will also be merged into your new concatenated file!

Creating an Empty File on Linux or MacOS With cat

Since it's so easy to create files with cat, we often use it to make new files. For example, the below code will create a blank file called my-file.txt, as we are concatenating a blank string into it:

Shell
 
cat > my-file.txt


How to Show Nonprintable Characters on Linux or MacOS

Some documents or files may contain nonprintable characters. These are used to signal to applications how a file should be formatted - but they can sometimes mess up the format of files. To show nonprintable characters when using cat, we can use the -v option. This will show all nonprintable characters using caret notation, so that we can view them easily.

Shell
 
cat -v my-file.txt


Nonprintable Characters

Non printable characters are signals for things like character encoding. You can find a full list of nonprintable, along with their caret notation which cat uses, here.

All Options for cat on Linux or MacOS

There are a bunch of other options which help us use cat to get the ouputs we want. We've already discussed -n for getting line numbers, and -v for nonprintable characters, but here are the others:

  • -b - numbers only non empty output lines, overriding -n.
  • -E - displays a $ at the end of every line.
  • -s - suppresses repeated, empty lines.
  • -T - displays tabs as ^I, so as to easily discriminate them from spaces.
  • -A - equivalent to writing -vET.
Linux (operating system) Command (computing) shell MacOS

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Most Popular Kubernetes Alternatives and Competitors
  • 10 Steps to Become an Outstanding Java Developer
  • 6 Things Startups Can Do to Avoid Tech Debt
  • How to Submit a Post to DZone

Comments

Open Source Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo