![]() ESN 99237-090216-909508-69 |
|
Document Name: Find with -execdir Document Description: Find with -execdir2009/02/16 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 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 "-exdecdir": 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 youyr 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:
Author: Anthony Lawrence - Contact Author Publisher: Anthony Lawrence Licensee Name: Anthony Lawrence Reference URL: http://aplawrence.com/Unixart/find_execdir.html Copyright: All Rights Reserved Registration Date: 2/16/2009 7:47:51 PM UTC Views: 1409 |
