问题
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.
回答1:
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' _ {} +
来源:https://stackoverflow.com/questions/22623921/git-recursive-mv-files