Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

this is not an Ubuntu specific quesion, it applies to all unix/linux.

how can I run a command like this:

 find .  -maxdepth 1 -type d  -print -exec svn info "{}"   | grep URL  \; 

the command above doesn't do what I want, I can't seem to pipe the output of the svn info to grep.

This works, but the output contains much more than I need:

 find .  -maxdepth 1 -type d  -print -exec svn info "{}"  \; 

Any ideas?

share|improve this question
1  
Could you please describe what you really want ? Also please put the real question (related to your objective) in the title :) –  João Pinto Feb 23 '11 at 17:08

4 Answers 4

Generally, I find xargs much more sane to use than find's -exec option. This is especially true for speeding things up since a separate "svn" is not started for every line of find's output:

find . -maxdepth 1 -type d | xargs svn info | grep URL

If you're dealing with files with strange names (spaces, newlines, etc), just add -print0 to find and -0 to xargs:

find . -maxdepth 1 -type d -print0 | xargs -0 svn info | grep URL

If you're dealing with a program that can only take 1 argument, you can use -n1 with xargs:

find . -maxdepth 1 -type d -print0 | xargs -0 -n1 svn info | grep URL
share|improve this answer
    
If you use -exec ... + instead of -exec ... ";" you gain the parallel invocation like with xargs, without -print0 gymnastics. You don't need to mask {} in find either, not for a current shell. –  user unknown Mar 20 '11 at 0:18

Actually I figured out that the below does what I want in this instance:

  find .  -maxdepth 1 -type d  -print -exec svn info "{}"   \;  | grep URL

but I still would like to know how can I pipe output inside the exec of find

share|improve this answer
1  
The following should do it: -exec sh -c "ls {} | something" \; –  João Pinto Feb 23 '11 at 17:10
    
@João Pinto: No, you shouldn't embed {} into the script like that. Pass it as an argument instead. -exec sh -c 'ls "$@" | something' _ {} \; –  geirha Feb 24 '11 at 13:14
    
You shouldn't invoke the shell at all. Charbels solution is so much better - lean and clean. What do you like to reach with your shell? Charbel just can drop the useless maskerading of {}. –  user unknown Mar 20 '11 at 0:21

You need a shell to interpret the pipe.

find . -maxdepth 1 -type d -print -exec sh -c 'LANG=C svn info "$1" | grep URL' _ {} \;

See http://mywiki.wooledge.org/UsingFind for more examples.

Though. I'd just use a for-loop for that instead:

for svndir in ./*/.svn/; do
    dir=${svndir%/.svn/}
    echo "$dir"
    LANG=C svn info "$dir" | grep URL
    # or maybe just: sed '5!d;q' "$svndir/entries"
done
share|improve this answer
    
find . -maxdepth 1 -type d -print -exec sh -c 'LANG=C svn info "$1" | grep URL' gives me the following error: "find: missing arguments to `-exec'" –  Charbel Feb 24 '11 at 11:06
    
@Charbel you're missing the last part, you need to scroll right so you get the _ {} \; part too :) –  geirha Feb 24 '11 at 13:12
find .  -maxdepth 1 -type d  -print -exec svn info "{}" \; | grep URL
share|improve this answer
    
Nearly the same as Charbel. You too may omit the maskerading of {}. –  user unknown Mar 20 '11 at 0:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.