# renaming files



## hirohitosan (Nov 8, 2009)

Hi there. I'm sorry if this was asked before.

From my digital camera I got files like that:
img_6775.jpg	img_7042.jpg	img_7051.jpg	img_7066.jpg img_7037.jpg	etc. ...

I want to rename like 01.jpg; 02.jpg; 04.jpg; 05.jpg; etc. ...

I installed mmv but still cannot figured out how to do this.
Can give me some advices?

thanks


----------



## pat (Nov 8, 2009)

If you're fond of GUI, install sysutils/pyrenamer.
It has some dependencies but is very handy here.


----------



## hirohitosan (Nov 8, 2009)

pat said:
			
		

> If you're fond of GUI


I want to use this on my server so I don't have X
there is no a text based solution?

thanks


----------



## graudeejs (Nov 8, 2009)

```
#/bin/sh
for i in `ls *.jpg`; do
  mv $i $(echo $i | sed 's/^img_//')
done
exit 0
```

This sh script should do it

EDIT, ah no, soory, this will only remove img_


----------



## graudeejs (Nov 8, 2009)

```
#!/bin/sh
j=0
for i in `ls *.jpg`; do
  j=`expr $(echo $j) + 1`
  if [ $j -lt 10 ]; then
    mv "$i" "[color="Red"]0[/color]0${j}.jpg"
  [color="Red"]elif [ $j -lt 100 ]; then
    mv "$i" "0${j}.jpg"[/color]
  else
    mv "$i" "${j}.jpg"
  fi
done
exit 0
```

ok here it is 

This will output file names with 3 digits.
If you want only 2 digits, remove text in red


I suggest you test it first in temp on some files 
and only then try on your collection (just to make 100% sure)


----------



## hirohitosan (Nov 8, 2009)

thanks killasmurf86
here what I done
I create a file: rename.sh, copy your script inside, make the file executable and copy in the folder where my files are

```
> ls
img_6775.jpg	img_7048.jpg	img_7056.jpg
img_7037.jpg	img_7050.jpg	img_7066.jpg	
img_7038.jpg	img_7051.jpg	img_7071.jpg
rename.sh
> ./rename.sh 
j=0: Command not found.
for: Command not found.
do: Command not found.
Illegal variable name.
j=: Command not found.
i: Undefined variable.
>
```
do I need bash, or something?


----------



## Beastie (Nov 8, 2009)

That's because it should be

```
#[b][u]![/u][/b]/bin/sh
```
instead of

```
#/bin/sh
```

You don't need bash. This line ensures the script is ran by the Bourne shell.


----------



## graudeejs (Nov 8, 2009)

jeeee, haven't programmed sh for few months... I'm a bit rusted


----------



## graudeejs (Nov 8, 2009)

there was another error, i fixed both... now just copy code, paste, save and try again, it should work now... (just tested on my system)


----------



## mickey (Nov 8, 2009)

Try this script:


```
#! /bin/sh

c=0

for i in `ls img_*.jpg`; do

        c=`expr $c + 1`
        o=`printf "%02d.jpg" $c`

        echo "${i} -> ${o}"

        mv "${i}" "${o}"
done
```


----------



## graudeejs (Nov 8, 2009)

hmm, why didn't i think of printf.


----------



## hirohitosan (Nov 8, 2009)

mickey said:
			
		

> Try this script:


well, thanks. It works perfectly !


----------



## vermaden (Nov 8, 2009)

@hirohitosan 


```
for I in images/*
do
  COUNT=$(( ${COUNT} +1 ))
  mv ${I} $( printf "%02d" ${COUNT} ).jpg
done
```


----------



## hirohitosan (Nov 8, 2009)

vermaden said:
			
		

> @hirohitosan


thanks! it works smoothly too


----------



## mix_room (Nov 9, 2009)

I would suggest adding some form of checking to determine if the file exists before moving. Otherwise you might overwrite a picture.


```
for I in images/*
do
  COUNT=$(( ${COUNT} +1 ))
  if [ ! -f $( printf "%02d" ${COUNT}).jpg ];
  do 
    mv ${I} $( printf "%02d" ${COUNT} ).jpg
  done
done
```


----------



## lbl (Nov 10, 2009)

*the easy way.*

This is bash tho ...


```
[lbl@atom0 ~/test]$ ls
IMG_1127.JPG	IMG_1130.JPG	IMG_1133.JPG	IMG_1136.JPG	IMG_1139.JPG	IMG_1142.JPG	IMG_1145.JPG	IMG_1148.JPG
IMG_1128.JPG	IMG_1131.JPG	IMG_1134.JPG	IMG_1137.JPG	IMG_1140.JPG	IMG_1143.JPG	IMG_1146.JPG	IMG_1149.JPG
IMG_1129.JPG	IMG_1132.JPG	IMG_1135.JPG	IMG_1138.JPG	IMG_1141.JPG	IMG_1144.JPG	IMG_1147.JPG	IMG_1150.JPG
[lbl@atom0 ~/test]$ n=0 ; ls | while read f ; do n=`expr $n + 1` ; mv $f $n.jpg ; done
[lbl@atom0 ~/test]$ ls
1.jpg	11.jpg	13.jpg	15.jpg	17.jpg	19.jpg	20.jpg	22.jpg	24.jpg	4.jpg	6.jpg	8.jpg
10.jpg	12.jpg	14.jpg	16.jpg	18.jpg	2.jpg	21.jpg	23.jpg	3.jpg	5.jpg	7.jpg	9.jpg
[lbl@atom0 ~/test]$
```

