Last Updated: February 25, 2016
·
552
· quoll

CLI Downloading on Mac

I've often found files on my system (particularly papers) whose provenance is uncertain. Safari has attempted to address this issue by adding metadata to every file it downloads. You can see this info by executing "mdls -name kMDItemWhereFroms file_name" on the downloaded file.

Unfortunately, this doesn't work when using wget or curl to get a file. To duplicate this, you can use the 'xattr' utility. This utility is limited, in that it cannot set a metadata value to a list, so I use the slightly different key of kMDItemWhereFrom (no "s").

Using a little bit of sed to get the filename, I use the following script in place of wget:

#!/bin/sh

for i in $*
do
    if [  -z `echo $i | grep '^-'` ]; then
        export urls="$urls $i"
    else
        export switches="$switches $i"
    fi
done

for u in $urls
do
    export fx=`echo $u | sed 's#^.*/##'`
    export file=`echo $fx | sed 's/\?.*//
                                 s/#.*//'`
    wget $switches $u
    if [ $file != $fx ]
    then
        mv "$fx" "$file"
    fi

    if [ ! -e "$file" ]
    then
        echo "Unable to find file: \"$file\" expected from url: $u"
    else
        xattr -w 'com.apple.metadata:kMDItemWhereFrom' "$u" "$file"
    fi
done