Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Wildcard file rename using MV command

Posted on 2013-11-04
5
Medium Priority
?
711 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 800 total points
ID: 39621402
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
ID: 39621564
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 800 total points
ID: 39621667
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
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!
0
 
LVL 14

Expert Comment

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

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

876 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