Sample files:
./data/foo/file1.xml
./data/foo/bar/file2.xml
./data/baz/file3.xml
Corresponding output:
./out/foo/file1.xml.xml2
./out/foo/bar/file2.xml.xml2
./out/baz/file3.xml.xml2
The subproblems are:
- Find all
xml
files indata/
- Construct the output path: replace data/ with out/ and add an additional extension
- Create the output path if it doesn't exist
- Execute a script, redirect to output file in new path
I've been experimenting with find
and I have come up with a solution that works on my test files:
find data/ -name "*.xml" -exec bash -c \
'out=${1/data/out}.xml2; mkdir -p $(dirname "$out"); ./testscript.sh > $out' - {} \;
(testscript.sh
contains echo "Hello World"
.)
But I'm not that good with bash, and this seems overly verbose, and might contain hidden errors.
Things I've thought about:
mkdir
silently fails if the dir exists, so it'll work for multiple files in the same dir, but maybe this entire script should be done in a loop in order to only call mkdir once per new dir.${1/data/out}
only replaces the first occurence ofdata
, which should always exist and be the outermost directory in the variable (since relative paths are used). I'd prefer it if this was done in a more automatic and safe way though, if possible.
Side-note: I have full permissions in the current dir and all subdirs, but not on the entire machine.