Rename files in bash keeping the originals unchanged
I have a folder with a lot of images for a mult开发者_开发技巧ilingual site, the images are stored in the next format filename.lang_code.jpg, on the deploy script i want to pick the correct one for the site I'm deploying and copy it to filename.jpg so I can share the same css between the sites.
So, what I need is something like the command rename, but performing a copy, not a move, because I need to keep all files.
I'm doing it with the next code, but I find it overcomplicated and awful.
find -name "*.es.*" -print0 | xargs --null -I {} sh -c "echo {} | sed 's/.es//' | xargs -I }{ sh -c 'cp {} }{'"
This kind of copying/renaming is usually easier to perform in zsh, where you don't need to write a for
loop or find
command.
zmv -Ls '(**)/(*).es.(*)' '$1/$2.$3'
The -Ls
is to create symbolic links; replace by -C
for a copy, or nothing for a move. Other useful options are -i
(ask for confirmation for each copy/move) and -n
(just show what would happen but don't actually perform the copies/moves).
The $1
, $2
, $3
in the replacement text refer to the first, second and third parenthesized group in the pattern. In the pattern, **/
means any chain of directories.
You may need to first load the zmv
command with autoload zmv
. This can usefully go into your .zshrc
, as well as alias zcp='zmv -L'
and alias zln='zmv -L'
.
find -name '*.es.*' -exec bash -c 'cp "$1" "${1/.es./.}"' modlang {} \;
bash 4
shopt -s globstar
for file in **/*.es*
do
newfilename=${file/.es/}
cp "$file" ""$newfilename"
done
of using find
find . -type f -iname "*.es.*" -print | sed 's/\(.*\)\.es\.\(.*\)/mv \1.es.\2 \1\2/' |bash
How about a for loop?
for x in *.es.* ; do
xx=$(echo ${x} | sed 's/\.es//')
cp ${x} ${xx}
done
Hmm.. you could just copy the whole lot of jpg's you want, put them in the directory you need them and just rename them from there?
Something like:
cp *.jpg /src /dst
cd /dst && rename 's/\.es//' *.jpg
The rename command is a wrapper for a perl script.
精彩评论