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
Comment Utility

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

Comment Utility

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
Comment Utility

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

Comment Utility
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

Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now