Find with -execdir


© Anthony Lawrence, aplawrence.com

This article discusses the "-execdir" option to "find" and the use of "\;" or "+" to terminate the arguments supplied to "-exec" or "-execdir". Please note that these things are likely to change. When this was written, Mac OSX "find" in Leopard worked differently than "find" in a current Linux. Both may work differently than described if you are reading this at a much later date than it was written or are using a different version of Linux or MacOSX.

With that out of the way, let's look at some newer "find" options that old Unix hands may not have picked up on. The first is "-execdir". This works like "-exec" but executes the requested command after first having changed into the directory that holds the file that is in the argument. So, if you have "find . -exec ls {} \;" and "./subdir/myfile" is one of the files under ".", when "find" reaches that it will call "ls ./subdir/myfile". If you use "find . -execdir ls {} \;" instead, "find" will change into "./subdir" and then execute "ls myfile".

Confused? OK, let's go slow. First, -exec:

The "find" locates the first file you've asked for. It fires up your command and passes the file name as an argument. It does that again for every file it finds. If the file is in a subirectory, the subdirectory path is included in what gets passed to your command.

But with "-execdir":

The "find" will move into any subdirectory it encounters before running your command. Only the base filename will be passed; the subdirectory path is not.

Simple enough so far? Sure, except that you had better not have "./subdir/ls" AND "." earlier in $PATH than the real "ls". That could be very unpleasant, right? Therefore, Linux "find" refuses your request if "." is in your PATH. MacOSX does not.

That's kind of dumb if your command was explicit - "/bin/ls" for example. But "find" with "-execdir" on Linux is going to protect you even when you are perfectly safe - well, safe from executing the wrong command, anyway.

That's not the only difference. Both Linux and MacOSX allow you to terminate your "-exec" or "-execdir" arguments with a "+" rather than the "\;" all old Unix hands will recognize. The "+" makes the whole thing work like "xargs" - rather than calling your command once for each file it finds, the "+" terminator makes "find" build up a list of files to pass to your command. Unfirtunately that lacks all the bells and whistles of "xargs", but it's handy enough nonetheless. Except..

Well, gosh, there are some issues. MacOSX does find with "-exec" and a "+" ("find . -exec ls {} +") but can't use it with "-execdir" if you have any subdirectories because it doesn't do the directory change. We can see this by creating a little "mytest" script containing just this:

cat $*
 

Additionally we'll create "./a" containing identifying text and "./tt/a" also containing recognizable text. Let's do "-exec" first on MacOSX:

$ find . -exec mytest  {} +
cat: .: Is a directory
This is ./a
cat: ./tt: Is a directory
THIS IS ./tt/a
 

OK so far? Let's do it on Ubuntu also:

$ find . -exec mytest  {} +
cat: .: Is a directory
cat: ./tt: Is a directory
THIS IS ./tt/a
This is ./a
 

The ordering is different because of how the search is done by default, but otherwise the output is identical.

Now let's try "-execdir". We'll let Ubuntu go first this time:

$ find . -exec mytest  {} +
cat: .: Is a directory
cat: ./tt: Is a directory
THIS IS ./tt/a
This is ./a
 

Exactly the same but supposedly more efficient because of the "+". We'll see in a moment that Linux cheated, but let's try OS X:

$ find . -execdir mytest  {} +
cat: .: Is a directory
This is ./a
cat: tt: Is a directory
This is ./a
 

Look closely.

Oops. It didn't pick up the "./tt/a". Why? Because the arguments it saw were ". a tt a", not ". a tt tt/a". The "+" only did one invocation of "mytest" and didn't do any directory movement at all. How could it? It would have to break up the invocations into one per subdirectory to do that trick.

So that must be what Linux does, right? Well, no..

At least right now, on the Ubuntu I used, the "find . -execdir mytest {} + " doesn't pay any attention to the "+". Your command gets called once for every file found; in other words, it's no different than "-exec" if "+" is used with "-execdir". You can prove that to yourself by changing "mytest" to "echo $#". If you do that and try the "-execdir" again, you'll see:

(MacOSX)
4
(Linux)
1
1
1
1
 

Proving once again that:

  • You have to read the manual
  • You can't trust the manual
  • Things that look the same aren't always





4 comments




More Articles by

Find me on Google+

Anthony Lawrence







Thu Feb 19 17:19:27 2009: 5467   MikeHostetler

gravatar
using the combo of find|xargs is supposedly faster than find -exec:

(link)



Thu Feb 19 18:04:56 2009: 5468   TonyLawrence

gravatar
Seems odd, doesn't it? You'd think that firing up the extra process of xargs would hurt a little and I would have expected that if "find" was slow, it wouldn't be at packing a command line - it apparently doesn't even care how big that buffer gets, while xargs does.. yet there it is.



Tue Oct 25 13:34:53 2011: 10065   anonymous

gravatar


That email (find | xargs is faster than find -exec) is full of soup.
1) It first says "find -exec grep \;" is slower than "find | xargs grep". Well, of course. What would be interesting is to compare "find -exec grep \+".
2) It shows "find -exec grep \;" takes 10 seconds while "find | xargs grep" takes less than one. But the second run is cached. I get the same results with "+", but watch:

$ time find /usr/include -type f -exec grep NFS_VERSION '{}' '+'
/usr/include/rpcsvc/nfs_prot.h:#define NFS_VERSION ((rpc_uint)2)
/usr/include/rpcsvc/nfs_prot.x: version NFS_VERSION {

real 0m7.167s
user 0m0.085s
sys 0m0.331s
$ time find /usr/include -type f -print0 | xargs -0 grep NFS_VERSION
/usr/include/rpcsvc/nfs_prot.h:#define NFS_VERSION ((rpc_uint)2)
/usr/include/rpcsvc/nfs_prot.x: version NFS_VERSION {

real 0m0.194s
user 0m0.060s
sys 0m0.070s
$ time find /usr/include -type f -exec grep NFS_VERSION '{}' '+'
/usr/include/rpcsvc/nfs_prot.h:#define NFS_VERSION ((rpc_uint)2)
/usr/include/rpcsvc/nfs_prot.x: version NFS_VERSION {

real 0m0.133s
user 0m0.059s
sys 0m0.069s






Tue Oct 25 13:40:55 2011: 10066   TonyLawrence

gravatar


Thanks - though remember that implementations vary and things change - software gets fixed for better or worse and so do the underlying filesystems. Whatever that article said might have been true on their hardware, their OS and their "find" at that moment in time.


------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us