LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-04-2008, 10:15 AM   #1
Luvz2Fly
LQ Newbie
 
Registered: Aug 2005
Location: Florida
Distribution: Fedora, CentOS
Posts: 15

Rep: Reputation: 0
Run a command or a script recursively


Good day everyone...
I'm trying to figure this out on my own but I surrender! I have a command line program that converts HTML files to PDF but I need it to start in the main directory and traverse through each directory hitting every HTML file (basically do a batch HTML to PDF conversion)

The basic command I need to run is:

htmldoc -f output.pdf --webpage *.html


How do I execute this command starting at top directory and going to each subdirectory? If it helps there is only ONE level of subdirectory:

>START
|__Dir1
|file1.html
|file2.html
|__Dir2
|file1.html
|__Dir3
|file1.html
|file2.html
|file3.html

etc...

Thanks!
 
Old 12-04-2008, 10:47 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,330

Rep: Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089
Quote:
Originally Posted by Luvz2Fly View Post
Good day everyone...
I'm trying to figure this out on my own but I surrender! I have a command line program that converts HTML files to PDF but I need it to start in the main directory and traverse through each directory hitting every HTML file (basically do a batch HTML to PDF conversion)

The basic command I need to run is:

htmldoc -f output.pdf --webpage *.html

How do I execute this command starting at top directory and going to each subdirectory? If it helps there is only ONE level of subdirectory:
You don't say what you've already tried, or if you need a script file to do this, but if all the files are in one directory/sub-directory location, you could do:

htmldoc -f output.pdf --webpage `find ./ -name *.html`

Note those are backticks, not single quotes, around the find command.
 
Old 12-04-2008, 10:49 AM   #3
rayfordj
Member
 
Registered: Feb 2008
Location: Texas
Distribution: Fedora, RHEL, CentOS
Posts: 488

Rep: Reputation: 78
if it'll take args from stdin you could try something like this
Code:
find . -type f -iname \*.html -print | htmldoc -f output.pdf --webpage
 
Old 12-04-2008, 11:02 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
There are two basic approaches that you could use.

One is the real recursive one. The script would need to process the files into a dir one by one. The basic scheme would be:

Code:
if [ "$1" == "" ]; then
  dir=$PWD
else
  dir="$1"
fi

for file in "$dir"
do
  if [ -d file ]; then
    $0 "$dir" # launches the script again on the next dir
  else if [ ! "${file/.html/}" == "${file}" ]; then
    # if the filename without ".html" is not the same than the filename
    # then the file is an html file, there are many other ways to check this
    # Here you must insert the commands to convert $file to pdf
  else
    : # do nothing since it's not a dir, and it's not html either.
  fi
done
That's just the basic idea and might not be perfect but it should put you on the right track if you really want to do this recursively.

The other way (usually simpler and probably much less intensive in ram usage) is to just use find, with -exec. Or better, use it to feed a while loop which brings you more possibilities. For example:

Code:
cd /whatever/dir/; find . -name \*.html | while read file; do html2pdf "$file" "${file/.html/.pdf}"; done
I have no idea if html2pdf is a real tool, and if it is I have no idea of the syntax. The above snippets are meant only as a rough example on how to implement this on a bourne compatible shell.
 
Old 12-04-2008, 11:11 AM   #5
zer0x333
Member
 
Registered: Oct 2007
Posts: 31

Rep: Reputation: 16
This will do the files one by one, keeping the PDF in the same location as the source HTML:

Code:
#!/bin/bash
IFS="

"
for htmlfile in $(find . -type f -name '*.html')
do
        pdffile=$(echo $htmlfile | sed s/\.html$/\.pdf/g)
        htmldoc -f $pdffile --webpage $htmlfile
        echo Converted $htmlfile to $pdffile
done
 
  


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
run php script from command line blizunt7 Programming 11 11-06-2008 12:59 PM
Entering folders recursively to run script on files nested inside, ubuntu 7.10 aidansmoker Linux - Newbie 4 11-12-2007 12:39 AM
Script that will run a command, time it etc. kinetik Programming 7 06-20-2006 05:42 PM
Whats the command that allow you to run a script as another user? ziggie216 Linux - Software 3 11-10-2005 04:05 AM
How do I run a command in script from a particular directory? davee Linux - Newbie 1 08-07-2003 04:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:01 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
Open Source Consulting | Domain Registration