Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8488321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:30:00+00:00 2026-06-10T21:30:00+00:00

I have a bunch of files which contain prefix codes in brackets. I’d like

  • 0

I have a bunch of files which contain prefix codes in brackets. I’d like to turn these prefixes into suffixes, like so:

Finance-(4BF)-001.doc   --> Finance-001-4BF.doc
Corporate-(K04)-001.doc --> Corporate-001-K04.doc

I previously wrote a very simple VBScript to do this on a Windows machine but now I need to do this on Linux. After some tedious searching I can’t find a simple and elegant way to apply a regular expression to a filename and rename it using the regex matches.

So far I have this:

#!/bin/bash
for i in *.doc
do
    x=${i//[\(\)]/}
    echo "$i renames to: $x"
done

The output of this is:

Corporate-(K04)-001.doc renames to: Corporate-K04-001.doc
Finance-(4BF)-001.doc renames to: Finance-4BF-001.doc

I know the regular expression above is just stripping the brackets () out of the filename… but surely there must be a way to match their contents (e.g. \((\w)\)) and then use that match in the rename command (e.g. $1)?

Also being a Linux novice I don’t understand what x=${...} is doing, and since I don’t know what it’s called I can’t Google it. I presume it’s applying a regex to the string i but in that case why can’t I extract matches from it like $1 and $2 etc?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:30:02+00:00Added an answer on June 10, 2026 at 9:30 pm

    The construct ${...} is called “parameter expansion” and can be found in the Bash manual.

    The replacement feature is very elementary and does not support backreferences ($1). You can use sed instead:

    x=$(sed -E 's/\(([[:alnum:]]+)\)-([[:alnum:]]+)/\2-\1/' <<< "$i")
    

    Note that [[:alnum:]] is the same as \w in other languages, but POSIX regular expressions don’t have the latter. See man re_format for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of files which contain an ascii header with a time
I have a bunch of files (almost 100) which contain data of the format:
I have a bunch of wav files and corresponding XML files which contain times.
I have a bunch of log files which are pure text. Here is an
I have a bunch of .doc files in a folder which I need to
I have a function where I will compress a bunch of files into a
I have a bunch of scripts which I want to refactor into modules. This
I have a file which has xml like tags and a bunch of invalid
I have a bunch of text files that contain tags referenced by the @
I have a git directory which contains the a whole bunch of files and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.