LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-28-2007, 02:06 AM   #1
checkmate3001
Member
 
Registered: Sep 2007
Location: Folsom, California
Distribution: Debian 4.0 (Etch), Debian 5.0 (Lenny), Ubuntu 8.04
Posts: 301

Rep: Reputation: 32
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.
 
Old 10-28-2007, 02:20 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 10-28-2007, 02:59 AM   #3
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 59
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.
 
Old 10-28-2007, 04:00 AM   #4
checkmate3001
Member
 
Registered: Sep 2007
Location: Folsom, California
Distribution: Debian 4.0 (Etch), Debian 5.0 (Lenny), Ubuntu 8.04
Posts: 301

Original Poster
Rep: Reputation: 32
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.
 
Old 07-15-2008, 07:47 AM   #5
jmiter
LQ Newbie
 
Registered: Jul 2005
Posts: 12

Rep: Reputation: 0
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
 
Old 07-15-2008, 08:08 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 07-15-2008, 08:14 AM   #7
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 53
http://www.linuxquestions.org/questi...36#post2939436
 
Old 07-15-2008, 08:34 AM   #8
jmiter
LQ Newbie
 
Registered: Jul 2005
Posts: 12

Rep: Reputation: 0
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
 
Old 07-15-2008, 10:09 AM   #9
jmiter
LQ Newbie
 
Registered: Jul 2005
Posts: 12

Rep: Reputation: 0
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
 
Old 07-15-2008, 10:12 AM   #10
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 199Reputation: 199
Quote:
Originally Posted by jmiter View Post
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.
 
Old 07-15-2008, 10:29 AM   #11
jmiter
LQ Newbie
 
Registered: Jul 2005
Posts: 12

Rep: Reputation: 0
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
 
Old 07-15-2008, 10:43 AM   #12
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 07-15-2008, 10:46 AM   #13
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by jmiter View Post
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...
 
Old 07-15-2008, 10:49 AM   #14
jmiter
LQ Newbie
 
Registered: Jul 2005
Posts: 12

Rep: Reputation: 0
Cool

Occasionally, I do attempt living on the edge . But in this case, I copied some sample files to a test directory in order to perfect the renaming command.
 
Old 07-15-2008, 11:12 AM   #15
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
He he! Very good!
 
  


Reply

Tags
files, mv, rename


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assigning a large float number in E notation edM Programming 3 05-09-2005 12:51 PM
Deleting a large number of files msteudel Linux - General 4 01-26-2005 01:36 AM
Large Number of files? mikeshn Linux - Security 2 01-10-2004 06:11 AM
Java: Compile Large number of source files ? mikeshn Programming 7 10-07-2003 11:33 AM
Large number of open ports RefriedBean Linux - Security 3 07-05-2002 11:34 PM


All times are GMT -5. The time now is 06:19 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration