Skip to main content

cat: foo: input file is output file

I needed to grow a text file and thus decided to do this:

$ ls -a > foo
$ cat foo >> foo
cat: foo: input file is output file

$ cat --version | head -1
cat (GNU coreutils) 8.13
Hm? Why does cat know that stdout is redirected to itself? And even if it does know, why should it care? On FreeBSD 9.1 (and on MacOS X), stdout is fed ad infintum to itself:
$ ls -a > foo
$ cat foo >> foo
^C
$ ls -lh foo
-rw-------  1 alice  users  -   16M Jun 24 04:22 foo
On Solaris 10, cat behaves similar to GNU/coreutils
$ cat foo >> foo
cat: input/output files 'foo' identical

$ pkginfo -l SUNWcsu | grep VER
   VERSION:  11.10.0,REV=2005.01.21.16.34
The workaround is to use a pipe:
$ ls -lgo foo
-rw-------+  1     100 Jun 24 06:36 foo
$ cat foo | cat >> foo
$ ls -lgo foo
-rw-------+  1     200 Jun 24 06:36 foo