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
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'
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
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
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
If you want to know when a line ends, the cat command can help you with the -E option.
:~$ cat -E file.txt
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
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.
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: