2 min read

Linux find: which one is faster?

harry@Aither ~/Downloads> time find . -type f -exec ls -lah {} + > /dev/null

________________________________________________________
Executed in   67.66 millis    fish           external
   usr time   28.07 millis  161.00 micros   27.90 millis
   sys time   37.10 millis  930.00 micros   36.17 millis

harry@Aither ~/Downloads> time find . -type f -exec ls -lah {} \; > /dev/null

________________________________________________________
Executed in    2.12 secs    fish           external
   usr time    0.31 secs  171.00 micros    0.31 secs
   sys time    1.02 secs  651.00 micros    1.02 secs

harry@Aither ~/Downloads> time find . -type f -exec ls -lah {} + > /dev/null

________________________________________________________
Executed in   64.73 millis    fish           external
   usr time   28.02 millis  160.00 micros   27.86 millis
   sys time   33.65 millis  919.00 micros   32.73 millis

harry@Aither ~/Downloads> time find . -type f -exec ls -lah {} \; > /dev/null

________________________________________________________
Executed in    2.17 secs    fish           external
   usr time    0.32 secs  128.00 micros    0.32 secs
   sys time    1.03 secs  680.00 micros    1.03 secs

In Unix-like systems, the find command is used to search for files and directories in a directory hierarchy based on various criteria. The -exec flag is used to execute a command on the found files. The two different ways I using -exec involve different ways of passing the found files to the command.

  1. Using {} +:
    • When you use {} +, it tells find to collect the found files and pass them as arguments to the command in batches, as opposed to invoking the command once for each file.
    • This is more efficient because it reduces the number of times the command is executed, and the command receives multiple file paths at once, making more effective use of system resources.
    • It's similar to the way the xargs command works, grouping the arguments and passing them to the command.
  2. Using {} \;:
    • When you use {} \;, it tells find to execute the specified command once for each found file individually.
    • This can be less efficient because it incurs the overhead of starting a new process for each file, which may lead to increased system call overhead.

In my above examples, the timings indicate that using {} + is faster than using {} \;. The time difference is due to the reduced overhead of starting and managing fewer processes when using {} +. The command with {} + is able to process multiple files in a single invocation, leading to better performance.

In summary, using {} + with the -exec flag allows find to optimize the execution by passing multiple file paths to the command in batches, resulting in improved efficiency and faster overall execution.