There are some tasks that we end up doing over and over by hand. For example, I frequently convert DOC and PDF files to text. Rather than opening a command prompt and running pdftotext or antiword every time I need to convert a file, I created an automated conversion directory.
The dnotify program watches a specified directory for any change. A change may be a file creation, update, renaming, deletion, permission modification, or all of the above. When a change happens, dnotify can run a script. I have a script that converts DOC and PDF files to text, and the directory being monitored is on my desktop. Using dnotify, I can convert files by simply dropping them into a directory on my desktop.
Install dnotify as well as any conversion tools you need. In this example, pdftotext and antiword will perform text conversions.
Tip | For graphic conversion, consider installing the netpbm package. This provides programs such as giftopnm, jpegtopnm, pnmtopng, pnmtogif, and pnmtojpeg. |
sudo apt-get install dnotify sudo apt-get install xpdf-utils # provides pdftotext sudo apt-get install antiword
Create a small script to convert all files in a directory. Listing 8-2 shows my convert2text script. This script takes a directory name on the command-line and scans the directory for all DOC and PDF files. Any file not already available as text is converted.
Listing 8-2: Simple convert2text Script to Convert Files to Text
#!/bin/sh # Be sure to make this executable: chmod a+x convert2text # Be sure to place it in your $PATH (e.g., sudo cp convert2text /usr/local/bin/) if [ "$1" = "" ] ; then echo "Usage: $0 directory" exit fi # Get list of files in the directory find "$1" -type f | while read Name ; do # Based on the file name, perform the conversion case "$Name" in (*.pdf) # convert pdf to text NameTxt="${Name%.pdf}.txt" if [ ! -f "${NameTxt}" ] ; then pdftotext "${Name}" "${NameTxt}"; fi ;; (*.doc) # convert doc to text NameTxt="${Name%.doc}.txt" if [ ! -f "${NameTxt}" ] ; then antiword "${Name}" > "${NameTxt}"; fi ;; esac done
Create a directory to monitor.
mkdir $HOME/Desktop/convert2text
Run the dnotify program. Specify the convert2text script and the directory to monitor. In this case, the action to watch for is file creation (-C). The dnotify program changes the ‘{}’ string to the directory name.
dnotify -C $HOME/Desktop/convert2text -e convert2text '{}'
Now, dragging or pasting any PDF or DOC file into this folder will create a text file. The text file will have the same name as the original document, but will end with the .txt extension. For example, ch08.doc will generate ch08.txt. If you are using the command line then you can copy (cp) or move (mv) files into $HOME/Desktop/convert2text/ for automatic conversion. Figure 8-6 shows a sample conversion directory.
Figure 8-6: Automatic file conversion using dnotify and the convert2text directory
Tip | If you are like me and frequently use the convert2text directory, then you can add the dnotify command to your Gnome startup (System Preferences Sessions Startup Programs). This way, the automatic conversion will be enabled immediately after you login. Also, you can use the -b option to run it in the background: dnotify -b -C $HOME/Desktop/ convert2text -e convert2text ‘{}‘. |