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.
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 <file name>. For example, I have a config file named file1 in my directory. Let’s list out the contents of the file.
root@HowLinux:~# cat file1 Hi this is content from file 1
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.
root@HowLinux:~# cat file1 file2 Hi this is content from file 1 Hello! This is content from file 2
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
root@HowLinux:~# cat > NewFile
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.
root@HowLinux:~# cat >> SampleFile
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.
root@HowLinux:~# cat numbers > numbers-copy
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.
root@HowLinux:~# tac numbers 5 4 3 2 1
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:
root@HowLinux:~# cat -n file1 file2 1 Hi this is content from file 1 2 Hello! This is content from file 2
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.
root@HowLinux:~# cat -E numbers 1$ 2$ 3$ 4$ 5$ $
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.
root@HowLinux:~# cat -vET numbers
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.
root@HowLinux:~# cat -a numbers
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.
root@HowLinux:~# more file1
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 +<line number> command.
root@HowLinux:~# more +10 file1
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 string/regex> 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.
root@HowLinux:~# more +/15 file1
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.
root@HowLinux:~# more file1 file2 :::::::::::::: file1 :::::::::::::: Hi this is an example file 1
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.
...Skipping ...Skipping to file file2 OR ...Skipping ...Skipping back to file file1
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.