Automatically copy files to hundreds of USB flash drives

bashusb-flash-drive

I'm helping my girlfriend with a event where she has to load around 1,000 USB flash drives with the same file.

It is a rather small file (only a few MB) but the tedious part is actually copying the file.

Is there a way to automatically copy a file to a USB flash drive the moment it is inserted? This would make her job very easy.

My desktop OS is Windows 10, but I have access to Linux as well. I wouldn't even be opposed to writing a bash script for Linux, if that would be easier.

Best Answer

On Ubuntu 16.04 a USB flash drive is mounted in /media/$USER/LabelName, but it's possible to handle the file copying without knowing it. A rough script would be:-

#!/bin/bash
while sleep 1
do  [ -e /media/$USER/*/TheFile ] || cp TheFile /media/$USER/*/
    umount umount /media/$USER/*
    xmessage -center -timeout 3 "File copied - change disc"
done

This is just to show the principle. One thing you need to do is make sure there is nothing mounted in /media/$USER/ before you start (unless you have already mounted the first drive). You can replace TheFile by a parameter or a preset environment variable, but make sure it's in your current directory, so that there's no path, otherwise the check for its existence on the drive already will fail.

You can probably do all of this on an Ubuntu Live disc without installing, but you'll need to check where the pen-drives are mounted and modify the script if necessary; also I'm not sure if xmessage is included on a Live disc, so you may need to install it, unless you use another method of indicating that the drive needs changing.