How to use ZCAT and CAT commands on GNU/LINUX

0
386
a terminal from GNU/LINUX
a terminal from GNU/LINUX

Many people find it difficult to use the terminal of a GNU/LINUX distribution, and this makes it difficult for them to learn how to work with it, which is one of the most powerful tools in the Unix environment.

There are very simple commands to use and others not so simple, for now we will focus on two very simple but useful commands when performing tasks in the terminal, especially if you are a system administrator, the ZCAT and CAT commands.

First we go with zcat!!

Actually both commands are simple and similar to use, first we will deal with zcat.

zcat is a command for unix systems such as GNU/LINUX that allows us to view the contents of compressed files from the command line.

Its use from the terminal is simple and uses this syntax:

zcat [OPTION]… [FILE]…

Using the help of zcat in the terminal we visualize the available options:

ZCAT  options
Option Use
-f –force force
-l, –list list compressed file contens
-q, –quiet Suppress all warnings
-r, –recursive recursive mode
-S, –suffix=SUF use suffix SUF on compressed file (replace SUF)
-t, –test test compressed file integrity
-v, –verbose verbose mode

 

Its use is simple. First we will create 4.txt files and then we will compress them for our example.

:~$ mkdir zcat_command
:~$ cd zcat_command
:~$ touch r{1..3}.txt
:~$ nano r1.txt // we add some text
:~$ gzip r1.txt r2.txt r3.txt
1.- Creating files
1.- Creating files

the result of this is the creation of 3 compressed text files with extension.txt.gz

2.- showing created files
2.- showing created files

Now we’re ready to go.

  • Display the contents of a compressed text file with gz

:~$ zcat r1.txt.gz
3.- showing the content of a compressed tex file
3.- showing the content of a compressed tex file
  • Displaying the contents of multiple files at the same time

We can display the contents of several files at once:

:~$ zcat file1 file2
:~$ zcat r1.txt.gz r2.txt.gz

We will get the contents of each file successively:

4.- Showing multiple contents files
4.- Showing multiple contents files
  • Enabling pagination

To enable pagination we can do it with less or more

:~$ zcat file | more
:~$ zcat file | less
:~$ zcat r1.txt.gz | more
:~$ zcat r1.txt.gz | less
5.- enabling pagination
5.- enabling pagination
  • Force Mode

By default zcat only works with.gz compressed files but it can be used with uncompressed files by forcing them to run.

:~$ zcat -f file
:~$ nano t1.txt // add hello
:~$ zcat -f t1.txt
6.- Force mode of zcat
6.- Force mode of zcat
  • Showing compressed file properties

It is also possible to show the properties of a file by using the option -l

:~$ zcat -l file.gz
:~$ zcat -l r1.txt.gz
7.- Using -l option
7.- Using -l option

Now it’s cat’s turn

The cat command is very similar in its use and functionality to zcat, but this time we will work with uncompressed text files. But it also serves to concatenate the output of several files by the standard output of the terminal. We will also have a new feature, which is that cat allows us to concatenate text files to be displayed in a single output by the terminal.

First we’ll examine the help the command gives us from your terminal:

CAT options
Option Use
-A, –show-all Show all contens
-b, –number-nonblank number nonempty output lines
-E, –show-ends display $ at end of each line
-n, –number number all output lines
-T, –show-tabs display TAB characters as ^I
-t, –test test compressed file integrity
–version show cat’s version

 

We will examine several examples to demonstrate how to use this command:

  • Display the contents of a text file

This is the most basic way to use the cat command:

:~$ cat path_of_the_file
:~$ cat file1
8.- Using cat command
8.- Using cat command
  • Display the contents of multiple files

It is also possible to display several files at once, for this purpose:

:~$ cat path_of_the_file1 path_of_the_file2 ...
:~$ cat file1.txt file2.txt
9.- using cat command
9.- using cat command
  • Identify Specific File Content

If, as in the previous example, we need to show the contents of several files, it is very useful to identify when one line is finished and the other starts. Using the -e option we can do that, because at the end of the line’s content display it places the $ sign

:~$ cat -e file1.txt file2.txt
10.- using -e option
10.- using -e option
  • Add Number of Lines in Output

However, showing the lines that a file has is very easy and its use is highly recommended to better examine the file.

:~$ cat -n file1.txt
11.- Adding the -n option
11.- Adding the -n option
  • Copy Contents of One file to Another

cat comes from “concatenate” so we can pass the content from one file to another.

:~$ cat file1.txt > file2.txt
:~$ cat file2.txt
12.- using cat command for copy contents
12.- using cat command for copy contents

With this > cat operator, it reads the contents of the first file and overwrites the second file.

  • Combine or Append the Content of One File to Another

Now if we want to “combine” the contents of one file with another we must use the >> operator.

:~$ cat file1.txt >> file2.txt
:~$ cat file2.txt
13.- using >> operator
13.- using >> operator

As you can see cat reads the content of the first file and combines it with the second one.

As a result, we can say that these very similar commands in their use are of great help to get a taste for the terminal of any Unix system, or in this case, of GNU/LINUX. Both are simple to use, and their usefulness is very diverse according to what we need.

Try using these and other commands, please share this article through your social networks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here