Use avrdude and something like
./avrdude -C ./avrdude.conf -c avrisp -b 38400 -i 10 -p m1284p -P /dev/tty.usbserial- -F -U flash:r:factory_gen6.hex:i
This is using a device known as a avrisp (thus the -c avrisp)
see [
www.nongnu.org] for full list of command line options