Etc : Bash - cat

cat

cat is very hand command line tool for reading short text files. Try it out by going to the etc folder and use it to take a peek at some of the files there.

$ cd /etc/
$ cat debian_version 
11.4
$ cat hosts
127.0.0.1	localhost
127.0.1.1	debian
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

$ cat timezone
Europe/Dublin
$ cat motd
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

cat's man page is brief, when using it to read files you might find these options useful..

-n, --number 
			number all output lines

-s, --squeeze-blank
			suppress repeated empty output lines

cat's real reason for being is to join concatenate files so lets try that out!!. Go to your sandbox directory and use the echo command.

$ cd sandbox/
(or $ cd ~/sandbox/ )
$ echo part one
(prints out part one to the screen)
part one
$ echo part one > 1.txt
(This time divert the output to the file 1.txt)
$ cat 1.txt
part one
(Yes success, now create 2.txt and 3.txt)
$ echo part two > 2.txt
$ echo part three > 3.txt
$ cat 2.txt
part two
$ cat 3.txt
part three
(now to join the three files together.)
$ cat 1.txt 2.txt 3.txt > all.txt
(and to check the result)
$ cat all.txt
part one
part two
part three

cat can also create text files ..

$ cat > roses.txt
(Just hit Enter and start typing)
Roses are red
(Hit Enter each time to start a new line)
Violets are blue
Sugar is sweet
And so are you 

To finish the file hit Enter and then Ctrl+C which stops cat. Use cat this time with the filename to see the results.

$ cat roses.txt 
Roses are red
Violets are blue
Sugar is sweet
And so are you

Maybe a bit slushy, there is an alternate ending to that poem, so to use cat to add that to roses.txt do the following...

$ cat >> roses.txt
or should I say
WHAT HAPPENED TO YOU
(then Enter and Ctrl+D)
(and to see the results)
$cat roses.txt
Roses are red
Violets are blue
Sugar is sweet
And so are you
or should I say
WHAT HAPPENED TO YOU

Yes cat is very handy for small text files and for good measure there is tac which concatenates and prints files in reverse order.

$ tac roses.txt
WHAT HAPPENED TO YOU
or should I say
So are you
Sugar is sweet
Violets are blue
Roses are red