• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 775
  • Last Modified:

Wildcard file rename using MV command

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
shaf81
Asked:
shaf81
  • 2
  • 2
2 Solutions
 
woolmilkporcCommented:
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
 
shaf81Author Commented:
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
 
woolmilkporcCommented:
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
 
shaf81Author Commented:
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
 
ThomasMcA2Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now