19.6 C
Texas
angeloma
Senior Writer and partner

The Linux cat command

Friends today I am going to teach you about the Linux cat command. This command is quite simple to learn but quite useful in many situations. So, today we will teach it to you along with some examples so that you can better understand this command.

The cat command is part of GNU CoreUtils and is mainly used to display by standard output the entire contents of a text file. This can be quite useful in many advanced tasks with the system, for example, sysadmin uses it quite a bit to view any file from the terminal.

On the other hand, the cat command is also used to create files because it allows you to capture standard input and assign it to an empty file. Of course, this possibility offered by the cat command is rarely used.

So let’s go for it.

The Linux cat command

- Advertisement -

Cat is a fairly simple command to use, it is also possible to use it on UNIX based systems such as FreeBSD. It has a basic syntax of use which is as follows:

cat [OPTION]… [FILE]…

However, it has other options that extend the functionality of the command. If you want to show them, just use the following command:

:~$ cat --help
Usage: cat [OPTION]… [FILE]…
 Concatenate FILE(s) to standard output.
 With no FILE, or when FILE is -, read standard input.
 -A, --show-all           equivalent to -vET
   -b, --number-nonblank    number nonempty output lines, overrides -n
   -e                       equivalent to -vE
   -E, --show-ends          display $ at 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
   -u                       (ignored)
   -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
       --help     display this help and exit
       --version  output version information and exit
 Examples:
   cat f - g  Output f's contents, then standard input, then g's contents.
   cat        Copy standard input to standard output.
 GNU coreutils online help: http://www.gnu.org/software/coreutils/
 Full documentation at: http://www.gnu.org/software/coreutils/cat
 or available locally via: info '(coreutils) cat invocation'
1.- Cat command help
1.- Cat command help

But, let’s see the usefulness of the cat command using several useful examples.

Using the cat command

I will create a sample text file called file.txt and add content.

To display the contents of the file, simply use the following command:

:~$ cat file.txt
2.- Using the cat command
2.- Using the cat command

It is also possible to use an absolute route, for example:

:~$ cat /home/angelo/file.txt

So you can see the contents of a file from another directory.

It is also possible to view the contents of several files, just separate them by a blank space:

:~$ cat file.txt file2.txt

And so on.

It is also possible, to show the content of the file and display the lines it has. To do this, add the -n option.

:~$ cat -n file.txt
3.- Using the -n option
3.- Using the -n option

So we can quickly identify a particular line.

However, many text files have empty lines. This is common. The cat command also allows us to show only those that have content and are not empty. The -b option is the correct one.

:~$ cat -b file.txt
4.- Showing only the relevant lines
4.- Showing only the relevant lines

If you want to know when a line ends, the cat command can help you with the -E option.

:~$ cat -E file.txt
5.- Cat can show the end of the lines
5.- Cat can show the end of the lines

Also, cat can display the non-printable characters with the -v option.

:~$ cat -v file.txt

Or the tabs with the -T option

:~$ cat -T file.txt
6.- Showing the TAB with the cat command
6.- Showing the TAB with the cat command

Note: I have modified the file to include a tab and show you how it looks.

So, that is it. You already know how to use the cat command.

Conclusion

The cat command is quite simple to use but it can get us out of trouble in many cases. Especially useful if you are sysadmin or a student of some computer-related engineering, the post will help you get familiar with the terminal.

Please share this post and join our Telegram channel.

I invite you to read other posts about Linux commands:

cp command
The Tail command
head command
The wget command

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article