Tonight I had a need to produce some output to be used in conjunction with Apache HTTPD’s mod_autoindex.
This site will eventually have frequent updates, so we wanted to use HEADER.html to show the recent changes, and making them links to make getting to those updates even easier.
Using ls -1td /path/to/files/*/* we get the output of the all content. We then pipe that through head, to essentially count the number of rows we want to show.
Finally we use sed a few times, to remove the file system paths that don’t relate to URL path, add the appropriate html tags, and using backreferences we re-use the matched string.
So what we have ended up with is :
ls -1td /foo/*/* | head -n 3 | sed -r s/[^\e]foo// | sed -r s/\(.*\)/\<li\>\<a\ href=\"\\1\"\>\\1/ | sed -r s/$/\<\\/li\>/
So lets break this down.
sed -r s/[^\e]foo//– This essentially strips the leading file system path that doesn’t relate to the webpath.
sed -r s/\(.*\)/\<li\>\<a\ href=\"\\1\"\>\\1/– This function essentialy takes the entire string, as is from the previous replacment, it then prepends the <li> and <a href> tags.
sed -r s/$/\<\\/li\>/– This bit adds the trailing </li> tag.
So we go from
# ls -1td /foo/*/*
/foo/Audio/Audio_track-1.mp3
/foo/Audio/Audio_track-2.mp3
To
<li><a href=”/Audio/Audio_track-1.mp3″>/Audio/VAudio_track-1.mp3</li>
<li><a href=”/Audio/Audio_track-2.mp3″>/Audio/VAudio_track-2.mp3</li>
This is a little more complex than what I have had to deal with in the past. Granted to some of you this may be a simple task, but us Windows folks do things the hard way. Whilst I am it, I don’t claim that this the best, or most technically accurate way of achieving the required results. It has however taught me several new regex methods, such as back references, and that has to be a good thing, right?
Just wondering but making mod_autoindex sort on last modified didn’t do the job? (ok I’m lazy).
also: /Audio/VAudio_track-2.mp3
is incorrect (x)html, you forgot to close your element.
using sed -r s/$/\\/
should add it I think. then again is suck at regex so you’re better of not listening to me
Let me have another go at it:
—
Just wondering but making mod_autoindex sort on last modified didn’t do the job? (ok I’m lazy).
also:
/Audio/VAudio_track-1.mp3
is incorrect (x)html, you forgot to close your
elementusing
sed -r s/$/\\/should add it I think.
Then again is suck at regex so you’re better of not listening to me
—
hopefully I didn’t mess up this time round.