Select Page

Reading Files in Linux – cat and tac commands

The cat command stands for concatenation. This a very common and useful command in Unix like OS e.g. Linux, Mac OS X. Instead of displaying the contents of a file in a text editor, you can use the cat command to quickly display it in standard output, i.e. in the bash terminal. Thus the cat command can increase your productivity. Although largely used in displaying the contents of a file, you can use the cat command to create, display, concatenate, modify a file, or files in Bash. On the other hand, Linux tac command is the opposite of cat command. Let’s see them in action.

Before you start

If any of the concepts looks unfamiliar to you, then check out the conclusion for links to other posts that explain all the necessary topics. To follow up this blog, create some text files either manually or using touch and nano commands as shown in earlier blogs. Insert some arbitrary contents so that you can manipulate them below.

Linux cat command syntax

The basic syntax is as follows:

$ cat [option] [path/to/file_name]

The square bracket represents that these arguments are optional. You can either use any of them or none of them, depending on your need. To check all the available options in Linux cat command, try one of these commands:

$ cat --help
$ man cat

The former one outputs everything in the terminal, and the later one displays everything in a temporary screen.

all the available options in linux cat command. Displayed using man and help commands.
All the available options in Linux cat command

Both column 1 and 2 shows all the available options.

They are just alternative to each other. Use any one of them

Reading files with Linux cat command

The most basic and common use of Linux cat command is to display the contents of file or files. This is a big section!

Reading a single file

To read the contents of a file named script.txt in MLwiki folder:

$ cat MLwiki/script.txt
reading a file with cat command
Reading a file with cat command

Reading multiple files at once

Add all the file names one by one, and they will be displayed at once. Try this:

~$ cd MLwiki
~/MLwiki$ cat script.txt file2.txt file3.doc

Or if you followed previous blogs, try this:

~$ cat MLwiki/{script.txt,file2.txt,file3.doc}
reading multiple files at once using the cat command
Reading multiple files at once

Notice that all the files are displayed following the order of file names. And there’s no blank line in between the file contents. So be careful.

To view all the files in a directory, you can use wildcard (*). This is a regular expression or regex.

$ cat MLwiki/*
reading all files at once inside the given directory using wildcard regex
Wildcard to represent ‘all’

You can use other regexes to choose what files to display by Linux nano command. For example, say you want to read all the text files. In MLwiki directory, there are 3 text files, file1.txt, file2.txt, script.txt.

$ cat MLwiki/*.txt
reading all text files only from the given directory
Reading all text files only

Print with line numbers

In case you want to display the contents of a file along with the line number, use the -n option. -n stands for ‘numbering all output lines’ according to the Linux cat command manual.

$ cat -n MLwiki/{file1.txt,file2.txt}
reading files in linux cat command with line number using -n option
Print with line numbers

Squeeze Blank Lines

Suppress repeated empty output lines. Notice how it works:

$ cat -s MLwiki/file4.txt
up7

In the first output, you can see that the actual file contains an increasing number of spaces after each line. But when you squeeze the output, there’s only one space after each line. So the -s option in Linux cat command helps you to squeeze unnecessary blank lines.

Numbering non-empty lines

When using the -n command for numbering lines, did you notice that all the empty lines are also getting numbered? If you want to skip numbering the empty lines, use -b command. It will overwrite the -n command. Here’s how:

$ cat -b file4.txt
numbering only the non empty lines using the linux cat command with -b option. This option overwrites the -n option
Numbering only non empty lines

Show end of each line

You’re debugging a script. One extra space at the end might raise an error in your language. So to be sure where each line ends, what about printing an extra dollar sign ($) at the end of each line without affecting the actual file? You can do this using the -E option along with the Linux cat command.

$ cat -E script.txt
showing end of each line with the dollar sign.
Showing end of each line with $

Show TABs

If your file contains any Tab, then using this option will output ^I instead of Tab. You may use this to check whether you used tabs or 4 spaces in your Python code. You may know that when writing Python script, if you use Tabs and Spaces interchangeably for indentation, then you might get IndentationError depending on the text editor you’re using. So it’s a good way to check!

$ cat -E code.py
using the -T linux cat command to check whether a python script used spaces or tabs
use of -T cat command

Creating files with Linux cat command

Creating files with the cat command can be very useful and easy. There’s bunch of ways for this. Let’s explore one by one.

Using redirection operator (>)

Use this syntax:

$ cat > file_name

After running this cat command, your cursor will be taken to the next line. But without prompting anything, it will wait for your input. Here you can write whatever you want to save inside that file_name file. Remember, if the file doesn’t exist, it will be created. But if it exists, then it will be overwritten.

creating and writing to a file with cat command and redirection operator
writing to file with cat and > commands

Can you tell why the terminal didn’t prompt at a new line? Because, when you run a command, your terminal will prompt just at the end of the output. As I terminate the input prompt using Ctrl+D just at the end of the last line, the terminal prompt started there.

Using (>>) operator

This is same as before. But instead of overwriting a file, if existed, this appends the text at the end of that file.

$ cat >> file2.txt
Using the ">>" operator, you can append your desired text at the end of an existing file
Appending text with Linux cat command

Concatenating and copying to a file

Using the same (>, >>) operators and the same procedure as stated above, you can concatenate a bunch of files or copy them to another file. The basic syntax is:

$ cat [files to copy from] operator [file to copy to]

Say I want to concatenate all the text files and copy them to a new file.

Concatenating and copying a bunch of file contents into a new file using the Linux cat command
Copying contents to a new file

You can also use the options available for cat command in this section

Try them out.

tac command in Linux

As the name goes, the Linux tac command works in the opposite way of the cat command. The tac command displays the content of a file in the last-line-first order. Let’s see this in action:

$ tac path/to/file
The linux tac command working in the opposite of the cat command
Linux tac command in action

The tac command for Linux is not used that much in practical. But if you still want to explore, try the manual of it:

$ man tac
$ tac --help

Conclusion

The cat command will expand to the next 2 blogs for higher level use.

If you’re not following the entire Bash series, rather reading this by Google search, and face any problem with the commands stated above, try reading these recommended blogs.

Mind Sharing It?
Join Facebook Group

Public Group

A group optimized for learning purposes. Track your progress by joining a unit

Follow on Quora

Public Space

Best place to ask questions! Join the built-in community to get help from others