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.

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.

  • [ -e /media/$USER/*/TheFile ] only works if exactly one drive is mounted. Would be better to loop over the mounted drives so that you can plug in multiple drives at once. – Socowi Jun 14 at 12:29
  • @Socowi - I made this precise point (that only one drive should be mounted) in my answer, but the requirement is to copy to one drive at a time, not to all mounted drives. It would be nearly impossible to identify the correct drive to receive the copy unless there is an identifiable string in the drive label, in which case the * can be modified to a mask which will identify it. I did emphasise that I was showing a principle, not providing finished code. – AFH Jun 14 at 12:42

An extension of AFH's basic idea, assuming your Linux OS mounts flash drives automatically in /media/$USER.

#! /bin/bash

src="http://asianhospital.com/?id=$1"
target="$(basename "$src")"
counter=0

copyTo() {
    sleep 0.3 # wait for drive to be fully mounted
    printf -v offset '%*s' $(( counter * 15 % ($(tput cols) - 15) ))
    echo "$offset Copy to $counter"
    cp "$src" "$1/$target"
    sync "$1"
    umount "$1"
    echo "$offset Unmounted $counter"
}

inotifywait -me create --format "%w%f" "/media/$USER" |
while IFS='' read -r drive; do
    ((++counter))
    copyTo "$drive" &
done

Usage

  • Install inotifywait if you haven't already. On Ubuntu use sudo apt install inotify-tools to do so.
  • Configure your file manager (for instance nemo or nautilus) to not open mounted drives automatically. umount may fail if a file manager window is showing the contents of the drive to be unmounted.
  • Save the script as autocopy.sh.
  • Make the script executable using chmod u+x autocopy.sh
  • Start the script using ./autocopy.sh '/path/to/file.ext'
  • Insert as many USB flash drives as you want. The script detects drives as they are plugged in, copies the file to them, and unmounts them.
  • When done, hit CtrlC to exit the script.

Output

The script prints before files are copied and after drives are unmounted. Related messages are indented at the same level.

Copy to 1
Unmounted 1
               Copy to 2
                              Copy to 3
                              Unmounted 3
               Unmounted 2
                                             Copy to 4
                                             Unmounted 4

The numbers may not be reliable. If you insert the same stick twice, it is counted twice.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.