Categories
Design

How to use the linux cat and tac commands

Display and combine files from a shell prompt

  • Tweet
  • Share
  • Email

The cat command in Linux concatenate files and displays the output to the standard output (usually, the shell).

One of the most common uses of cat is to display a file to the screen and also to create a file on the fly and allow basic editing straight at the terminal.

How to use the linux cat and tac commands

How to Create a File Using ‘cat’

To create a file using the cat command enter the following in the terminal window:

When you create a file in this manner, the cursor will be left on a new line, and you can start typing. This technique offers a great way to start a text file. To finish editing the file, press Ctrl+D. The file saves with whatever you used for filename.

Test that the process worked by typing the ls command:

You should see your new file, and the size should be greater than zero.

How to Display a File Using ‘cat’

The cat command displays a file to the screen as well. All you need to do is eliminate the greater than symbol as follows:

To view the file page by page use the more command:

Alternatively, you can use the less command as well:

How to Show Line Numbers

For all the non-empty lines in a file use the following command:

If there are lines with no characters at all they won’t be numbered. To show numbers for all the lines regardless as to whether they are blank, type the following command:

How to Show the End of Each Line

Sometimes when parsing data files, programmers discover problems because there are hidden characters at the end of lines that they weren’t expecting — such as spaces. This error prevents their parsers from working correctly.

To show the dollar as an end of line character enter the following command:

As an example look at the following line of text

When you run this with the cat -E command you receive the following output:

Reducing Blank Lines

When you show the contents of a file using the cat command you probably don’t want to see when there are loads of consecutive blank lines. Use the -s switch to condense all blank lines into a single blank line:

How to Show Tabs

When you display a file that uses tab delimiters, you won’t ordinarily see the tabs.

The following command shows ^I instead of the tab, which makes it easy to see them:

Concatenate Multiple Files

The whole point of cat is concatenation. Concatenate several files to the screen with the following command:

To concatenate the files and create a new file use the following command:

Showing Files in Reverse Order

Show a file in reverse order by using the following command:

Technically this isn’t the cat command, it is the tac command, but it essentially does the same thing but in reverse.

Cat command is a well known Unix utility that reads files sequentially. Writing them to conventional output. The name is derived from its function for concatenating and listing the documents. Tac (that is “cat” backwards) concatenates every record to traditional output much like the cat command. However in opposite: line-by means of-line, printing the last line first. This article explains about “How to use ‘cat’ and ‘tac’ commands with examples”.

The basic example of cat command should be like this –

The above command is to read files and display them to stdout, meaning to display the content of files on your terminal. The sample output should be like this –

Another usage of the cat command is to study or combine a couple of files together and ship the output to a monitor as shown below –

The sample output should be like this –

This command can also be used to concatenate (join) multiple files into one single file using the “>” Linux as shown below –

The above command joins text.txt and text2.txt and concatenates the data into a different file- text3.txt file. The sample output should be like this –

The cat command is also used to copy the content from one file to an other new file. The new (updated) file can be renamed as arbitrary.

For example, copy the following file from the current location to /tmp/ directory as shown below –

The above command copies text.txt file data to file.txt file. The sample output should be like this –

A less usage of the cat command is to create a new file with the below command –

The sample output should be like this –

Usage of Tac Command in Linux

Tac is almost the reverse model of cat command (additionally spelled backwards) which prints every line of a report beginning from the lowest line and completing on the top line in your gadget trendy output. Sample example should be like this –

The sample output should be like this –

The most important usage of tac command is that, it can provide great help in order to debug log files, even reversing the chronological order of log contents.

The sample example should be like this –

The sample output should be like this –

After this article, you will be able to understand – How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux, we will come up with more Linux based tricks and tips. Keep reading!

The cat and the more commands are used to display the contents of a file or files on the terminal in Linux. Both commands have similar functionality but are still different. How are they similar as well as different at the same time? Let’s find that out now.

Table of Contents

1. The cat command

Short for concatenate, the cat command is very frequently used for displaying the entire contents of a file or multiple files at once. Like most other commands in Linux, the basic syntax for the cat command is cat . For example, I have a config file named file1 in my directory. Let’s list out the contents of the file.

As you can see, it outputs the contents of the file right away. Let’s look at the command in a little more detail.

1.1) Listing content of multiple files with cat command

This part is really simple. All you’d do is enter the file names one after the other in the same line. I’ve created two files for this tutorial. Let’s see how we can list out the contents of both the files.

1.2) Creating and manipulating files using the cat command

We can create, a new file, enter the contents and save the file with just one command. Let’s look at an example. Enter the command below.

1.2.1) Creating a file

Once you enter the command you’ll be presented with no output, which is an indication for you to start entering the information that you wish to have in the file. Once done, press the Ctrl+D button to stop editing.

1.2.2) Appending text to a file

If you run the same command on an existing file with text written in it, you’ll overwrite the file. If you want to append some data to an existing text file, we’ll use the append (>>) operator in Linux.

1.2.3) Output content into a new file

If you quickly want to output the contents of a file into a new file, we will make use of the output redirect (>) operator in Linux. The below command will create a new file numbers-copy which has the output of the command “cat numbers” stored in it.

This operator can be used with any other command as well allowing you to log a command output in a file for later use.

1.2.4) Reversing the output of a file

Since the cat command offers us the output of the file, the tac command will reverse the output that the cat command gives. Interesting isn’t it? Let’s see it in action!

I’ve created a file named “numbers” with numbers from 1 to 5 in a list format. Here’s how the tac command will output for the file.

1.3) Numbering the output lines

Okay, now these are really small files but what if we want to number the lines that are produced as output? Let’s make use of our first command option here. It’s the -n option, which is short for “numbered”. Similar to the other commands we’ve learned previously, you enter this option right after the cat command. Let’s see an example below:

1.4) Other common cat command options

To highlight the end of a line and understand where there are empty spaces, the -E option comes handy. It adds a $ sign wherever a line ends. As you can see in the example, I had an empty line in the numbers file, which is highlighted by the cat command with a $ sign.

If you want to display non-printing characters, line endings, and the tabs, you could add the -v (for non-printing characters) -T (for tabs) and -E (for line endings). Your cat command would look something like this.

Or, you could just use the -a option which stands for “all”. This will output the contents along with the outputs that you’d get by combining the three options I mentioned above.

2. The more command in Linux

Similar to the cat command, the more command outputs the contents of the file on the terminal STDOUT. So why should we make use of the more command if it does the same thing the cat command does? Let’s find out.

2.1) Difference between the cat and more command in Linux

The cat command outputs a file without a pause. So the entire contents of the file are output to the terminal and the terminal scrolls to the end of the file. With the more command, the output stops at the end of the terminal screen allowing you to scroll down using the space key (to scroll a page) or the enter key (scroll line by line) on your keyboard.

As you must have noticed, the –More– (87%) section shows you how much of the file has been scrolled through already. You can press the space key to continue scroll until the file ends and you reach the prompt.

2.2) Options for the more command

Let’s go over some common and useful options that will come handy when scrolling through really large files.

2.2.1) Displaying output from a specific line number

If we want to skip the output to line number 10 in our previous example, we don’t really have to scroll down to it. We can just use the + command.

2.2.2) Search for a string using more command

If you’re looking for a specific piece of information within the file, you can make use of the +/ search operation. This will return the first instance of the searched string on your screen. Especially useful with bigger files, let’s see an example with our 16 line file here.

2.2.3) Using more for multiple files

Similar to the cat command, we can scroll through multiple files in the more command. The benefit here is that the more command distinguishes the content of both the files using the below indication.

You can skip to the next file by typing :n or go back to the previous file by typing :p. You won’t see the keys you type but you should see this on your screen.

3. Conclusion

Well, that’s it for now. You should have a very good understanding of the cat and the more commands by now and will be able to use this in your day-to-day Linux usage. Now coming to self-learning, try and get used to the –help option that comes along with all the commands. This will allow you to see all the available options with the command.

And for an in-depth understanding of the command, the developers have man pages created for your use whenever you need them. Learn how to use man pages and you should become very efficient with Linux in no time.

Learn when to use the tac command instead of cat, and why you might want to.

