2

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 !

4 Answers 4

4

You can do:

for file in *release.apk; do
  mv "$file" "${file/-*-/-}"
done
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

0

You can use a regex with the rename command:

rename 's/-*-/-/g' *release.apk

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.