· 8 years ago · Aug 12, 2018, 07:10 PM
1#!/bin/bash
2### Download to Dir by Type ####
3# Version 0.1 by Scott Garrett #
4# Wintervenom [(at)] gmail.com #
5################################
6# Depencencies:
7# curl (MIME type detection)
8# wget
9#
10c='curl -kLI --connect-timeout 10'
11w='wget -c -T15 -nc -E --content-disposition'
12
13shopt -s nocasematch # So extension case won't matter.
14shopt -s extglob # Enable extended globs in matching.
15
16for uri in "$@"; do
17 # Try to use file extension, first.
18 ftype=${uri##*.}
19 alt=0
20 echo 'Determining type...'
21 while [[ -z "$d" ]]; do
22 case "$ftype" in
23 @(txt|text|doc|xls|ppt|nfo|od?|htm)*|@(text|vnd)/*)
24 d=~/Documents
25 ;;
26 @(zip|tar|gz|bz2|xz|lzma|rar|Z)|*/*@(zip|tar))
27 d=~/Downloads/Archives
28 ;;
29 @(png|jpg|jpeg|tiff|gif|svg|xcf)|image/*)
30 d=~/Pictures
31 ;;
32 @(mpg|mpeg|mov|flv|mp4|avi|wmv|ogv)|video/*)
33 d=~/Videos
34 ;;
35 @(wav|mp3|flac|oga)|audio/*)
36 d=~/Music
37 ;;
38 *)
39 # If we couldn't figure out what type it was the first time
40 # around, let's try by MIME type.
41 ftype=$($c "$uri" | awk -F'(:|;) ' '/^Content-Type/{print $2}')
42 # If we've tried everything, the loop will get broken out of.
43 ((++alt == 2)) && d=~/Downloads
44 ;;
45 esac
46 done
47 mkdir -p "$d"
48 echo "Saving '$uri' to '$d'..."
49 $w -P "$d" "$uri"
50done