BASH: Make the same files tree with other extension

… using ‘find’. The issue is making same tree files in other directory with other extension,
in my case is making js-behaviors tree for templates.
Also have a two directories assets/app/dialogs (current directory) and templates/dialogs.
Prepare current directory. Next shell make the same directory tree:
$ find ../../../templates/dialogs/ -mindepth 1 -type d \ -exec sh -c "mkdir -p \${0:27}" {} \;

And let’s look to the files that will bee created:
$ find ../../../templates/dialogs/ -iname "*.tpl.html" -type f \ -exec sh -c "echo \${0:27:\${#0}-35}js" {} \;
A litle explanation of numbers in right part of the command:
    27 – a length of “../../../templates/dialogs/”;
    35 – is a summ of lengths of previous string and “tpl.html”

Ok. Make them:
$ find ../../../templates/dialogs/ -iname "*.tpl.html" -type f \ -exec sh -c "touch \${0:27:\${#0}-35}js" {} \;

Hmm… Oh, I forgot! What’s about making clone of template? Fill by prototype code into the index.js and :
$ find ../../../templates/dialogs/ -iname "*.tpl.html" -type f \ -exec sh -c 'AB=$(du -sh ${0:27:${#0}-35}js | awk '\''{print $1}'\''); [[ "x${AB}x" == "x0x" ]] && echo cp index.js "${0:27:${#0}-35}js"' {} \; or more readable variant:
$ find . -iname "*.js" -type f \ -exec sh -c 'AB=$(du -sh ${0} | awk '\''{print $1}'\''); [[ "x${AB}x" == "x0x" ]] && cp index.js "${0}"' {} \;

And to continue and show a more usefull example look at this:
$ find . -iname "*.js" -type f \ -exec sh -c 'A=${0#*/}; A=${A/\//\\\/}; sed -i -e "s/_id: .*$/_id: '\''${A%.js*}'\','/" ${0}' {} \;
This command will replace a _id with similar js-file name. Note the replacement /, in my case left part of / mean a category or group.

Here I tried to show you how to use a “find” like a swiss army knife.

Leave a Reply

Your email address will not be published. Required fields are marked *