Showing posts with label shell-programming. Show all posts
Showing posts with label shell-programming. Show all posts

Saturday, April 3, 2010

Merging pdf files

In the previous post I discussed how to write some basic shell scripts that allow you to do many of the repetitive tasks automatically instead of typing the commands into the terminal manually.

Well, one of the tasks that I very often find myself doing is merging multiple pdf files into a single pdf file. The very fact that you can merge multiple pdf files using linux is itself a very nice thing. Better still, you can automate it by writing a script.

Below is the command that you would generally enter into a terminal. Of course, you need the software called ghostscript installed. After installing gs, collect all pdf files that you want to merge into a single folder and execute the following command in the terminal. The code given below is adapted from [1]

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=mergedAll.pdf -dBATCH *.pdf 

Once executed, you will have the resulting merged pdf file in mergedAll.pdf. Now, if you want to repeat the above process, make sure you delete/rename mergedAll.pdf and then execute the above command again.

A few more niceties that I like to have in my script besides the above command is to first automatically delete/rename any file called mergedAll.pdf so that I can keep adding new files into the folder and merging them. Secondly I also like to delete/move all previous pdf files so that I have only the merged pdf file in the folder.

Here is the script, to achieve all of the above :

a) For deleting all pdf files after merging,

mv mergedAll.pdf merged.pdf
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=mergedAll.pdf -dBATCH *.pdf 
mv mergedAll.pdf mergedAll
rm *.pdf
mv mergedAll mergedAll.pdf

b) For moving all pdf files into a subfolder after merging,

mv mergedAll.pdf merged.pdf
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=mergedAll.pdf -dBATCH *.pdf 
mv mergedAll.pdf mergedAll
mv *.pdf pdf
mv mergedAll mergedAll.pdf


[1] http://www.everyjoe.com/newlinuxuser/merge-multiple-pdfs-into-one-file

Thursday, January 14, 2010

Automating repetitive tasks using Linux Shell programming

So, here is the scenario. Suppose you have a .wav file on your system and you want to convert it to .mp3. How do you do that?

In case, you are not already familiar with ffmpeg, well, you can always learn about it here. It can be used for many purposes but the main way in which i use it is for converting audio/video files from one format into another. The way to do that (after installing ffmpeg, of course) is to type in
    $ ffmpeg -i <input-File>.wav <output-File>.mp3
As simple as that !!!

Now, imagine that you have a folder that has plenty of .wav files and you want to convert all of them to .mp3 then, you could type in the name of each file and use the command as above which is really tiresome if you have plenty of .wav files. Or, you can use shell programming to automate the task as given in the code below.

Type in the code below and save it in some file, say ffmpeg.sh.
#!/bin/bash

for arg in $( ls *.WAV)
do
    ffmpeg -i $arg -acodec libmp3lame $arg.mp3
done


In order to run the above program all you have to do is, type in the following in command line,
    $chmod u+x ffmpeg.sh
    $./ffmpeg.sh
         or
    $sh ffmpeg.sh


There are many documentations available online for learning shell programming but you need not understand the complete thing to create small programs such as the one above. If you already know some programming then you need to know only the relevant stuff to write your own programs to automate whatever tasks that you are interested in.

Now, another similar scenario. Suppose I had plenty of .wav files and instead of converting I wanted to play them one after another with a gap of 3 seconds between any two .wav files. I just have to type the code below into a file and then run it in a similar fashion as the previous file.
#!/bin/bash

for arg in $( ls *.WAV)
do
    mplayer $arg
    sleep 3
done

I hope that proves useful in automating the repetitive tasks for you on your system.