How to "mass" renaming files

Ben Browning plug at emptiedout.com
Fri Aug 19 12:37:26 MST 2005


Better yet, there is a utility called "rename" that comes with perl5
that does the same thing, except you use perl regular expressions to do
the deed.

So, for example:

benb at nulled:~/rename$ touch foo\ \ \ bar
benb at nulled:~/rename$ touch foo\ bar\ bar
benb at nulled:~/rename$ touch cherry\ bar\ foo
benb at nulled:~/rename$ ls -al
total 8
drwxr-xr-x   2 benb benb 4096 2005-08-19 12:36 .
drwxr-xr-x  84 benb benb 4096 2005-08-19 12:35 ..
-rw-r--r--   1 benb benb    0 2005-08-19 12:36 cherry bar foo
-rw-r--r--   1 benb benb    0 2005-08-19 12:35 foo   bar
-rw-r--r--   1 benb benb    0 2005-08-19 12:35 foo bar bar
benb at nulled:~/rename$ rename 'tr/ /_/' *
benb at nulled:~/rename$ ls -al
total 8
drwxr-xr-x   2 benb benb 4096 2005-08-19 12:36 .
drwxr-xr-x  84 benb benb 4096 2005-08-19 12:35 ..
-rw-r--r--   1 benb benb    0 2005-08-19 12:36 cherry_bar_foo
-rw-r--r--   1 benb benb    0 2005-08-19 12:35 foo___bar
-rw-r--r--   1 benb benb    0 2005-08-19 12:35 foo_bar_bar
benb at nulled:~/rename$

Use rename -n to do a dry run to see what it would change.

~Ben
MCR wrote:
> Here is a script that I have been using a lot lately,
> it is a simple shell scripts:
> 
> #!/bin/bash
> number=12
> for img in  *.jpg
> do
>         mv $img $number.jpg
>         $((number = number + 1))
> done
> 
> this will take all file in a directory that ends in
> jpg and will rename it to the name of $name.  You can
> set $name to start at what number you want and it will
> increment by one.  I have gone as high as 253
> pictures, I do not know what number it will stop at.
> 
> Hope it Helps.
> 
> Mike
> 
> 
> --- Josef Lowder <joe at actionline.com> wrote:
> 
> 
>>Can anyone provide a simple shell script command to 
>>change the file names of all the files in a given
>>directory? 
>>	from pict0001.jpg to just 01.jpg 
>>	from pict0002.jpg to just 02.jpg etc. etc. 
>>with the numbers running to a few hundred pix, all
>>with 
>>the same "pict00..." first part of the filename. 
>>
>>I looked on the 'net to find a shell script to
>>"mass" rename the files 
>>in any given directory and found the example below
>>to rename the 
>>suffix from php3 to just php, but I couldn't figure
>>out how to modify 
>>that to change the filename itself. 
>>
>> ls -d *.php3 | sed 's/\(.*\).php3$/mv "&"
>>"\1.php"/' | sh 
>>
>>I also found a *nix utility on my system named
>>'rename' but I 
>>couldn't get it to work.  Below is an excerpt from
>>the 'man' page 
>>for 'rename' ... but what in the world is 'foo'?? 
>>
>>MAN PAGE excerpt: 
>>rename will rename the specified files by replacing 
>>the first occurrence of from in their name by to. 
>>For example, given the files foo1, ..., foo9, foo10,
>>..., foo278, the 
>>commands
>>
>>rename foo foo0 foo? 
>> rename foo foo0 foo?? 
>>
>>will turn them into foo001, ..., foo009, foo010,
>>..., foo278.
>>And
>>rename .htm .html *.htm		will fix the extension of
>>your html files. 
>>
>>---------------------------------------------------
>>PLUG-discuss mailing list -
>>PLUG-discuss at lists.plug.phoenix.az.us
>>To subscribe, unsubscribe, or to change  you mail
>>settings:
>>
> 
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss
> 
> 
> 
> One is led to truth through the interpretation of a myth.
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> ---------------------------------------------------
> PLUG-discuss mailing list - PLUG-discuss at lists.plug.phoenix.az.us
> To subscribe, unsubscribe, or to change  you mail settings:
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss
> 
> 

-- 
---
"Confession only helps if you actually feel bad for your actions.
For you, it would just be a really long boast."
	-Tara
http://www.emptiedout.com


More information about the PLUG-discuss mailing list