Wildcard file rename using MV command

Posted on 2013-11-04
Last Modified: 2013-11-04

I have a *nix system (x-wrt infact) and need to do some wilcard rename of files.

The files are in a folder, along with many other files. The files I'm interested, share the following name format:

and I need to rename all these files into:

If it was MS-DOS, then I would use something like:

ren before-*.sh after-*.sh

But in *nix, this won't work.

mv before-*.sh after-*.sh

I need to know how to get this done on *nix. Specially, the commands should run on the x-wrt system. I cannot install anything extra.

Question by:shaf81
  • 2
  • 2
LVL 68

Accepted Solution

woolmilkporc earned 200 total points
ID: 39621402

as far as I know WRT uses an Almquist shell derivate, so this should work:

for f in $(ls before-*.sh) ; do
  mv $f "after-"${f#before-}

Attention: Existing files "after-........" will be silently overwritten! To avoid this (by getting prompted before overwriting) use "mv -i ... ... ... "

EDIT: In case there are no files matching the pattern "before-*.sh" you'll get an error message saying something like "File not found".
If you don't want to see this message (discard it) use:

for f in $(ls before-*.sh 2>/dev/null) ; do
  mv $f "after-"${f#before-}

Author Comment

ID: 39621564

Thank you for the help,

but when I run the script the result is, ->

Secondly, I understand this script has to be executed in the same path where the files are.
Would it be possible to have the files in /var/myfiles but have the script in /var/myscripts and then launch if from the /var/myscripts directory?

In that case, I would be doing something like...

for f in $(ls /var/myfiles/before-*.sh 2>/dev/null) ; do.....
LVL 68

Assisted Solution

woolmilkporc earned 200 total points
ID: 39621667

The given syntax should work in ash, as far as I understood the docs (I don't have such a shell at hand to test). It does work in ksh and bash, that's confirmed.

On the command line, try

echo ${A#before-}

What do you see? I assume that those "before-" and "after-" strings are just examples, so you should replace them with samples of your actual values. It might be that the strings you're using would contain some special characters disturbing the variable editing mechanism.

If we can't succeed in using variable editing, how about "sed"? Is this tool available? What output do you get when just issuing "sed"?

2) We'll need a bit more coding  to make the second requirement work:

for file in $(ls /var/myfiles/before-*.sh 2>/dev/null) ; do
  mv $file $T

The above assumes that we can make variable editing work.

Alternatively, you can always add "cd /var/myfiles" just above the line starting with "for" in the original version, or "cd" to "/var/myfiles" on the command line and run "/var/myscripts/".

Author Closing Comment

ID: 39621747
Thank you for your replies.

Actually your initial solution worked fine. It was my mistake that I had overlooked something and made a mess out of it.

Things do work like a charm. Thank you very much!
LVL 14

Expert Comment

ID: 39621855
Here's a simpler solution that works in some shells:
rename before after *.sh

Open in new window

And this version works in other shells:
rename "s/before/after" *.sh

Open in new window


Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question