#4: Cut your use of cat

Posted by | Comments (0) | Trackbacks (4)

All Linux administrators know this tool and some make use of it very often: cat. Also Linux beginners get to know this quickly and use it a lot. Most people love it so much that they utilize it in every situation whether it's needed or absolute nonsense. Not very seldom this results in strange constructs like:

cat file | grep string

or my favorite one to copy a text file

cat file1 > file2

All these operations cost you a new process whereas you could just do the same much cheaper with redirection operators (and the second one of course with cp). As a rule of thumb one can say that cat in front of a pipe is useless. Our first example could also be done with

<file grep string
grep string < file

or in this special case even better with

grep string file

because grep is also built to work with files, not only with STDIN.

cat stands for (con)catenate and therefore it's been invented to concatenate two or more files to one single file. E.g.

cat file1 file2 > catfile

In nearly all cases where you use cat on just one file you could do it a better way with redirection operators. One of the few exceptions of this rule is a construct like this:

(command1 ; cat file ; command2) > outputfile

where you redirect the collected output of a subshell to a file (or pipe it to another command). And of course another one is the case of printing file contents to the screen but wouldn't you use more, less or lv for that? Especially on long files cat does a pretty bad job here.

So keep this in mind and try to lower your use of cat if you are one of those using it all the time. Otherwise one day you could find yourself nominated for the Useless Use of Cat Award (UUOC). ;-)

Read more about cat:

Trackbacks

robo47 sent a Trackback on : (permalink)

RT @reflinux: #Advent series “24 Short #Linux #Hints”, day 4: Cut your use of #cat http://bit.ly/eUN3Rb

Manko10 sent a Trackback on : (permalink)

RT @reflinux: #Advent series “24 Short #Linux #Hints”, day 4: Cut your use of #cat http://bit.ly/eUN3Rb

Refining Linux sent a Trackback on : (permalink)

This article is part of the 2010 Advent calendar series “24 Short Linux Hints”. This series focuses on little (and sometimes longer) tricks, tips and hints to solve common problems and really improve your workflow. grep is probably one of the best known,

Refining Linux sent a Trackback on : (permalink)

This article is part of the 2010 Advent calendar series “24 Short Linux Hints”. This series focuses on little (and sometimes longer) tricks, tips and hints to solve common problems and really improve your workflow. Pipes are used very often on UNIX syste

Comments

No comments have been submitted yet. Be the first!

Write a comment:

HTML-Tags will be converted to Entities.
Textile-formatting allowed
Standard emoticons like :-) and ;-) are converted to images.
Design and Code Copyright © 2010-2017 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms. tweetbackcheck