Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Jul 95 11:22:42 WET
From:      Peter May <peter@osix.oz.au@osix.osix.oz.au>
To:        mbarkah@hemi.com (Ade Barkah)
Cc:        hackers@freebsd.org
Subject:   Re: Being curious with `cat * > file'
Message-ID:  <199507200122.LAA07988@blain.osix.oz.au>
In-Reply-To: <199507192022.OAA10037@hemi.com>; from "Ade Barkah" at Jul 19, 95 02:22:50 pm

next in thread | previous in thread | raw e-mail | index | archive | help
Ade Barkah spoke thus:

> Hello,

> What should `cat * > output` do ? Should it gracefully concatenate
> all the files together into a file called `output', or will it
> attempt to cat the output into itself so many times until the
> file system is filled ?

Sort of. It will expand the '*' in alphabetical order, which is why
you are getting this below.

> Here's a sample session:

> barkah: {1} mkdir test1
> barkah: {2} cd test1
> barkah: {3} cat >> testfile <<!
> test file
> barkah: {4} cat * > output
> barkah: {5} ls -l
> total 4
> -rw-rw-r--  1 mbarkah  userm  10 Jul 19 13:50 output
> -rw-rw-r--  1 mbarkah  userm  10 Jul 19 13:50 testfile

The shell created the file output with length 0 before it exec'd the
cat command. It then would have run the command:

cat output testfile 

with output redirected to output.

Since output was a zero length file at this point, it "copied" zero bytes
from output to output. Then it copied the contents of testfile to output
and you ended up with a 10 byte file "output"

> [Looks great up to this point, works as expected]

> barkah: {6} cat * > output2

> [Job killed with ctrl-c at this point]

> barkah: {7} ls -l
> total 916
> -rw-rw-r--  1 mbarkah  userm      10 Jul 19 13:50 output
> -rw-rw-r--  1 mbarkah  userm  454360 Jul 19 13:50 output2
>                               ^^^^^^
> -rw-rw-r--  1 mbarkah  userm      10 Jul 19 13:50 testfile

This is also expected, consider how the shell would have handled the
filename expansion:

(Creates empty file output2)

cat output output2 testfile 

With standard output redirected to output2.

First, it cat's the contents of output (which is 10 bytes) into output2,
then it copies the contents of output2 to output2, which is at first 10
bytes, but keeps growing add infinitum. It never gets to testfile.

If you want to prove it (it's based on alphabetic order) try:

(with just output and testfile in the directory)

cat '*' > aoutput

As aoutput is the first file processed, it would work as expected.

The issue is when the shell does filename expansion. In some shells,
the expansion is done before redirection is processed. This does not
appear to be the case in the shell you are using.

> -Ade Barkah
> --------------------------------------------------------------------
> Inet: mbarkah@hemi.com - HEMISPHERE ONLINE - www: <http://hemi.com/>;
> --------------------------------------------------------------------

Regards,

---------------------------------------------------------------->>>>>

Peter May                           OSIX Pty Ltd
Director                            Level 1, 261-263 Pacific Highway
Technical Services                  North Sydney. NSW. Australia. 2060.

Home: +61-2-418-7656                Internet: peter@osix.oz.au
Work: +61-2-922-3999                Fax:  +61-2-922-3314

          >>>> PGP Public key available upon request <<<<

---------------------------------------------------------------->>>>>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199507200122.LAA07988>