The rename
command you need is:
If you're using rename
from util-linux
:
rename . _1. *.orig && rename .orig '' *.orig
The first will replace the first occurence of .
with _1.
for all files ending in .orig
. The second will remove the .orig
from the file names.
If you're using perl-rename (default on Debian-based systems), things are simpler since it uses Perl Compatible Regular Expressions so can do everything in one step:
rename 's/(\..*)\.orig/_1$1/' *orig
Since this version uses The regex will match the first .
(\.
, the .
needs to be escaped since ti means "any character" whan not escaped), then everything (.*
) until .orig
. Because the first pattern is inside parentheses, it is "captured" and can later be referred to as $1
. Therefore, the replacement will replace anything matched previously with a _1
and the captured pattern (the first extension) and it will simultaneously remove the .orig
.
You can combine the commands with find
as follows:
find /example -name '*.orig' -exec rename . _1. {} +
find /example -name '*.orig' -exec rename .orig '' {} +
Or:
find /example -name '*.orig' -exec rename 's/(\..*)\.orig/_1$1/'
You don't even need find
for this. If all your files are in the same directory, you can use the commands above directly, or, to make it recursive (assuming you are using bash):
shopt -s globstar ## make ** recurse into subdirectories
rename.ul . _1. **.orig && rename.ul .orig '' **.orig ## util-linux
rename 's/(\..*)\.orig/_1$1/' **orig ## perl rename
In fact, strictly speaking, you don't even need rename
. You can do everything in the shell (see here for an explanation of the shell's string manipulation abilities):
for file in **/*.orig; do
newname="${file%%.*}_1.${file#*.}"
mv "$file" "${newname/.orig/}"
done
Finally, a note on globs (-name
uses a glob, not a regular expression). The *.*.orig
is Windows glob syntax for "anything ending in .orig
". The equivalent and what you should use with your find
is *.orig
. The *
alone matches anything, using *.*.orig
will only match file names with two extensions, the last of which is .orig
.
rename
implementations out there and they have very different format. We need to know which one you have to answer correctly. – terdon Oct 20 '15 at 11:52rename.ul --version
. That will either priutn a usage message (which includes the information we need) or version information. Either way, please add the output to your question. – terdon Oct 20 '15 at 12:05rename --version
.rename.ul
is always going to be the util-linux rename, andprename
is always going to be the perl rename.rename
could be either. on a debian-based system, it's controlled by the alternatives system -update-alternatives --display rename
– cas Oct 20 '15 at 12:13prename
, yes, but as I recall, the RedHat world defaults torename.ul
. I don't know if all distros will have both installed by default. – terdon Oct 20 '15 at 12:56