Solved

Wildcard file rename using MV command

Posted on 2013-11-04
5
643 Views
Last Modified: 2013-11-04
Hello,

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:

before-apple.sh
before-orange.sh
before-peach.sh
before-melon.sh
before-papaya.sh
before-xxx.sh

and I need to rename all these files into:

after-apple.sh
after-orange.sh
after-peach.sh
after-melon.sh
after-papaya.sh
after-xxx.sh

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.

Ideas?
0
Comment
Question by:shaf81
  • 2
  • 2
5 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 200 total points
Comment Utility
Hi,

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-}
done

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-}
done
0
 
LVL 3

Author Comment

by:shaf81
Comment Utility
Hi,

Thank you for the help,

but when I run the script the result is,

before-apple.sh -> after-before-apple.sh


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.....
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 200 total points
Comment Utility
1)

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

A="before-aaa.sh"
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
  P=${file%/*}
  F=${file##*/}
  T=${P}"/after-"${F#before-}
  mv $file $T
done

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/scriptname.sh".
0
 
LVL 3

Author Closing Comment

by:shaf81
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!
0
 
LVL 14

Expert Comment

by:ThomasMcA2
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

0

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