1

I'm trying to add version numbers to all javascript files in a directory so that changes to the files won't be cached by our users.

How can I move all Javascript files that are in several directories within one directory?

With the example file structure:

js/
--home/
----main.js
--apps/
----main.js
--handlers.js
--ajax.js

I would like to do something like git mv -r js/*.js js/*.1.js to append a .1 to the filenames. Obviously there isn't a flag for recursion for git mv, so I'm wondering what my options are to do something similar.

bozdoz
  • 12,550
  • 7
  • 67
  • 96
  • You don't need a recursive flag to `git mv`. You don't need `git mv` at all. http://stackoverflow.com/questions/1094269/whats-the-purpose-of-git-mv – Edward Thomson Mar 25 '14 at 04:50

1 Answers1

5

Globstar (bash >=4)

shopt -s globstar # Set the SHell OPTion globstar
for f in **/*.js; do
  git mv "$f" "${f%.js}.$version.js"
done

To move everything to a single directory:

for source in **/*.js; do
  dest="${source%.js}.$version.js"
  dest="$destination/${dest##*/}" # Strip off leading directories and prepend actual destination
  git mv "$source" "$dest"
done

Find

You can use find, but it's almost the same, so save this for where you need Bash 3 or POSIX sh portability (OS X, for example).

find . -iname '*.js' -exec sh -c 'for f; do
  git mv "$f" "${f%.js}.$version.js"
done' _ {} +
kojiro
  • 74,557
  • 19
  • 143
  • 201
  • That `**.*.js` makes it go into a single directory? Also, what's the `shopt` and `globstar`? – bozdoz Mar 25 '14 at 01:44
  • @bozdoz `globstar` is just the recursive version of a regular `*` and is written `**`. `shopt -s` sets the option to true (false by default in [tag:bash]), allowing it to be used. – Reinstate Monica Please Mar 25 '14 at 01:48
  • @bozdoz updated with a way to move everything to a single destination – I wasn't sure that's what you wanted at first. – kojiro Mar 25 '14 at 01:49
  • I didn't need `shopt` or `globstar`. Also I added it to a makefile like so: `for f in js/**/*.js; do git mv "$$f" "$${f%.js}.$(v).js"; done` – bozdoz Mar 25 '14 at 03:39