[mdlug] UNIX tips: Learn 10 good UNIX usage habits

Aaron Kulkis akulkis3 at hotpop.com
Mon Mar 10 03:01:01 EDT 2008


Robert Citek wrote:
> On Sun, Mar 9, 2008 at 11:41 PM, Robert Meier <eaglecoach at wwnet.com> wrote:
>>  I posted some suggestions to IBM.
>>  I included also the conflict between xargs (hint 8)
>>  which splits filenames with spaces and quoting (hint 4)
>>  which maintains filenames.
>>  I suggested using find ... | while read f; do ... "$f" ...; done
>>  as more portable, flexible, and less error prone.
> 
> Alternatively, you can use the -print0 option to find and the -0
> option to xargs.  Perhaps not as portable, but nevertheless useful.
> 
> As for the piping cat, I take that rule with a grain of salt.  For
> instance, when I'm building up a series of command, I'll often start
> with head or grep or similar to pare down date in a largefile.  Then
> once I'm confident the pipeline will work, I'll substitute cat.  To
> borrow the example on the page, this command:
> 
> $ head tmp/a/longfile.txt | grep and
> 
> may become this:
> 
> $ grep foo tmp/a/longfile.txt | grep and
> 
> before it becomes this:
> 
> $ cat tmp/a/longfile.txt | grep and

Thats what the file redirect operator is for.

catting a single file, without flags, into a pipe
is nothing more than a waste of CPU cycles.


> 
> Another reason is that some commands generate different output when
> piped vs. when used as arguments.  For example, contrast the output of
> these commands:
> 
> $ cat /etc/passwd /etc/group | wc -l
> $ wc -l /etc/passwd /etc/group
> 
> and these:
> 
> $ cat /etc/passwd /etc/group | grep root
> $ grep root /etc/passwd /etc/group

cat (short for conCATenate files) is legitimately used
when the contents of 2 or more files need to be directed
to a pipe.

But for a single file...it's poor programming.







More information about the mdlug mailing list