/lbl


----------



## vivek (Nov 20, 2009)

There there

```
#!/usr/bin/perl -w
#
#  This script was developed by Robin Barker (Robin.Barker@npl.co.uk),
#  from Larry Wall's original script eg/rename from the perl source.
#
#  This script is free software; you can redistribute it and/or modify it
#  under the same terms as Perl itself.
#
# Larry(?)'s RCS header:
#  RCSfile: rename,v   Revision: 4.1   Date: 92/08/07 17:20:30 
#
# $RCSfile: rename,v $$Revision: 1.5 $$Date: 1998/12/18 16:16:31 $
#
# $Log: rename,v $
# Revision 1.5  1998/12/18 16:16:31  rmb1
# moved to perl/source
# changed man documentation to POD
#
# Revision 1.4  1997/02/27  17:19:26  rmb1
# corrected usage string
#
# Revision 1.3  1997/02/27  16:39:07  rmb1
# added -v
#
# Revision 1.2  1997/02/27  16:15:40  rmb1
# *** empty log message ***
#
# Revision 1.1  1997/02/27  15:48:51  rmb1
# Initial revision
#
use strict;

use Getopt::Long;
Getopt::Long::Configure('bundling');

my ($verbose, $no_act, $force, $op);

die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n"
    unless GetOptions(
        'v|verbose' => \$verbose,
        'n|no-act'  => \$no_act,
        'f|force'   => \$force,
    ) and $op = shift;

$verbose++ if $no_act;

if (!@ARGV) {
    print "reading filenames from STDIN\n" if $verbose;
    @ARGV = <STDIN>;
    chop(@ARGV);
}

for (@ARGV) {
    my $was = $_;
    eval $op;
    die $@ if $@;
    next if $was eq $_; # ignore quietly
    if (-e $_ and !$force)
    {
        warn  "$was not renamed: $_ already exists\n";
    }
    elsif ($no_act or rename $was, $_)
    {
        print "$was renamed as $_\n" if $verbose;
    }
    else
    {
        warn  "Can't rename $was $_: $!\n";
    }
}
```

For example, to rename all files matching C<*.bak> to strip the extension, you might use

```
rename 's/\.bak$//' *.bak
```
To translate uppercase names to lower, you'd use

```
rename 'y/A-Z/a-z/' *
```


----------



## wayward (Nov 13, 2010)

Here is my 50 cents on this, i have choosen a script i understand and use it every 15 minutes to fetch the scans from my epson multifunctional, becease it just is not reliable, it looses connection every now and then, so i just let FreeBSD take care of this thing.

This is how my batch script looks like:


```
#!/bin/sh

## Copy the scans every 30 minutes from the printer, so we don't
## have to struggle to get the scans anymore
#
umount -f /mnt/EPSON

mount_smbfs -N //Epson00595a/MEMORYCARD/EPSCAN /mnt/EPSON

mv /mnt/EPSON/EPSCAN/001/* /encrypt_a/Special/Scans

cd /encrypt_a/Special/Scans

## Rename the Epson files to scans witch date and follow-up number
##
j=0
datum=`date '+%d-%m-%Y_%H-%M'`

for i in `ls EPSON*.PDF`; do
  j=`expr $(echo $j) + 1`
  if [ $j -lt 10 ]; then
    mv "$i" "scan_${datum}-00${j}.pdf"
  elif [ $j -lt 100 ]; then
    mv "$i" "scan_${datum}-0${j}.pdf"
  else
    mv "$i" "scan_${datum}-${j}.pdf"
  fi
done

j=0
datum=`date '+%d-%m-%Y_%H-%M'`

for i in `ls EPSON*.JPG`; do
  j=`expr $(echo $j) + 1`
  if [ $j -lt 10 ]; then
    mv "$i" "scan_${datum}-00${j}.jpg"
  elif [ $j -lt 100 ]; then
    mv "$i" "scan_${datum}-0${j}.jpg"
  else
    mv "$i" "scan_${datum}-${j}.jpg"
  fi
done

j=0
datum=`date '+%d-%m-%Y_%H-%M'`

for i in `ls EPSON*.jpg`; do
  j=`expr $(echo $j) + 1`
  if [ $j -lt 10 ]; then
    mv "$i" "scan_${datum}-00${j}.jpg"
  elif [ $j -lt 100 ]; then
    mv "$i" "scan_${datum}-0${j}.jpg"
  else
    mv "$i" "scan_${datum}-${j}.jpg"
  fi
done

umount -f /mnt/EPSON

exit 0
```

The scanner only produces PDF, JPG and jpg.

I almost forgot: THANKYOU!


----------

