LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack Search this Thread
Old 01-27-2010, 06:43 AM   #1
comiconomenclaturist
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Rep: Reputation: 0
bash script to rename (renumber) files


I'm trying to write a script to process some images and rename them, or more specifically, renumber them so that pg_0001.png becomes pg_0.png, pg_0002.png becomes pg_1.png, etc. I've looked at the rename command and sed, but I'm not really very familiar with these. It should also be part of a bash script that I've written for the processing of these files - this is what I have so far:

Code:
#!/bin/bash
pdftk 2012\ theatre\ Sep\ 2009.pdf burst
mogrify -format png -resize 1200 -negate pg_00**.pdf
rm pg_00**.pdf
So I just need the the next line to renumber the png's - any ideas?
 
Old 01-27-2010, 07:15 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Squeeze (Fluxbox WM)
Posts: 1,324
Blog Entries: 43

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
You might be able to use rename for this, eg:
Code:
rename 's/pg_([0-9]*)\.png/$1/; $_="pg_".($_-1).".png"' *.png
The first part of the rename strips off the non-numeric part; the second part subtracts 1 and regenerates the filename.

I'm assuming that the numbers are sequential and start at 1.

==LATER EDIT==

Slight improvement so that filenames without a number don't get mangled:

Code:
rename 'if (m/[0-9]+/) {$_ = "pg_".($&-1).".png";}' *.png

Last edited by neonsignal; 01-27-2010 at 07:57 AM.
 
1 members found this post helpful.
Old 01-27-2010, 07:24 AM   #3
pixellany
Guru
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 16,552

Rep: Reputation: 412Reputation: 412Reputation: 412Reputation: 412Reputation: 412
You say that you looked at rename and SED, but you are not familiar with them----Did you look at the man pages or any tutorials? The hardest part of this is actually changing the number

A brute force way using sed--NOT TESTED:
In a folder containing the only files to be renamed:

Code:
for fil in *; do
     newnum = $(($(echo $fil|sed 's/[^0-9]//g')-1))
     newname = pg_${newnum}.png
     mv $fil $newname
done
Test this (or any other code) on some duplicate files in a separate folder.
 
Old 01-27-2010, 07:44 AM   #4
comiconomenclaturist
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neonsignal View Post
Code:
rename 's/pg_([0-9]*)\.png/$1/; $_="pg_".($_-1).".png"' *.png
This works great for me, thanks. I'm learning some of these commands still, but this was a bit beyond me!
 
Old 01-27-2010, 07:58 AM   #5
David the H.
Senior Member
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 4,821

Rep: Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986Reputation: 986
According to the man page, you can specify the format in which pdftk outputs it's files.

Quote:
burst

Splits a single, input PDF document into individual pages. Also creates a report named doc_data.txt which is the same as the output from dump_data. If the output section is omitted, then PDF pages are named: pg_%04d.pdf, e.g.: pg_0001.pdf, pg_0002.pdf, etc. To name these pages yourself, supply a printf-styled format string via the output section. For example, if you want pages named: page_01.pdf, page_02.pdf, etc., pass output page_%02d.pdf to pdftk.
Edit: By the way, if the above doesn't do it for you, here's a way to rename the files that only uses bash built-ins. No need to call on external tools like sed.
Code:
i=1
for f in pg_*.pdf; do

   i=$(printf %02d $i)   #zero-pad "$i", if wanted

   mv "$f" "${f/%_*.pdf/_$i.pdf}"  #replace the orginal file ending with "$i.pdf"
   let i++     #increment "$i" for the next file

done

Last edited by David the H.; 01-27-2010 at 08:18 AM. Reason: additions and formatting
 
Old 07-22-2010, 09:24 AM   #6
dr_smit
Member
 
Registered: Jan 2007
Posts: 39

Rep: Reputation: 15
I have scanned a book with Xsane--
the pages have been scanned and stored in sets of 50's in folder named "50odd" "50even"
"100odd" "100even" "150odd" "150even" storing first 50 odd & even, next 50 odd & even, next 50 odd & even pages respectively as image-0001.pnm, image-0002.pnm and so on in each...

Now I wish to
1. know that in future scans can I have Xsane autonumber scan as odd numbers and even numbers seperately as it scans odd numbered pages first and then even numbered pages.

2. do number all files in "50odd" folder as page 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 and in "50even" as 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 and in "100odd" as 27, 29, 31, 33,35, 37, 39, 41, 43, 45, 47, 49 and in "100even" as 26, 28,30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50 and so on..

3. finally add all pages into a pdf book from page 1 to 150 page.
 
  


Reply


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 04:50 AM
bash script? how to rename files in random way? lefty.crupps Linux - Software 8 08-07-2009 12:39 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 09:00 AM
bash script to rename files removing one character Byenary Linux - Newbie 2 04-08-2008 11:12 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM


All times are GMT -5. The time now is 02:32 AM.

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
Open Source Consulting | Domain Registration