Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Find all the PDF containing a string

  1. #1
    Join Date
    Mar 2009
    Location
    Italy
    Beans
    29

    Find all the PDF containing a string

    I've written a little bash script that makes it possible to search the contents all the pdfs in a certain folder to find the occurrences of a string.
    It uses a zenity gui so it's quite user friendly; after the search is completed the user is provided with a list of files and a double-click opens evince on the page where the first occurrence of the string is.
    I hope it can be useful to somebody!

    Code:
    #!/bin/bash
    
    IFS="
    "
    
    # Select Directory
    NEWPATH=$(zenity --file-selection --directory --title="Select the Directory")
    if [ -z "$NEWPATH" ]; then
    	echo 'No Directory Selected'
    	exit 1
    fi
    cd $NEWPATH
    
    files=$(ls *.pdf -1)
    if [ $? != "0" ]; then
    	zenity -error --text='No files to convert!'
    	exit 3
    fi
    
    #Get Keyword
    KEYWORD=$(zenity --entry --text="Enter Keyword" --title="Search in PDF")
    if [ -z "$KEYWORD" ]; then
    	echo 'No keyword'
    	exit 2
    fi
    
    #Files text exctraction and matching
    ( for MYFILE in $files; 
    	do
    	result=$(pdftotext $MYFILE  - | grep $KEYWORD -i)
    	if [ -n "$result" ]; then
    		echo $MYFILE >> '/tmp/pdfFinder'
    	fi
    done
    ) |  zenity --progress --title='Processing PDFs' --pulsate --auto-close
    
    #Checking Results
    list=$(cat '/tmp/pdfFinder')
    if [ -z $list ]; then
    	zenity --error --text='No pdf found!'
    	exit 5
    fi
    rm '/tmp/pdfFinder'
    
    #Show List until user exits the program or selects a file
    while [ 1 ]; do
    	fileToOpen=$(zenity --list --column="FileName" --title="Files containing \"$KEYWORD\"" $list)
    	if [ $? == '0' ]; then
    		if [ -z "$fileToOpen" ]; then
    			zenity --error --text='No file Selected'
    		else
    			#Running pdf viewer already looking for our word
    			evince --find=$KEYWORD $fileToOpen
    		fi
    	else
    		exit 4
    	fi
    done
    edit. edit a line in order to keep the results window open
    Attached Files Attached Files
    Last edited by badook; June 13th, 2009 at 03:43 PM.

  2. #2
    Join Date
    Dec 2007
    Location
    United States
    Beans
    2,900
    Distro
    Ubuntu

    Re: Find all the PDF containing a string

    Well done. Thanks for taking time to post this.

  3. #3
    Join Date
    Feb 2010
    Beans
    9

    Re: Find all the PDF containing a string

    Thanks very much badook - I was looking for something like this. I also found pdfsearch on sourceforge, but this might be a better option for me since it looks at the pdf files one-by-one. I'll give it a try...

  4. #4
    Join Date
    Feb 2010
    Beans
    9

    Re: Find all the PDF containing a string

    It did the job - well done

    I was impressed with zenity too - must keep it in mind for my scripts.

    Good luck.

  5. #5
    Join Date
    Aug 2010
    Beans
    19

    Re: Find all the PDF containing a string

    Hello!
    I am ubuntu newbe ...and I desperately need pdf searcher
    sooo my question is

    how do I start this script?

    I tried with
    ./FindPdf.sh
    from the place where script is

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Find all the PDF containing a string

    and what happened? did you mark it as an executable? and do you have that zenity app installed?

  7. #7
    Join Date
    Aug 2010
    Beans
    19

    Re: Find all the PDF containing a string

    Quote Originally Posted by Vaphell View Post
    and what happened? did you mark it as an executable? and do you have that zenity app installed?
    Well I have zenity installed and
    when I put ./FindPdf.sh
    in terminal, response is "permission denied"
    then I checked permission for script and I have it (or I thing that I have it)

    when I put
    sudo ./FindPdf.sh
    after entering pass
    response is "command not found"

    what am I doing wrong?

  8. #8
    Join Date
    Jun 2008
    Location
    Lincolnton, NC
    Beans
    62
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Find all the PDF containing a string

    Try:
    Code:
    $ chmod 755 ./FindPdf.sh
    $ ./FindPdf.sh
    You may need to use sudo to run the chmod command.

    Maybe sudo is looking in /bin or /usr/bin for the script. You could try copying it to each of those locations, but maybe someone has a better idea before you throw extra stuff into the system folders...
    Code:
    $ sudo cp ./FindPdf.sh /bin
    $ sudo cp ./FindPdf.sh /usr/bin
    Nice script, badook.
    Software: Ubuntu 10.04 "Lucid Lynx" (x86_64); Linux Kernel 2.6.32-26-generic; Gnome 2.30.2; X.Org 1.7.6
    Hardware: Hewlitt-Packard HDX16; Intel Core 2 Duo T6600 @ 2.2 GHz; 3.9 GiB RAM; nVIDIA GeForce 9600M GT (512 MB RAM)

  9. #9
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Beans
    214
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Find all the PDF containing a string

    You can always use "beagle" (Synaptic). It indexes all documents, including .pdf files. Then you can search with Beagle Search". Very fast.

    /luigi

  10. #10
    Join Date
    Aug 2010
    Beans
    19

    Re: Find all the PDF containing a string

    Quote Originally Posted by Forgotten Path View Post
    Try:
    Code:
    $ chmod 755 ./FindPdf.sh
    $ ./FindPdf.sh
    You may need to use sudo to run the chmod command.

    Maybe sudo is looking in /bin or /usr/bin for the script. You could try copying it to each of those locations, but maybe someone has a better idea before you throw extra stuff into the system folders...
    Code:
    $ sudo cp ./FindPdf.sh /bin
    $ sudo cp ./FindPdf.sh /usr/bin
    Nice script, badook.
    thanks for replay I will tray that

    Quote Originally Posted by luigi_mb View Post
    You can always use "beagle" (Synaptic). It indexes all documents, including .pdf files. Then you can search with Beagle Search". Very fast.

    /luigi
    Thanks for the idea, beagle work great!!

    but I still need to figure out how to start scripts

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •