1

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?

1
  • 1
    Could you please describe what you really want ? Also please put the real question (related to your objective) in the title :) Feb 23, 2011 at 17:08

4 Answers 4

5

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
1
  • 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. Mar 20, 2011 at 0:18
1

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

3
  • 1
    The following should do it: -exec sh -c "ls {} | something" \; Feb 23, 2011 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, 2011 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 {}. Mar 20, 2011 at 0:21
1

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
2
  • 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, 2011 at 11:06
  • @Charbel you're missing the last part, you need to scroll right so you get the _ {} \; part too :)
    – geirha
    Feb 24, 2011 at 13:12
0
find .  -maxdepth 1 -type d  -print -exec svn info "{}" \; | grep URL
1
  • Nearly the same as Charbel. You too may omit the maskerading of {}. Mar 20, 2011 at 0:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .