Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I type find in unix a shell, is a new process created (to handle the find operation) or not? And why?

share|improve this question

migrated from Dec 26 '12 at 9:34

This question came from our site for professional and enthusiast programmers.

yes, generally speaking find is not a built-in command so the shell will spawn a new process, by calling fork() which will exec() the command.

share|improve this answer

Possibly (probably), but not necessarily. If you have previously typed:

$ find() { :; }

then no new process is created when you type find. Instead, the shell will invoke the function, which is a no-op. However, if you have not defined such a function, have no alias set for find, and an executable named find exists in your PATH, then the shell will (almost certainly) fork a new process. ("Almost certainly" because it is quite possible that you are using a shell which implements find as a builtin, although I am not aware of any shells which do.)

share|improve this answer
+1 for the only answer so far that does not make the mistake of ignoring aliases or functions. – Johannes Schaub - litb Dec 25 '12 at 14:34

all external commands create process(i.e uses fork()).Ex.find,grep,cp,mv etc. only internal commands did not use fork()(ex. cd,pwd etc).Even in some famous books internal commands are defined as the command which does not generate any process.

share|improve this answer

You also asked why a new process is created.

Running a program on Unix involves replacing the memory image of the current process with the image of the program; this is done by the exec() family of system calls. When the program finishes, the process terminates. If we hadn't forked a new process, there would no longer be a shell process running to display a new prompt and let you run more commands.

So the shell forks a child process, and this child execs the program. Meanwhile, the original shell process waits for this process to terminate. Or, if you end the command with &, the shell doesn't wait -- this is how we run programs in the background (I'm simplifying greatly, but this is the important gist).

share|improve this answer

You must log in to answer this question.