[mdlug] UNIX tips: Learn 10 good UNIX usage habits
Robert Citek
robert.citek at gmail.com
Mon Mar 10 01:22:45 EDT 2008
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
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
Regards,
- Robert
More information about the mdlug
mailing list