A way to automatically generate a list of hyperlinks from directory tree
July 22, 2015 1:04 PM   Subscribe

I have the need to periodically crawl our FTP archive and generate HTML hyperlinks for each file in the tree. This seems like the sort of thing that would be perfect for a script to do, but I have no idea where to start. I have shell access to the server. Is there a relatively simple way to do this? It doesn't need to retain the "tree" structure, even just a pile of filenames converted to hyperlinks would be fine.

So basically, if a folder had the following files:
first.pdf
second.pdf
third.pdf

The script would generate a text file similar to this
<a href="first.pdf">first.pdf</a>
<a href="second.pdf">second.pdf</a>
<a href="third.pdf">third.pdf</a>

It does not necessarily need to recursively dig through the entire tree, but if it did that would be amazing. How to get started? Thanks!
posted by xedrik to Computers & Internet (7 answers total)
 
Can you use something like PHP Directory Lister?

(or a quick and dirty script?)
posted by Nonsteroidal Anti-Inflammatory Drug at 1:07 PM on July 22, 2015


what OS?
posted by andrewcooke at 1:10 PM on July 22, 2015


Best answer: From a bash shell on the server you can use find:

find . -type f | while read filename; do echo "<a href=\"$filename\">$filename</a>"; done

You can replace the . after find with the name of whichever directory you want to operate on. Add > myhtmlfile.html at the end to store the results in a file.
posted by pocams at 1:14 PM on July 22, 2015 [5 favorites]


What pocams said. If you want to refine it to being just pdfs, you can add the -name "*.pdf" option to find (note that it's case sensitive). Run the command in the webroot, so if the FTP and web server both serve content from /var/www/html, make sure you're in that directory first.
posted by Candleman at 1:25 PM on July 22, 2015


In most web servers, you can simply turn directory listing on and have the web server generate the page. Of course, that assumes you want to put a listing on a web site.
posted by advicepig at 2:06 PM on July 22, 2015 [1 favorite]


Response by poster: OS is Linux. Thanks, I will try the suggested scripts. :)
posted by xedrik at 3:13 PM on July 22, 2015


Response by poster: pocams, I used your suggestion and tweaked it a bit so it added a new line after each file, and it worked perfectly. Thanks much!
posted by xedrik at 7:13 PM on July 22, 2015


« Older Help me chill out   |   What jobs* involve sorting lots of junk and... Newer »
This thread is closed to new comments.