cat and tac

Both concatenate files and print their contents but tac does it in reverse.

So if you need to reverse file contents, print out last line of the file first, use tac:

$ cat file
1
2
3
$ tac file
3
2
1

cat has few more useful features in addition to concatenating files

You can use it to remove multiple empty lines (replace any number of empty lines with just one empty line) or “squeeze blanks” as the man page says. Useful to get rid of useless empty lines in the input.

# cat file
1

2




3
$ cat -s file
1

2

3

You can use cat to make disk images and/or write images to the disk. You really don’t need dd utility or some other imaging software to write or take images. At least not small ones like some Linux distro images you can download from the net. To write images to disk, just cat image (or zcat if its zipped/gzipped) and redirect output to your hard disk, usb drive or memory card.

Writing images to disk
# cat any_disk.iso > /dev/sdx
# cat os_image.img > /dev/sdx
# zcat os_image.imng.gz > /dev/sdx

Creating images
# cat /dev/sdx > mydisk.img
# cat /dev/sdx | gzip > mydisk.img.gz

There are many more cats:

  • zcat – cat gzipped files directly, without using some other utility for extraction.
  • lzcat, xzcat – cat .xz and .lzma files directly
  • ncat – netcat, you can use it to connect to somewhere or listen.
  • socat – socket cat, you can use it to connect two data streams together – files, file descriptors, pipes, sockets etc. to to different set of files, file descriptors, pipes, sockets. Really neat tool.

If you found this useful, say thanks, click on some banners or donate, I can always use some beer money.