LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-23-2014, 11:12 PM   #1
farrukhi
LQ Newbie
 
Registered: Sep 2014
Posts: 8

Rep: Reputation: Disabled
converting multiple jpg to a pdf according to timestamp


Hey all,

I am not familiar with command lines. I want to take a series (100s) of jpg files and convert them into one pdf, but sorted according to the timestamp (earliest jpg first).

I was able to convert the multiple jpg's using the 'convert' command:

convert *.jpg foo.pdf

but this sorts them according to the filename. I know the command 'ls -t -r' will give me a list of the filenames (earliest first). but I can't figure out how to do both... help would be greatly appreciated.
 
Old 09-23-2014, 11:40 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

it's not very robust (Eg will probably fail if the files have spaces in their names), but the following will probably work in most cases:
Code:
convert $(ls -t -r *.jpg) foo.pdf
Evo2.
 
Old 09-24-2014, 12:02 AM   #3
farrukhi
LQ Newbie
 
Registered: Sep 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks Evo2 for this.

This is an advancement. The jpg filenames don't have any spaces, so that's good. I tried this command, but after a few minutes the command stopped and i just got the message in the cmd line 'Killed'. Without any output. I can only assume that this isn't working because I am working with too many files in the command by using the $. I am operating on about 150 files, and in the future i will be using possibly even more files. any work arounds?

Last edited by farrukhi; 09-24-2014 at 12:11 AM.
 
Old 09-24-2014, 12:14 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

is your problem repeatable? Please run the following and the copy the terminal session into [code] tags.
Code:
time convert *.jpg all0.pdf
time convert $(ls -t -r *.jpg) all1.pdf
Evo2.
 
Old 09-24-2014, 12:35 AM   #5
farrukhi
LQ Newbie
 
Registered: Sep 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Here is what I get...

Code:
time convert *.JPG all0.pdf
Killed

real	8m3.732s
user	0m39.139s
sys	0m20.009s

Code:
time convert $(ls -t -r *.JPG) all1.pdf
Killed

real	1m14.090s
user	0m31.978s
sys	0m13.841s
 
Old 09-24-2014, 12:54 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

strange. Does it work with fewer files?

Evo2.
 
Old 09-24-2014, 01:33 AM   #7
farrukhi
LQ Newbie
 
Registered: Sep 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
When I tried running it with 195 files (original) it failed, 100 files as well. but worked when i reduced it to 65 files.

Code:
time convert $(ls -t -r *.JPG) all1.pdf

real	0m48.045s
user	0m37.566s
sys	0m6.148s
i am dealing with .jpg files that are each about 2MB. And the output of the 65 files is as expected about 120MB. When i did these processes in OSX (i recently made the move to Linux), I would compress the pdf using osx's color utility by 75%. If this is a question of memory, i wonder if i could just compress the files before hand? Or...?
 
Old 09-24-2014, 01:45 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

you could convert each jpg to pdf and then join all the pdfs. Eg something like:
Code:
pdfs=""
while read f ; do
  pdf=${f%.*}.pdf
  convert $f ${pdf}
  pdfs="${pdfs} ${pdf}
done <<(ls -r -t -1 *.JPG)
# Join all the newly created pdf files into a single pdf file
pdftk ${pdfs} cat output foo.pdf
# Remove the individual pdf files.
rm ${pdfs}
Note that this is not really tested.

Evo2.
 
1 members found this post helpful.
Old 09-24-2014, 02:16 AM   #9
farrukhi
LQ Newbie
 
Registered: Sep 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
mm...i can't really debug this code, but this is the output i get.. (i imagine this back and forth will be quite time consuming for you)..

Code:
./jpgtopdf
./jpgtopdf: line 5: unexpected EOF while looking for matching `"'
./jpgtopdf: line 11: syntax error: unexpected end of file
 
Old 09-24-2014, 02:32 AM   #10
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

Oops, add " to the end of line 5. Ie it should be like:
Code:
  pdfs="${pdfs} ${pdf}"
Evo2.
 
Old 09-24-2014, 03:01 AM   #11
farrukhi
LQ Newbie
 
Registered: Sep 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
yikes..another bug..

Quote:
./jpgtopdf: line 6: syntax error near unexpected token `('
./jpgtopdf: line 6: `done <<(ls -r -t -1 *.JPG)'
 
Old 09-24-2014, 03:06 AM   #12
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

sorry, that works with zsh, for bash you need a space between the two <. Ie the line terminating the loop should be:
Code:
done < <(ls -r -t -1 *.JPG)
and in this case I think you don't need the -1. So, the following should work too
Code:
done < <(ls -r -t *.JPG)
Evo2.
 
1 members found this post helpful.
Old 09-24-2014, 03:37 AM   #13
farrukhi
LQ Newbie
 
Registered: Sep 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
This works brilliantly! This is great. Much appreciated.

What would your suggestion be for adding a line to compress the pdf (e.g. to 1/3rd the size)?
 
Old 09-24-2014, 03:43 AM   #14
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

both convert and pdftk seem to have compression options. I suggest you check their man pages and perhaps have a search online too. I think the optimum compression will really depend on the content of the input files.

HTH,

Evo2.
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert multiple jpg files into pdf Brocolli Linux - Software 5 05-08-2013 09:43 AM
Converting .png to .jpg GtkUser Linux - Software 9 04-17-2007 02:04 PM
Converting to .jpg format satimis Linux - General 15 02-08-2006 02:12 AM
Converting the date (not timestamp) mpgram Programming 2 04-18-2004 03:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:55 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration