Skip to content

Instantly share code, notes, and snippets.

@oneohthree
Last active July 12, 2017 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oneohthree/fed9906a772861244c5ebe1f0fea62ef to your computer and use it in GitHub Desktop.
Save oneohthree/fed9906a772861244c5ebe1f0fea62ef to your computer and use it in GitHub Desktop.
bash-feed-gen generates a feed file from new files arriving to a directory. This would be useful for people who mantain file servers and want their users to be notify of new files.
#!/bin/bash
FILEDIR="/path/to/files/root/directory"
FEEDDIR="/path/to/feed/root/directory"
BASEURL="http://domain.tld"
TITLE="TITLE"
DESCRIPTION="DESCRIPTION"
ENTRIES="10"
if [[ ! -d $FILEDIR ]]
then
echo "$FILEDIR does not exist"
exit 1
fi
if [[ ! -d $FEEDDIR ]]
then
echo "$FEEDDIR does not exist"
exit 1
fi
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">
<channel>
<title>$TITLE</title>
<description>$DESCRIPTION</description>
<link>$BASEURL/feed.xml</link>
<atom:link href=\"$BASEURL/feed.xml\" rel=\"self\" type=\"application/rss+xml\" />
<pubDate>$(date -R)</pubDate>" > $FEEDDIR/feed.xml
cd $FILEDIR
i=0
while (( i < ENTRIES )) && IFS=$'\t' read -rd $'\0' TS FILE FILESIZE FILENAME
do
echo " <item>
<title>Available: $FILENAME ($FILESIZE)</title>
<description>Size: $FILESIZE</description>
<link>$BASEURL/$FILE</link>
</item>" >> $FEEDDIR/feed.xml
(( i++ ))
done < <( find * -type f -printf '%T+\t%p\t%s\t%f\0' | sort -rz )
echo " </channel>
</rss>" >> $FEEDDIR/feed.xml
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment