LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Renaming large number of files. (https://www.linuxquestions.org/questions/linux-general-1/renaming-large-number-of-files-595164/)

checkmate3001 10-28-2007 02:06 AM

Renaming large number of files.
 
Hello ladies and gents,

I have a very large number (~500) of files I need to rename. I suppose I could write a perl script to do it... but I'm looking for something quick and dirty.

I need to rename files that look like this:
EM76540.pdf

to this:
76540EM.pdf

Can I use bash with mv or am I doing to have to write some perl script?

I wish you could just type mv EM*.pdf *EM.pdf

That would be awesome.

jschiwal 10-28-2007 02:20 AM

There is a rename command that can do bulk renames.

I will use variable expansion instead.
Code:

for file in EM*.pdf; do
  tmp=${file#EM}
  mv ${file} ${tmp%.pdf}EM.pdf
done

Or
Code:

for file in EM*pdf; do
  mv $file ${file//[[:alpha:].]}EM.pdf
done

The latter deletes all alphabetic characters & the period from the variable.

Junior Hacker 10-28-2007 02:59 AM

I usually refer to this article for such tasks and can usually figure out how to come up with the right command through trial and error on some test subjects relatively quickly.

checkmate3001 10-28-2007 04:00 AM

Thank you both!

jschiwal that simple script did exactly what I needed. I can see why you're a guru. I really appreciate it. For me to do that with a perl script would have taken me hours I'm sure.

I haven't had a chance to visit that site you mentioned Junior Hacker - it stated it was too busy. But it looks like something I will definitely look at later.


Man... I need to learn more scripting. That's a handy tool.

jmiter 07-15-2008 07:47 AM

Renaming in bulk, more complicated example
 
Can anyone provide some advice on a more complicated example to rename files...

I have files in the example format:
Babypicture_playing_with_spoon_date=20070602

Where the front of the file name is always a constant number of characters (here its 11 'Babypicture'), the middle is a descriptor, and the end is a constant number of characters for the date (14 characters, _date=20070602), with no end descriptor (.jpg is not there)

How can I rename these, using perl, to
Babypicture_date=20070602.jpg

Where I am cutting out the middle descriptor and adding the .jpg at the end?

Thanks

colucix 07-15-2008 08:08 AM

I don't know using Perl, but in Bash you can do something like
Code:

file="Babypicture_playing_with_spoon_date=20070602"

newfile=${file:0:11}${file:$((${#file}-14))}.jpg

basically using substring extraction and concatenation.

nx5000 07-15-2008 08:14 AM

http://www.linuxquestions.org/questi...36#post2939436

jmiter 07-15-2008 08:34 AM

Thanks Colucix.

Will try it, but hopefully someone can guide me on some perl code for this too. Seem like bash fails for me when the list of files is to long - I get the "argument list to long" error

jmiter 07-15-2008 10:09 AM

Perhaps a little more guidance...

I ran the commands on the command line and also in a file

#/bin/sh

file="Baby*"
newfile=${file:0:11}${file:$((${#file}-14))}.jpg

No file names are changed and there is no output of errors.
What am I doing wrong?

Thanks

trickykid 07-15-2008 10:12 AM

Quote:

Originally Posted by jmiter (Post 3215251)
Perhaps a little more guidance...

I ran the commands on the command line and also in a file

#/bin/sh

file="Baby*"
newfile=${file:0:11}${file:$((${#file}-14))}.jpg

No file names are changed and there is no output of errors.
What am I doing wrong?

Thanks

Umm.. well, you need more in your script than just:

Code:

file="Baby*"
newfile=${file:0:11}${file:$((${#file}-14))}.jpg

You need the mv or cp commands, however you're going to approach in renaming.

jmiter 07-15-2008 10:29 AM

yes, I actually had done:
mv file="Baby*" newfile=${file:0:11}${file:$((${#file}-14))}.jpg

but was returned an error:
mv: cannot stat `file=Baby*': No such file or directory

Recommendation on the placement of mv? Any usage with other file notation such as ()/\{} or others?

Thanks

colucix 07-15-2008 10:43 AM

Of course. Mine was only a little guidance on how to build the new file name in BASH, based on your requirements. You have to embed it in a little script which retrives all the file names and rename them one at a time.

Note: at this point, since it looks like you are not experienced in shell programming, you have to do a backup of your pictures/files before trying to rename them!

A simple script could be
Code:

#!/bin/bash
for file in Baby*
do
  newfile=${file:0:11}${file:$((${#file}-14))}.jpg
  echo mv $file $newfile
done

This works by looping over the list of files whose name begin with "Baby" and assigns each file name to the variable "file", one at a time. Then build the new file name and finally perform the mv command to rename it. I intentionally put an echo in front of the mv command to let you verify what the script will do, before actually doing it: when you've verified that the mv commands are good, strip out the echo command and launch the script again.

This code works only 1) if the files are all in the working directory and 2) if file names don't contain blank spaces, otherwise you have to slightly modify the code to let it work properly.

colucix 07-15-2008 10:46 AM

Quote:

Originally Posted by jmiter (Post 3215276)
yes, I actually had done:
mv file="Baby*" newfile=${file:0:11}${file:$((${#file}-14))}.jpg

I strongly suggest to not try renaming/deleting/moving commands without knowing exactly what you're doing, that is without knowing the exact syntax! Unless you like living on the edge... ;)

jmiter 07-15-2008 10:49 AM

Occasionally, I do attempt living on the edge :D. But in this case, I copied some sample files to a test directory in order to perfect the renaming command.

colucix 07-15-2008 11:12 AM

He he! :) Very good!


All times are GMT -5. The time now is 12:39 PM.