The UNIX find command and efficiency

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.

3 thoughts on “The UNIX find command and efficiency”

  1. I don’t want my confidential file to be displayed when running find to locate files. the file must be isolated from the output.
    how should i do this????

  2. How you hide a file depends on your needs. The general way to do this (in order to keep files from cluttering listings) is to use a dot as the first character in the filename.

    This method, while effective, does not prevent find from finding it (with appropriate options) and does not prevent people from reading it.

    To do that, you might want to consider steganography or an encrypted disk. Steganography hides a data file within portions of an image. An encrypted disk will allow you to place files within the disk that are not readable outside of the disk (without encryption).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: