Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a lot of pdf files (3308) on which I must apply four steps:

1) I have to convert all to jpg

I found this little script on the web with ImageMagick: batch converting pdf to jpg

I want to do this, but I keep my files the same name as before: foo.pdf => foo.jpg Also I would like all images are saved in a folder in "scenes"

2) Then I have to resize to 612x792

3) Then I have to create thumbnails in 255x330

4) Finally I have to rename them. Indeed, I have a csv file in which is their names and new names

Here is an example of some lines.
Each line corresponds to the "actual name" file "comma" "the new name." There are 3308 rows, one per file

current_name,new_name
foo,bar
PS130_1060,55-large

As you can see, the extensions are not displayed because the two files are in jpg format.

I am completely lost, I do not know whether to use 4 scripts or if it is possible in one script. I often work in PHP, but I wanted to do a bit of bash to change, but it's a bit hot for me.

Can you help me?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

You can start like this:

for i in "$@"; do
  dst="${i%pdf}jpg"
  convert "$i" -resize 612x792 "$dst"
  convert "$i" -resize 255x330 "${i%.pdf}_thump.jpg"
done

And call it like

$ bash my_script.sh *.pdf

For renaming you can use another script. I don't understand your example .csv-file. Does is contain 3 lines for 3 files? Ok, this is the case.

You can rename the file with following command line:

$ awk -F, '{ system("echo mv \"" $1 "\" \"" $2 "\"")  }' myrename.csv

Awk executes the echo ... command for each line, $1 is the value of the first field of a line and $2 is the value of the 2nd field. The quoting \" is needed in case a filename contains spaces. -F, tells awk to use a comma as field separator.

If you have tested this command you can remove the echo to do real renaming of files. You can add -n to mv to avoid accidental overwrites of existing files.

share|improve this answer
    
Each line corresponds to the "actual name" file "comma" "the new name." There are 3308 rows, one per file –  Raphaël Oct 7 '11 at 9:24
    
@Raphaël, updated the answer –  maxschlepzig Oct 7 '11 at 9:58
    
The first part works. Approximately 15 minutes to convert, this is good. I test your second part. I do not think it to fit on a single order ^ ^! –  Raphaël Oct 7 '11 at 10:13
    
I delete the "echo" $ awk -F, '{ system("mv \"" $1 "\" \"" $2 "\"") }' myrename.csv ... for each line, I have an error : mv: cannot stat `S160_3237.jpg': No such file or directory ... but the file is rename. It works, but it's strange ! –  Raphaël Oct 7 '11 at 10:35
    
@Raphaël, perhaps duplicated entries in myrename.csv? –  maxschlepzig Oct 7 '11 at 13:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.