问题
I have files named:
test-12.5.0_567-release.apk
I want them to look like:
test-release.apk
I realized I can do it with bash:
for file in *release.apk; do
mv "$file" "`basename $file SOMETHING`NEW_FILE_NAME"; done
It needs some regex I guess ? How would it look like ?
Thanks !
回答1:
You can do:
for file in *release.apk; do
mv "$file" "${file/-*-/-}"
done
回答2:
Alternatively you can use this:
for file in *release.apk; do
mv "$file" "${file%-*-*}-release.apk"
I'm removing -12.5.0_567-release.apk and then add -release.apk.
However, IMHO anubhava's solution looks better since it is more precisely doing what you want.
回答3:
Another alternative, with an actual regex and capturing groups. It uses the =~ operator and the BASH_REMATCH array (containing the captured sub groups)
for file in *; do
# check if the filename matches the regex and extract 2 groups
if [[ $file =~ ([^-]*-).*-([^-]*) ]]; then
# use the captured groups
mv "$file" "${BASH_REMATCH[1]}${BASH_REMATCH[2]}";
fi
done
回答4:
You can use a regex with the rename command:
rename 's/-*-/-/g' *release.apk
来源:https://stackoverflow.com/questions/35625458/rename-files-using-bash-regex