How To Speed Up Find With -exec Many Times

We all use find to locate stuff on the command line. The -exec option allows us to execute a command on the located objects without having to pipe the output anywhere.

This is convenient but slow.

Find executes the command for every matched file. For example, the following command runs ls -la individually for every file in the /home/user/Documents/ directory:

find /home/user/Documents/ -type f -exec ls -ls {} \;

This command takes 9.5s on the my Documents directory. That’s pretty slow for the 3763 files that are in my Documents directory.

We can do better!

The solution here is to substitute \; with +. This small change makes find append the files to the exec’ed command before executing it. This significantly reduces the number of times that find invokes the command.

Re-writing the find command above with a + gives us:

find /home/user/Documents/ -type f -exec ls -ls {} +

So how much faster is it?

146x faster!

The new find command completes in 0.065 seconds. Your mileage will vary depending on the command you are executing and the number of files you are working with but it’s still an awesome tweak to a tool we all thought we knew.