How does `+` differ from `;`?

The + and ; symbols in the -exec option of the find command serve different purposes regarding how commands are executed on the found files.

Using ;

  • When you use ;, the command specified with -exec is executed once for each file found.
  • This means that if find locates multiple files, it will run the command separately for each file.

Example:

find . -type f -name '*.txt' -exec cat {} \;

In this case, if there are 10 .txt files, the cat command will be executed 10 times, once for each file.

Using +

  • When you use +, the command is executed with all the found files passed as arguments in a single execution.
  • This is generally more efficient because it reduces the number of times the command is invoked.

Example:

find . -type f -name '*.txt' -exec cat {} +

In this case, if there are 10 .txt files, the cat command will be executed only once, with all 10 files passed as arguments.

Summary

  • ;: Executes the command once per file found (less efficient for many files).
  • +: Executes the command once with all found files as arguments (more efficient for many files).

Using + is often preferred when dealing with a large number of files to minimize the overhead of multiple command executions.

0 Comments

no data
Be the first to share your comment!