How to use the linux cat and tac commands

Image credits: Jeff Macharyas, CC BY-SA 4.0. Donald, the cat.

Subscribe now

Get the highlights in your inbox every week.

The tac command is essentially the cat command, but its purpose is to concatenate files in reverse. Like cat, it has a convenient fallback mode to print to standard output (STDOUT) if no output file is provided, making it one of those commands that are more often used as a lazy pager—like less and more—than the function it is named for.

The cat command is often overused and abused, and tac is often taken as a joke command like ddate or cowsay. It often gets paraded out in April Fool’s day articles detailing stupid terminal tricks. So, it may come as a surprise that tac actually has a legitimate reason to exist.

It’s actually a useful command.

What is the purpose of tac?

The tac man page does a rather poor job of describing its own function:

Taking that statement as it’s written, tac should print the last line of a file, then print the file starting back at line one:

That’s not what it does, though. Its info page is much clearer:

Ignoring the fact that tac gives you everything in reverse, it has a few surprisingly useful and unique options.

Tac and separators

As the info page indicates, the file doesn’t have to be delimited by line, meaning that tac is equally as effective with, for example, a CSV file. You define a file’s separator character with the –separator or -s option, along with the delimiter used in the file.

For a CSV file, the character is probably a comma (,), but you can define any character. If a file doesn’t terminate with the separator character, though, then you get an unexpected result:

There is no separator character between the first two items. The file’s final record (the string following the final separator, in this case, a comma) is not itself followed by a comma, so it’s treated as a non-record by tac. To account for this issue, use the –before or -b option, which places the separator character before each record:

The separator character doesn’t have to be a single character. It can also be a regular expression (regex).

Tac and regular expressions

A full explanation of regex is out of scope for this article, but it’s worth mentioning that extended POSIX is supported by means of an environment variable . Extended regex greatly enhances the readability of a regular expression, and for the sake of simplicity, that’s what this example uses. Assume you have a file containing strings all separated by integers:

  • Linux commands cheat sheet
  • Advanced Linux commands cheat sheet
  • Free online course: RHEL Technical Overview
  • Linux networking cheat sheet
  • SELinux cheat sheet
  • Linux common commands cheat sheet
  • What are Linux containers?
  • Our latest Linux articles

To use regex in your tac command, use the –regex or -r option before your –separator definition. Also, unless it’s already set in your environment, you must activate the REG_EXTENDED environment variable. You can set this variable to anything but zero to activate it, and you can do that in all the usual ways:

  • Export the variable for the shell session you’re using.
  • Set the environment variable in your shell configuration file (such as

/.bashrc).

  • Prepend the environment variable to the tac command (in Bash, Zsh, and similar), as shown in the example below:
  • The regex option doesn’t handle non-terminated records well, though, even using the –before option. You may have to adjust your source file if that feature is important to you.

    When to use tac

    These simple yet useful parsing options make tac worth using as an uncomplicated, minimalist parsing command. For those simple jobs that aren’t quite worth writing an AWK or Perl expression for, tac just might be a sensible solution.

    The tac command is limited, obviously, because it doesn’t manipulate records in any way aside from reversing them. But sometimes that’s the only list manipulation you need.

    For instance, if you’re packaging software for distribution, it’s not unusual to have a list of dependencies that are required for installation. Depending on how you gathered this list, you may have it in the order you established the dependencies were required instead of the order in which they must be installed.

    This practice is relatively common because compiler errors hit the high-level dependencies first. That is, if your system is missing libavcodec then GCC stops and alerts you; but since GCC hasn’t gotten a chance to probe your system for libvorbis and libvpx, for example, it can’t tell you that those dependencies are also missing (and, often, required to exist on your system before compiling libavcodec).

    So, your list of dependency grows in top-down form as you discover what libraries your system needs to build the libraries that the libraries need (and so on). At the end of such a process, tac is the quick and easy way to reverse that list.

    Another common annoyance is log files. Entries are generally appended to a log file, so admins use tail to see the latest errors. That works well, but there are times you want to see a “chunk” of entries without knowing how far back you need to go. The tac command piped to less or more puts the latest entries at the top of your screen.

    Finally, many configuration files have no clear termination marker for a given section. You can look up awk and sed commands to devise a way to determine when a block in a config file ends, or you can use tac to reverse the order such that once your parser has found the first relevant entry in that block, it also knows when to stop reading, because what used to be the header is now a footer.

    Tac on

    There are plenty of other great uses for tac, and probably a bunch of reasons that tac is too rudimentary to be a solution. Your system likely has it installed, however, so remember this command the next time you find that edge case in your workflow that really really needs to be attacked in reverse.

    There are several commands and programs provided by Linux for viewing the contents of file. Working with files is one of the daunting task, most of the computer users be it newbie, regular user, advanced user, developer, admin, etc performs. Working with files effectively and efficiently is an art.

    Manage Files in Linux

    Today, in this article we will be discussing the most popular commands called head, tail and cat, most of us already aware of such commands, but very few of us implement it when needed.

    1. head Command

    The head command reads the first ten lines of a any given file name. The basic syntax of head command is:

    For example, the following command will display the first ten lines of the file named ‘/etc/passwd‘.

    If more than one file is given, head will show the first ten lines of each file separately. For example, the following command will show ten lines of each file.

    If it is desired to retrieve more number of lines than the default ten, then ‘-n‘ option is used along with an integer telling the number of lines to be retrieved. For example, the following command will display first 5 lines from the file ‘/var/log/yum.log‘ file.

    In fact, there is no need to use ‘-n‘ option. Just the hyphen and specify the integer without spaces to get the same result as the above command.

    The head command can also display any desired number of bytes using ‘-c‘ option followed by the number of bytes to be displayed. For example, the following command will display the first 45 bytes of given file.

    2. tail Command

    The tail command allows you to display last ten lines of any text file. Similar to the head command above, tail command also support options ‘n‘ number of lines and ‘n‘ number of characters.

    The basic syntax of tail command is:

    For example, the following command will print the last ten lines of a file called ‘access.log‘.

    If more than one file is provided, tail will print the last ten lines of each file as shown below.

    Similarly, you can also print the last few lines using the ‘-n‘ option as shown below.

    You can also print the number of characters using ‘-c’ argument as shown below.

    3. cat Command

    The ‘cat‘ command is most widely used, universal tool. It copies standard input to standard output. The command supports scrolling, if text file doesn’t fit the current screen.

    The basic syntax of cat command is:

    The most frequent use of cat is to read the contents of files. All that is required to open a file for reading is to type cat followed by a space and the file name.

    The cat command also used to concatenate number of files together.

    It can be also used to create files as well. It is achieved by executing cat followed by the output redirection operator and the file name to be created.

    We can have custom end maker for ‘cat’ command. Here it is implemented.

    Never underestimate the power of ‘cat’ command and can be useful for copying files.

    Now what’s the opposite of cat? Yeah it’s ‘tac‘. ‘tac‘ is a command under Linux. It is better to show an example of ‘tac’ than to talk anything about it.

    Create a text file with the names of all the month, such that one word appears on a line.

    For more examples of cat command usage, refer to the 13 cat Command Usage

    That’s all for now. I’ll be here again with another Interesting Article, worth Knowing. Till then stay tuned and connected to Tecmint. Don’t forget to provide us with your valuable feedback in our comment section.

    If You Appreciate What We Do Here On TecMint, You Should Consider:

    TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

    If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

    We are thankful for your never ending support.

    By How to use the linux cat and tac commandsPriya Pedamkar

    How to use the linux cat and tac commands

    Introduction to Linux tac

    In the Linux operating system, the tac command is used to concatenate and print the files in reverse format. When the input or specified file is not provided to the command then the tac command will read the standard input. The tac command is exactly opposite to the “cat” command. The tac command was written by Jay Lepreau and David MacKenzie.

    Syntax:

    Web development, programming languages, Software testing & others

    tac [ OPTION ] . [ FILE ] .

    • tac: Using the tac keyword in the command | syntax. It will take the two different sets of arguments such as an option and file name then revert the output result.
    • OPTION: We can provide the different flags as options that are compatible with tac command.
    • FILE: we need to provide the input file to tac command.

    How does Linux Tac Command Work?

    In the Linux operating system, there are different ways to read files like cat, tail, head, more, less, etc. The cat command is useful to read the file as it i.e. the command is reading the file from start to end. But tac command is inversely proportional or opposite to the cat command. It will print the output in reverse format.

    The tac command will take the two different sets of arguments as an input to the command. As per the give input option, the tac command will revert the input file result and display it. The tac is a complete reverse version of the cat command and also spelled the backward. It will print every line of file from start to end of the file but in reverse format.

    Examples to Implement Linux tac Command

    Following are the examples are given below:

    Example #1 – Tac Command

    In Linux, the tac command will print the input file in reverse format. It is a very basic Linux tac command to get the command output.

    Command:

    Explanation: We are having a sample directory, in the same directory we have “data_1.txt” file. There are few words of data present in it (refer Screenshot 1 (a)). Now we need to use the “tac” command and read the same file “data_1.txt”. While reading the data via “tac” command the compete output will be in the reverse format (refer Screenshot 1 (b)).

    Output:

    Example #2 – Tac Command with “-b” option

    In tac command, we are having the facility to add the separator while reading the multiple files. We need to use the “-b” option in the Tac command.

    Command:

    tac -b data_1.txt data_2.txt

    Explanation: In the sample directory, there are two different files i.e. “data_1.txt” and “data_2.txt”. Both the files are having few data in it (refer Screenshot 2 (a)). Now we need to read both the files at the same time. If we will read the files normal then the files come together and we are not able to find the starting and ending point of the file. To overcome this condition, we are using the “-b” option in tac command. It will separate the files while displaying the multiple files (refer Screenshot 2 (b)).

    Output:

    How to use the linux cat and tac commands

    How to use the linux cat and tac commands

    Example #3 – Tac Command with “-r” option

    In tac command, the command option will interpret the separator as a give regular expression. We need to use the “-r” option in the tac command.

    Command:

    tac -r data_2.txt data_1.txt

    Explanation: In the Linux ecosystem via tac command, we can read the multiple files in the same time and get the output in reverse format. We are having the functionality to get the output as compare to a separator (as a regular expression). To get the same output, we need to use the “-r” option in the command option will interpret the separator as a give regular expression.

    Output:

    Example #4 – Tac Command with “-s” option

    The tac command uses the string to separate out the output. It will not use the new line to separate out the end result. We need to use the “-s” option in the tac command.

    Command:

    tac -s data_1.txt data_2.txt

    Explanation: As per the above tac command, we are using the two files as an input to the command. The command is to use the string as a separator. It will not consider the new line to separate the output result. Hence we are getting the tac command is printing the second file output (data_2.txt) and skip the first line output of the file (data_1.txt). To get a similar tac command output, we need to use the “-s” option in the tac command.

    Output:

    Example #5 – Tac “–help” command

    In tac command, if we will need any help to get the reference in tac command, check the syntax of the command, check the different variable option available in the tac command. We need to use the “–help” option in the tac command.

    Command:

    Explanation: As per the above option, we are getting instant help on the tac command. We are able to get detail information on tac command including the command usage, different options available in the command, etc. To get such information we need to use the “–help” option in the tac command.

    Output:

    How to use the linux cat and tac commands

    Example #6- Tac “–version” command

    We are able to track the tac version of the current operating system. We need to use the “–version” option in the tac command.

    Command:

    Explanation: As per the Linux operating version, the tac version will also vary. As per the current operating system, we are having 8.22 version of the tac command. The version may vary from the different operating systems. The tac command feature will also vary from version to version.

    Output:

    Conclusion

    We have seen the uncut concept of “Linux Tac Command” with the proper example, explanation, and command with different outputs. The tac command is used to get the end result in reverse format. The tac command is mainly useful in data manipulation language.

    Recommended Articles

    This is a guide to Linux tac. Here we also discuss the Introduction and how does linux tac command work? along with different examples and its code implementation. You may also have a look at the following articles to learn more –

    Linux Training Program (16 Courses, 3+ Projects)

    The Linux cat command is used to print the contents of a text file. With the Linux cat command, you can print the contents of your c, java source file, Linux configuration files etc.

    The cat command is available in every Linux distribution out there by default. So, you don’t have to install it separately.

    In this article, I am going to show you how to use the Linux cat command. So, let’s get started.

    Basic Usage of Linux Cat Command:

    The basic and most common use of the Linux cat command is to use it without any command option.

    For example, to view to contents of the /etc/hosts directory, run the cat command as follows:

    As you can see, the contents of the /etc/hosts configuration file are printed on the screen.

    How to use the linux cat and tac commands

    Printing Line Numbers:

    Let’s say, you want to print the contents of a Java source file on the terminal. You can use the cat command of course. But the cat command does not show line numbers by default. For a source file or a program, it is essential. Luckily, the cat command has -n option which you can use to display line numbers.

    To display the contents along with the line number of the Java source file Welcome.java, run the Linux cat command as follows:

    As you can see, the line numbers are displayed.

    How to use the linux cat and tac commands

    Numbering Only Non Blank Lines:

    If you want to show line numbers for the lines that are not blank only, you can use the -b option of the Linux cat command.

    In the previous Java source file Welcome.java, I’ve added some blank lines just to demonstrate how the -b option works.

    As you can see, with the -n option, all the lines (including blank lines) are numbered.

    How to use the linux cat and tac commands

    With the -b option, only the lines that are not blank are numbered as you can see in the screenshot below.

    How to use the linux cat and tac commands

    Removing Repeating Empty Lines:

    A file you’re trying to view may have lots of empty lines one after the other. This will make the output of cat command very long and annoying.

    You can use the -s option of the Linux cat command to remove repeated empty lines as follows:

    Printing Tab Characters:

    In a source code file of a program, you may have used many tab characters. Fortunately, they are invisible by default. But, if you really need to see all the tab characters you have on your file, then you can use the -T option of the Linux cat command.

    Where you might need this feature is when you want to replace all the tab characters with white spaces and you want to make sure that there are not any tab characters left.

    To display all the tab characters in our Welcome.java source file, the Linux cat command can be used as follows:

    As you can see, the tab characters are displayed as ^I.

    How to use the linux cat and tac commands

    Printing End of Line Characters:

    If you want to print the EOL (End of Line) character which is represented by $, you can use the -E option of the Linux cat command.

    For example, to print the EOL characters of Welcome.java, run the Linux cat command as follows:

    As you can see, the EOL characters are printed.

    How to use the linux cat and tac commands

    Printing Non-Printing, Tabs, and EOL Characters:

    Earlier, you had to use the -v option to print the non-printable characters, use the -T option to print the tab characters, and use the -E option to print the EOL characters. What if you need to print all of these? Well, you can combine all of these options together as follows:

    How to use the linux cat and tac commands

    But there is a better solution. The Linux cat command has a -A option that does just the same thing with less typing.

    As you can see, the outputs are the same.

    How to use the linux cat and tac commands

    So, that’s basically how you use Linux cat command to display text files on Linux. Thanks for reading this article.

    The cat command in Linux is one of the most useful commands you can learn. It derives its name from the word concatenate and let you create, merge or print files in the standard output screen or to another file and much more.

    It does not require you to install anything since it comes pre-installed with the coreutils package in any Debian or Red Hat based system.

    In this tutorial, we will cover the common usages of the Linux cat command explaining its features.

    Cat Command Syntax

    Before we start exploring the article’s subject, we should log into the VPS using SSH, and quickly check the basic syntax. The command takes a filename as an argument along with options to specify particular operations.

    To find all the available options, just type cat –help from the terminal.

    Creating a File with the Cat Command

    Using the cat command you can quickly create a file and put text into it. To do that, use the > redirect operator to redirect the text in the file.

    The file is created, and you can begin populating it with text. To add multiple lines of text just press Enter at the end of each line. Once you’re done, hit CTRL+D to exit the file.

    To verify that the file is indeed created by the command used above, just use the following ls command in the terminal:

    Viewing the Content of a File with the Cat Command

    This is one of the most basic usages of the cat command. Without any options, the command will read the contents of a file and display them in the console.

    To prevent scrolling large files, you might want to add the option | more to output through the less or more display:

    You can also display the content of more than one file. For example, to display content of all text files, use the following command in the terminal:

    Redirecting Content Using the Cat Command

    Rather than displaying the contents of a file in the console you can redirect the output to another file using the option >. The command line would look like this:

    If the destination file does not exist then the command will create it, or overwrite an existing one by the same name.

    To append the contents of the destination file, use the >> option along with the cat command:

    Concatenating Files with the Cat Command

    This command also lets you concatenate multiple files into a single one. Basically it functions exactly like the redirection feature above, but with multiple source files.

    Like earlier, the above command will create the destination file if it does not exist, or overwrite an existing one with the same name.

    Highlighting Line Ends with the Cat Command

    The cat command can also mark line ends by displaying the $ character at the end of each line. To use this feature, use the -E option along with cat command:

    Display Line Numbers with the Cat Command

    With the cat command you can also display the contents of a file along with line numbers at the beginning of each one. To use this feature, use the -n option with cat command:

    Displaying Non-Printable Characters with the Cat Command

    To display all non-printable characters use the -v option along with cat command like in the following example:

    To display tab characters only, use -T:

    The tab characters will be shown as ^I

    Suppressing Empty Lines with the Cat Command

    To suppress repeated empty lines, and safe space on your display you can use the -s option. Keep in mind that this option will keep one blank line by removing the repeated empty lines only. The command would look like this:

    Numbering Non-Empty Lines with the Cat Command

    To display non-empty lines with line numbers printed before them use the -b option. Remember the -b option will override the -n option:

    Displaying a File in Reverse Order With the Cat Command

    To view the contents of a file in reverse order, starting with the last line and ending with the first, just use the tac command, which is just cat in reverse:

    Conclusion

    That’s it. You now know all the basic features and functions of the cat command. You will now have the basic understanding to put it to good use. For more information on the cat command, you can always invoke manual page of cat with the command man cat !.

    We hope this article helped you better your Linux Terminal skills. See you in the next one!

    Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward’s goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

    How to use the linux cat and tac commands

    The cat (concatenate) command in Linux/Bash is most commonly used to read the contents of a file. It outputs the contents of a given file. Here’s how to use it.

    cat concatenates files to standard output – by default, this is to the console for viewing on your computer screen. This makes it useful for quickly viewing the contents of files.

    It also has other uses, but first, the syntax:

    cat Syntax

    • If FILE is not specified, will read from standard input (stdin)
    • Multiple FILEs can be specified, separated by spaces
    • OPTIONS should be a list of options from the below table
    • The command will output data via standard output (stdout)

    Options

    Here are the commonly used options for cat, straight from the user manual:

    -A, –show-all Equivalent to -vET
    -b, –number-nonblank Number nonempty output lines, overrides -n
    -e Equivalent to -vE
    -E, –show-ends Display $ at the end of each line
    -n, –number Number all output lines
    -s, –squeeze-blank Suppress repeated empty output lines
    -t Equivalent to -vT
    -T, –show-tabs Display TAB characters as ^I
    -v, –show-nonprinting Use ^ and M- notation, except for LFD and TAB

    The full user manual can always be viewed by running:

    What is stdin and stdout?

    Read a File to the (Bash) Console (stdout)

    It’s that easy – cat will read the file and output the contents to the console for you to view.

    The contents of the file have been output via stdout, which by default sends the data to the console, but it can also be redirected into another program.

    Read File Contents into a Program (via stdin)

    As the standard inputs article above outlines, the output from cat can be redirected to the input of other commands.

    The command above pipes the contents of text.txt into the less command.

    Merging Files

    Given the commands namesake, I’d be remiss if I didn’t show you how to use it to merge (concatenate) files:

    cat can read from multiple files, so redirecting the output to a single file will result in a file with the contents of all of the read files joined sequentially.

    Brad Morton

    I’m Brad, and I’m nearing 20 years of experience with Linux. I’ve worked in just about every IT role there is before taking the leap into software development. Currently, I’m building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I’m up to.