Operating System - Linux
1827811 Members
1884 Online
109969 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.
Dennis Handly
Acclaimed Contributor

Re: mv on multiple file with joker

>This works with single quote

Yes, you can stutter your quotes like that but it is hard to read. If you can use double quotes, do so.
Of course if you use double quotes and have to use lots of backslashes, that is also hard to read.
Stephen P. Schaefer
Frequent Advisor

Re: mv on multiple file with joker

There's no need to invoke sed. If one can assume that neither prefix nor suffix matches "precl", in a bash shell you can

for i in *precl*; do mv "$i" "${i%%precl*}prect${i##*precl}"; done

Modern bash allows a very long command line length, so that probably won't hit the limit; if you're concerned that it might then you might do

ls | grep precl | while read i; do mv "$i" "${i%%precl*}prect${i##*precl}"; done