Operating System - Linux
1753677 Members
5469 Online
108799 Solutions
New Discussion юеВ

mv on multiple file with joker

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

mv on multiple file with joker

Hi Guys

Here 400 files like this:

ls *precl*
aa.precl.test
aaprecl.start
precl.bb
rrpreclyy.start
....


I need to rename all this file as this:
aa.prect.test
aaprect.start
prect.bb
rrprectyy.start
...

What is the best way ? Of course the naive command below
mv *prect* *prect*
doesn't work naturally.


Best regards
Den
11 REPLIES 11
Ivan Ferreira
Honored Contributor
Solution

Re: mv on multiple file with joker

You can use:

for FILE in $(ls); do NEWFILE=$(echo $FILE| sed 's/precl/prect/g'); mv $FILE $NEWFILE ; done
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Leo The Cat
Regular Advisor

Re: mv on multiple file with joker

Thank you very much Ivan.
I Don't close this thread immediately in case of some other callback !

Bests Regards
Den
Leo The Cat
Regular Advisor

Re: mv on multiple file with joker

Ho to use environment variable with sed like this

MYSOURCE=prect
MYDEST=precl
for FILE in $(ls *$MYSOURCE*); do NEWFILE=$(echo $FILE| sed 's/$MYSOURCE/$MYDEST/g'); mv $FILE $NEWFILE ; done

Bests Regards
Den
Steven Schweda
Honored Contributor

Re: mv on multiple file with joker

> for FILE in $(ls); [...]

Or, if "$(ls)" causes problems, start with
"find", and do something similar.

bash$ ls -l
total 1
-rw-r----- 1 SMS 40 5 Mar 19 08:41 a b
-rw-r----- 1 SMS 40 5 Mar 19 08:41 c d
bash$ for file in $(ls) ; do echo $file ; done
a
b
c
d
bash$


bash$ find . -type f
./a b
./c d


"$(ls)" could also make the command line too
long.
Leo The Cat
Regular Advisor

Re: mv on multiple file with joker

About Environment variable, this seems do the job:


MYSOURCE=prect
MYDEST=precl
for FILE in $(ls *$MYSOURCE*); do NEWFILE=$(echo $FILE| sed 's/'$MYSOURCE'/'$MYDEST'/g'); mv $FILE $NEWFILE ; done

Any comment ? except the find insteaf of ls command.

Bests Regards
Den
Ivan Ferreira
Honored Contributor

Re: mv on multiple file with joker

Single quotes in your sed command probably won't expand the environment variables. Use double quote instead.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Leo The Cat
Regular Advisor

Re: mv on multiple file with joker

This works with single quote ....
Steven Schweda
Honored Contributor

Re: mv on multiple file with joker

> This works with single quote ....

Because you're quoting only tiny parts of
the command:
's/'
'/'
'/g'

Not much point in that. The hazards are in
the other parts.
emha_1
Valued Contributor

Re: mv on multiple file with joker

and what about simple:

rename precl prect *


emha.