First of all, you can safely ignore that error. The command will run successfully and it will, correctly, ignore all.java
itself. It is simply letting you know that it did so.
Anyway, to avoid the error you could use tee
and find's
exec option:
$ find . -name '*.java' -exec cat {} + | tee all.java
From man find
:
-exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command until
an argument consisting of `;' is encountered. The string `{}'
is replaced by the current file name being processed everywhere
it occurs in the arguments to the command, not just in arguments
where it is alone, as in some versions of find.
-exec command {} +
This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca‐
tions of the command will be much less than the number of
matched files.
So, you can use -exec
to tell find
to run a command on each of its results. The {}
is replaced with the actual file/directory name found. The +
is just the marker that tells find
that the command ends here. I use it instead of \;
because it will run fewer commands since it will attempt to combine them into as few runs as possible.
Or use find's !
to exclude all.java
:
$ find . -name '*.java' ! -name all.java -exec cat {} + >> all.java
Or globbing:
$ shopt -s globstar
$ cat **/*.java > all.java