#!/bin/bash
#                       /usr/local/bin/down_ext
# https://crystalfaeries.net/posix/bin/down_ext
# celeste:crystalfaery DOWN_EXT 2016-09-25 16:56:22+00:00
# original one-liner by David Winterbottom (codeinthehole.com)
# http://feedproxy.google.com/~r/Command-line-fu/~3/qPiFDFeIezU/download-all-mp3s-listed-in-an-html-page
# http://www.commandlinefu.com/commands/view/12986/download-all-mp3s-listed-in-an-html-page

cd	/home/downloads	# download directory CONFIGURE HERE

let RETCODE=0	# default success
if [ $# -eq 0 ]
then
	echo "Download all files of a given extension listed in an html page."	1>&2
	echo "Usage: $0 FILE_EXTENSION URL(s)" 1>&2
	exit	${RETCODE}
fi
EXTENSION=${1}
shift
case ${EXTENSION} in
-h)
	echo "Download all files of a given extension listed in an html page."	1>&2
	echo "Usage: $0 FILE_EXTENSION URL(s)" 1>&2
	;;
--help)
	echo "Download all files of a given extension listed in an html page."	1>&2
	echo "Usage: $0 FILE_EXTENSION URL(s)" 1>&2
	;;
-v)
	tail -n +4 $0 | head -n 1	1>&2
	;;
--version)
	tail -n +4 $0 | head -n 1	1>&2
	;;
*)
	mkdir -p	${EXTENSION}	|| exit	255	# subdirectory for that extension
	cd		${EXTENSION}	|| exit	254

	while [ $# -gt 0 ]
	do
		wget -r -l1 -H -t1 -nd -N -np -A.${EXTENSION} -erobots=off ${1} |& tee -a .wget.txt
		let RETEMP=$?
		if [ ${RETEMP} -gt ${RETCODE} ]
		then
			let RETCODE=${RETEMP}	# save the largest error
			echo "$0: ERROR: ${RETCODE}"	1>&2	# complain
		fi
		shift	# next URL
	done
	;;
esac
exit	${RETCODE}	# pau



syntax highlighted by Code2HTML, v. 0.9.1