Linuxopsys Profile picture
Jul 8 21 tweets 5 min read Twitter logo Read on Twitter
99% of Linux users use the cat command every day.

Despite this, most people do not take advantage of its full potential.

In this thread, I'll explain what the cat command is and show you some options you probably didn't know about (but you should):
The cat command is one of the most commonly used in Linux. The cat command gets its name from its ability to concatenate files. It has the ability to concatenate files, read and write file contents to standard output.
If you do not specify a filename to read from, or if the filename is replaced with a single hyphen (-), the data will be read from standard input (stdin)
Before we get into how to use the cat command, let's go over the basic syntax.

The syntax for the cat command is as follows:

$ cat [OPTION] [FILE]
- OPTION - to view the available options that are available use the --help (cat --help) option or its man page (man cat).

- FILE - 0 or more files to concatenate or read data from.
Now that you know what the cat command is and how its syntax, let's look at how it's used and some of the options it has.
1. Displaying contents of files

The most basic and common use of the cat command is to read the contents of a file and print it to standard output (stdout).

Here is an example of displaying contents of a specific file:

$ cat chkf [.] sh
2. Print line numbers

The cat command has excellent option (-n) you can use to check the count of lines on a file . If you ever had a large text document and needed to add lines to it, this option is your best friend.
Rather than going through the time-consuming process of manually numbering each line , you can simply use this option with cat from the command line and be done in a matter of seconds.

$ cat -n file

This command is equivalent to the 'nl file' command.
Rather than going through the time-consuming process of manually numbering each line , you can simply use this option with cat from the command line and be done in a matter of seconds.

$ cat -n file

This command is equivalent to the 'nl file' command.
You can then save the output to a file if you wish to, by just using output redirections operators (> and >>).

$ cat -s distros.txt > nolines.txt

The > operator will create a file (nolines.txt) if it doesn’t exist. Otherwise, it will overwrite the file.
If you wish not to overwrite the file use >> operator to append the contents to the end of nolines.txt file

$ cat -s distros.txt >> nolines.txt
4. Concatenating multiple files

With cat command you can also read (concatenate) contents of multiple files and display it to the standard output.

To do so you just separate the file names with spaces.
The following command, for example, will read the contents of file, file2, and file3 and display the results to your standard output (terminal screen)l:

$ cat file file2 file3
This becomes very handy if you want to combine the contents of different files into one file.
5. Distinguish between TABs and Spaces

If you want to visually distinguish between tabs and spaces, use the -T option in conjunction with the cat command.

Tabs will be replaced with the ^I characters.
6. Create files

To create files, use the cat command (without the file argument) followed by the redirection operator (>) and the name of the file you want to create. Press Enter, type your text, and then press CRTL+D to save the file.

$ cat > data.txt
Just like the touch command but cat allows you to have the ability to write text you want to be saved before actually creating a file. You can omit the text if you want to create an empty file.
If the file data.txt exists, it will be overwritten. To append the text to an existing file, use the '>>' operator.

$ cat >> data.txt

This is useful if you want to write to a file without having to open a full-fledged text editor.
7. Display end of lines characters

By default, line endings are not visible when displaying the contents of a file or files. If you want to display the invisible line ending character, use the -e option with the cat command:

$ cat -e data.txt
From the output, the $ character represents the line endings.
That's it!

Did you learn anything new from this thread? If so, please let us know by replying in the comments.

If you're new here, do toss us a follow us (@linuxopsys) for more threads, tips and resources on Linux.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Linuxopsys

Linuxopsys Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @linuxopsys

Jul 1
If you want to learn Linux for FREE, open this (a thread of our Linux threads):
1. Permissions

The operating system controls file access in Linux by utilizing file permissions, attributes, and ownership. In Linux, file permissions, attributes, and ownership determine the level of access that system programs and users have to files.

2. paste command in Linux explained

(simple yet powerful way to merge and organize data from multiple text files):

Learn more about the paste command in this master guide:

Read 41 tweets
Jun 30
If you want to learn Linux bash scripting for FREE, open this:
1. What is Shell in Linux

In this guide, let's learn about the shell which is an important part of the Linux operating system which interprets and passes the commands you provide on your keyboard to the operating system to perform some specific tasks

https://t.co/l8nXQcHskVlinuxopsys.com/topics/what-is…
2. Bash variable assignment

A variable is a named storage location in a program's memory where a value can be stored, retrieved, and manipulated. Like any programming language, Variables are an essential component in the Bash scripts.

https://t.co/x1Mdxf9xv6linuxopsys.com/topics/assign-…
Read 58 tweets
Jun 16
10 Linux tr command practical examples you should know as a system administrator (bookmark this): Image
The tr command short for translate, is one of the most useful command for manipulating text on the command line.
It allows you to perform useful operations such as converting lowercase characters to uppercase characters, uppercase characters to lowercase characters, character replacing, and deleting characters.

It is usually used in conjunction with other commands via piping.
Read 30 tweets
May 29
Linux command line substitution allows you to assign the output of a shell command to a variable. This one of the most useful feature of shell scripts.

Learn more in this guide: Image
After assigning the output to a variable, you can use the stored value anywhere in your shell scripts. This is useful when processing data in your scripts.

There are two ways to assign a command's output to a variable:

- The backtick character (`).

- The $() format.
To do so, encircle the entire command line command with two backtick characters:

$ dueDate=`date`

or use the $() format:

$ dueDate=$(date)
Read 11 tweets
May 16
In addition to UGO/RWX permissions and ACLs (Access Control Lists), Linux uses file and directory attributes to control the level of access that system programs and users have to files.

Let’s dive in! Image
File and directory attributes can be set and removed using different commands and utilities. Let's explore some commonly used attributes and how to manage them.
1. Immutable Attribute:

The immutable attribute prevents a file or directory from being modified, renamed, or deleted, even by the root user. It provides an extra layer of security to critical system files or sensitive data.
Read 23 tweets
May 14
If you want to learn Linux for FREE, open this (a thread of our Linux threads): Image
1. Permissions

The operating system controls file access in Linux by utilizing file permissions, attributes, and ownership. In Linux, file permissions, attributes, and ownership determine the level of access that system programs and users have to files.

2. paste command in Linux explained

(simple yet powerful way to merge and organize data from multiple text files):

Learn more about the paste command in this master guide:

Read 40 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(