Merge multiple jpg into single pdf in Linux

0 votes

I used the following command to convert and merge all the jpg files in a directory to a single pdf file.

convert *.jpg file.pdf

The files in the directory are numbered from 1.jpg to 123.jpg. The conversion went fine but after converting the pages were all mixed up. I wanted the pdf to have pages from 1.jpg to 123.jpg in the same order as they are named. I tried with the following command as well:

cd 1 FILES=$( find . -type f -name "*jpg" | cut -d/ -f 2) mkdir temp && cd temp for file in $FILES; do BASE=$(echo $file | sed 's/.jpg//g'); convert ../$BASE.jpg $BASE.pdf; done && pdftk *pdf cat output ../1.pdf && cd .. rm -rf temp

But still no luck. Operating platform Linux. Any help will be appreciated!


 

Apr 13, 2022 in Linux Administration by Soham
• 9,710 points
1,313 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The problem is because your shell is expanding the wildcard in purely alphabetical order, and because the lengths of the numbers are different, the order will be incorrect:

$ echo *.jpg 1.jpg 10.jpg 100.jpg 101.jpg 102.jpg ...

The solution is to pad the filenames with zeros as required so they're the same length before running your convert command:

$ for i in *.jpg; do num=`expr match "$i" '\([0-9]\+\).*'`; > padded=`printf "%03d" $num`; mv -v "$i" "${i/$num/$padded}"; done

Now the files will be matched by the wildcard in the correct order, ready for the convert command:

$ echo *.jpg 001.jpg 002.jpg 003.jpg 004.jpg 005.jpg 006.jpg 007.jpg 008.jpg ...

answered Apr 14, 2022 by Aditya
• 7,680 points

edited Mar 5

Related Questions In Linux Administration

0 votes
0 answers

How to convert a PDF into JPG with command line in Linux?

What are fast and reliable ways for ...READ MORE

Apr 14, 2022 in Linux Administration by Aditya
• 7,680 points
661 views
0 votes
1 answer

Merge multiple pdf files in one using terminal

Install a utility call pdfunite which is ...READ MORE

answered May 17, 2019 in Linux Administration by ajs3033
• 7,300 points
891 views
0 votes
1 answer

How to create nested directories with a single command in linux?

Try this command. mkdir -p /parent_dir/son_dir READ MORE

answered Oct 19, 2020 in Linux Administration by anonymous
• 19,610 points
2,428 views
+2 votes
2 answers

What is 755 permission in Linux?

Hi, File permission 755 means that the directory ...READ MORE

answered Dec 9, 2020 in Linux Administration by MD
• 95,460 points
280,766 views
–1 vote
1 answer

How to get octal file permission in linux?

You can use this: stat -c "%a %n" ...READ MORE

answered Jan 3, 2019 in Linux Administration by Omkar
• 69,220 points
2,928 views
0 votes
1 answer

How to create a symlink in Linux?

To create a symbolic link, you can ...READ MORE

answered Feb 1, 2019 in Linux Administration by Omkar
• 69,220 points
1,402 views
0 votes
1 answer
0 votes
1 answer

Killing a Zombie Process

You can clean up a zombie process ...READ MORE

answered Jun 20, 2019 in Linux Administration by Upasana
• 8,620 points
1,164 views
0 votes
1 answer

How do I set variable if a specific package version is installed in CFEngine?

Here is what you can do.Just use packagesmatching to ...READ MORE

answered Jul 12, 2018 in Other DevOps Questions by Atul
• 10,240 points
1,431 views
0 votes
2 answers

Install postgreSQL on Ubuntu

Follow the below commands to install PostgreSQL (PSQL) ...READ MORE

answered Nov 12, 2020 in Database by Prachi
• 140 points
1,538 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP