Really simple to explain :
How to rename
/folder1/something.test.html
/folder1/somethingElse.test.html
/folder2/againsomething.test.html
/canBeHereAl
For simple string suffix and prefix manipulation, I suggest you familiarize yourself with the shell's standard parameter expansion features ${VAR%suffix_to_remove}
and ${VAR#prefix_to_remove}
. These will work on any standard sh
, not just bash
:
test -> test2
for NAME in */*.test.html; do # NAME is, e.g., "dir/foo.test.html"
BASENAME=${NAME%.test.html} # BASENAME is "dir/foo"
mv "$NAME" "$BASENAME.test2.html";
done
test2 -> test is similar:
...
BASENAME=${NAME%.test2.html} # strip off .test2.html
...
You could also use the standard shell utilities basename and dirname to achieve something similar.
You may use rename
command.
rename 's/(.*)\./$12./' *.html
or
rename 's/.*\K\./2./' *.html
You can do this:
name1="/folder1/something.test.html"
name2="${name1/%.html/2.html}"
echo "$name2"
#mv "$name1" "$name2"
Output:
/folder1/something.test2.html
.html
is being replaced with 2.html
at the end of the string.
To go back i.e test2 -> test
:
name2="/folder1/something.test2.html"
name1="${name2/%2.html/.html}"
See the Bash Manual for more info on Parameter Expansion.