2

I thought the following two commands would output the same thing, but they don't:

Command 1:

find . -name filename -exec print '{}' \;

Command 2:

find . -name filename -exec echo '{}' \;

In fact, Command 2 works as I expected it to work: it prints the relative path to all instances of filename under .

However, for Command 1 I get as many lines in the output as I get for Command 2, but instead of giving me the path, each line says:

find: print: No such file or directory

I have also tried removing the quotes fron {}.

Why doesn't the second command work as I expected it to work? And why does it fail to print the path to each instance of filename?

1
  • 1
    print isn't actually a standard command anywhere, and if it does exist it most likely is not a synonym for echo. From the error message you get, your system doesn't seem to have print in the path at all. find can -print file names for you without needing an external command anyways. Both commands you've listed are poor usage and could be replaced with a simple find -name filename since -print is the default action for find.
    – jw013
    Commented Oct 6, 2011 at 21:13

2 Answers 2

6

Contrast:

% whence -a print
print
% whence -a echo
echo
/bin/echo

This shows that print only exists as a built-in, whereas echo exists both as a built-in and an executable. (Actually, going by what's shown above, print and echo could be aliases, functions or reserved words; use whence -w to see precisely what type of internal object a name represents.)

find is an external command, so it can't invoke any shell built-in. If you really need to invoke your shell's built-ins, invoke a shell explictly:

find . -name filename -exec zsh -c 'print "$0"' '{}' \;

In this case, I don't think you want zsh's print built-in: find's -print primary will do the job, only properly (the zsh print built-in expands backslashes).

find . -name filename -print

(You can even leave out the -print here, because -print is the default action for find when no action is specified.)

The reason why there is an echo external executable but none called print is that echo is a standard command, so it's given maximum availability even to programs that want to invoke it without going via a shell. In contrast, print is specific to zsh, so only zsh scripts use it in the first place.

(Oh, and on my system, there is a print command, which sends files to the printer.)

2

find searches the PATH for a command which matches whatever you specify with '-exec'. echo is a actually a binary on your system (for me it's /bin/each).

print, by contrast is not a binary located on your system, but rather a shell-builtin (look for it here) which find can't exec for you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .