PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Batch file rename in Linux (consecutive numbers)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Batch file rename in Linux (consecutive numbers)

Réponse
 
LinkBack Outils de la discussion
Vieux 31/08/2007, 20h38   #1
gibbonsp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Batch file rename in Linux (consecutive numbers)

Hello,

I'm good with Unix, but I haven't learned Perl yet.... I know Perl can
do this, but since I don't know Perl yet, I'm a little hung up.

Problem:

I need to rename a batch of files:

foo_001.jpg
foo_002.jpg
foo_003.jpg
bar_001.jpg
bar_002.jpg
bar_003.jpg
.....and so on.

I need to take the number on these files (notice how the number is
duplicated on foo and bar) and make the entire batch consecutive. In
other words, I want the file list to look like this:

foo_001.jpg
foo_002.jpg
foo_003.jpg
bar_004.jpg
bar_005.jpg
bar_006.jpg
.....and so on.

I'd appreciate any ideas! I just feel like this is so simple and I'm
just not seeing the answer!

Thanks -

Peter

  Réponse avec citation
Vieux 31/08/2007, 22h36   #2
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Batch file rename in Linux (consecutive numbers)

In article <1188589094.698861.37200@i38g2000prf.googlegroups. com>,
gibbonsp <gibbonsp@gmail.com> wrote:
(From)
>foo_001.jpg
>foo_002.jpg
>foo_003.jpg
>bar_001.jpg
>bar_002.jpg
>bar_003.jpg
>....and so on.


(To)

>foo_001.jpg
>foo_002.jpg
>foo_003.jpg
>bar_004.jpg
>bar_005.jpg
>bar_006.jpg
>....and so on.


gawk -F_ '{ print "mv -iv",$0,$1"_"sprintf("%03d.jpg",++n) }' file1 > /tmp/dofile

Then bring up /tmp/dofile in your editor, check it over, and when you
are happy with it (and you've confirmed that "mv -iv" behaves the same
way on your system as it does on mine), source the file from your shell.

  Réponse avec citation
Vieux 01/09/2007, 13h42   #3
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Batch file rename in Linux (consecutive numbers)

2007-08-31, 19:38(-00), gibbonsp:
> Hello,
>
> I'm good with Unix, but I haven't learned Perl yet.... I know Perl can
> do this, but since I don't know Perl yet, I'm a little hung up.
>
> Problem:
>
> I need to rename a batch of files:
>
> foo_001.jpg
> foo_002.jpg
> foo_003.jpg
> bar_001.jpg
> bar_002.jpg
> bar_003.jpg
> ....and so on.
>
> I need to take the number on these files (notice how the number is
> duplicated on foo and bar) and make the entire batch consecutive. In
> other words, I want the file list to look like this:
>
> foo_001.jpg
> foo_002.jpg
> foo_003.jpg
> bar_004.jpg
> bar_005.jpg
> bar_006.jpg
> ....and so on.

[...]

zsh (assuming autoload -U zmv in ~/.zshrc):

n=0; zmv -n '(*_)<->(*)' '$1${(l:3::0$((++n))}$2'

recursively:

n=0; zmv -n '(**/)(*_)<->(*)' '$1$2${(l:3::0$((++n))}$3'

Remove "-n" when happy.

--
Stéphane
  Réponse avec citation
Vieux 01/09/2007, 17h33   #4
John DuBois
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Batch file rename in Linux (consecutive numbers)

In article <1188589094.698861.37200@i38g2000prf.googlegroups. com>,
gibbonsp <gibbonsp@gmail.com> wrote:
>I need to rename a batch of files:
>
>foo_001.jpg
>foo_002.jpg
>foo_003.jpg
>bar_001.jpg
>bar_002.jpg
>bar_003.jpg
>....and so on.
>
>I need to take the number on these files (notice how the number is
>duplicated on foo and bar) and make the entire batch consecutive. In
>other words, I want the file list to look like this:
>
>foo_001.jpg
>foo_002.jpg
>foo_003.jpg
>bar_004.jpg
>bar_005.jpg
>bar_006.jpg
>....and so on.
>
>I'd appreciate any ideas! I just feel like this is so simple and I'm
>just not seeing the answer!


A ksh93 utility that has various options for just this sort of thing:

ftp://ftp.armory.com/pub/scripts/ren

In this case, it would be:

ren -m2=s -z3 '*_*.jpg'

("replace the second globbed segment with sequential integers that
contain 3 digits")

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
  Réponse avec citation
Vieux 01/09/2007, 17h33   #5
John DuBois
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Batch file rename in Linux (consecutive numbers)

In article <1188589094.698861.37200@i38g2000prf.googlegroups. com>,
gibbonsp <gibbonsp@gmail.com> wrote:
>I need to rename a batch of files:
>
>foo_001.jpg
>foo_002.jpg
>foo_003.jpg
>bar_001.jpg
>bar_002.jpg
>bar_003.jpg
>....and so on.
>
>I need to take the number on these files (notice how the number is
>duplicated on foo and bar) and make the entire batch consecutive. In
>other words, I want the file list to look like this:
>
>foo_001.jpg
>foo_002.jpg
>foo_003.jpg
>bar_004.jpg
>bar_005.jpg
>bar_006.jpg
>....and so on.
>
>I'd appreciate any ideas! I just feel like this is so simple and I'm
>just not seeing the answer!


A ksh93 utility that has various options for just this sort of thing:

ftp://ftp.armory.com/pub/scripts/ren

In this case, it would be:

ren -m2=s -z3 '*_*.jpg'

("replace the second globbed segment with sequential integers that
contain 3 digits")

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
  Réponse avec citation
Vieux 02/09/2007, 13h32   #6
arch119
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Batch file rename in Linux (consecutive numbers)

On Sep 2, 1:33 am, spce...@armory.com (John DuBois) wrote:
> In article <1188589094.698861.37...@i38g2000prf.googlegroups. com>,
>
>
>
> gibbonsp <gibbo...@gmail.com> wrote:
> >I need to rename a batch of files:

>
> >foo_001.jpg
> >foo_002.jpg
> >foo_003.jpg
> >bar_001.jpg
> >bar_002.jpg
> >bar_003.jpg
> >....and so on.

>
> >I need to take the number on these files (notice how the number is
> >duplicated on foo and bar) and make the entire batch consecutive. In
> >other words, I want the file list to look like this:

>
> >foo_001.jpg
> >foo_002.jpg
> >foo_003.jpg
> >bar_004.jpg
> >bar_005.jpg
> >bar_006.jpg
> >....and so on.

>
> >I'd appreciate any ideas! I just feel like this is so simple and I'm
> >just not seeing the answer!



Assuming that your filenames follow the pattern .*_¥d{3}.jpg


#!/bin/bash

#dir files contains all the files
files=`ls files`
i=1
padding="000"
for file in $files
do
((len=3-${#i}))
j="${padding:0:${len}}${i}"
newName=${file/_???./_${j}.}
mv files/${file} files/${newName}
((i=i+1))
done

  Réponse avec citation
Vieux 02/09/2007, 15h21   #7
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Batch file rename in Linux (consecutive numbers)

arch119 wrote:
>
> Assuming that your filenames follow the pattern .*_¥d{3}.jpg
>
>
> #!/bin/bash
>
> [...]
> i=1
> padding="000"
> [...]
> ((len=3-${#i}))
> j="${padding:0:${len}}${i}"
> [...]


If you want leading zeros:

$ i=7
$ printf -v j "%03d" $i
$ echo $j
007

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
  Réponse avec citation
Vieux 02/09/2007, 15h52   #8
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Batch file rename in Linux (consecutive numbers)

Hello,

arch119 wrote:
>
> #!/bin/bash
>
> #dir files contains all the files
> files=`ls files`


With output of "ls" you get problems with spaces and line breaks in filenames.

> i=1
> padding="000"
> for file in $files


better:
for file in *

> do
> ((len=3-${#i}))
> j="${padding:0:${len}}${i}"
> newName=${file/_???./_${j}.}
> mv files/${file} files/${newName}
> ((i=i+1))
> done




If you want leading zeros:

$ i=7
$ printf -v j "%03d" $i
$ echo $j
007

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h14.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18946 seconds with 16 queries