30

I am trying to automate an svnadmin dump command for a backup script, and I want to do something like this:

find /var/svn/* \( ! -name dir -prune \) -type d -exec svnadmin dump {} > {}.svn \;

This seems to work, in that it looks through each svn repository in /var/svn, and runs svnadmin dump on it.

However, the second {} in the exec command doesn't get substituted for the name of the directory being processed. It basically just results a single file named {}.svn.

I suspect that this is because the shell interprets > to end the find command, and it tries redirecting stdout from that command to the file named {}.svn.

Any ideas?

squircle
  • 6,699
  • 5
  • 37
  • 68
pkaeding
  • 1,470
  • 3
  • 18
  • 27

2 Answers2

40

You can do the redirection like this:

find /var/svn/* \( ! -name dir -prune \) -type d -exec sh -c 'svnadmin dump {} > {}.svn' \;

and the correct substitution will be done.

Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
  • This is flawed and bad practice. Compare [this answer of mine](https://superuser.com/a/1327980/432690). The right way is to pass `{}` as an argument to `sh` and then (inside `sh`) refer to it as `"$0"` or `"$1"` or so. – Kamil Maciorowski Nov 24 '18 at 17:49
5

No, however you can write a simple bash script to do that then call it from find.
Example (/tmp/dump.sh):

#!/bin/sh
svn admin dump "$1" > "$1".svn

then:

find /var/svn/* \( ! -name dir -prune \) -type d -exec sh /tmp/dump.sh '{}' \;
OneOfOne
  • 947
  • 6
  • 13