Tags

,

The find command is a very demanding command, and can slow a system way down – and can take a long time as well. There are some ways to avoid these drawbacks.

First, when the urge strikes you to use find / -name "somename" – just don’t do it. This will take a very long time and will notably slow the system down. If you are using the GNU findutils, you may have the program locate on your system, which is much faster. Otherwise, you can use print the output from a find / command (run during off hours) and search the file with grep whenever you have a need to.

Secondly, you may wish to avoid using the -exec parameter. This parameter will run a command on each file that is found. Each time a file is found and printed, the find command loads this specified command, gives it the filename, and runs the command – which is very inefficient. GNU find has the ability to stack filenames together, but that still is not enough.

The most efficient way is to use xargs instead:

find . -mtime -1 | xargs ls -ld

This will combine all of the files together (as much as possible) and will run the binary image of the command as many times as necessary to handle it – without reloading the command at all. Of course, if you have GNU findutils, the builtin -ls option may be even faster!

find . -mtime -1 -ls

You can also manipulate where things go in the command output, or replicate items more than once, and so on. The operation of xargs is simple once you understand it, but the power is tremendous, and it is on all UNIX/Linux platforms. Check out the xargs(1) man page from OpenBSD.

Advertisements