(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



Find with -execdir


2009/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 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


;


Click here to add your comments





Thu Feb 19 17:19:27 2009: Subject:   MikeHostetler
http://mike.hostetlerhome.com
gravatar
using the combo of find|xargs is supposedly faster than find -exec:

http://www.sunmanagers.org/pipermail/summaries/2005-March/006255.html



Thu Feb 19 18:04:56 2009: Subject:   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.

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



numly esn 99237-090216-909508-69
numly barcode

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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Shell
       - MacOSX
       - Linux


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress