adario
adario

Reputation: 11

Copy a file to a usb automatically on Linux bash

I'm looking for a way to copy a file to usb drive then inserted, automaticaly. I need to:

I need to do this with linux bash. I already searched for some useful commands as udev, mount, udevinfo but really don't know how to used them (combine them) to get what I need.

Any suggestions?

Upvotes: 0

Views: 3780

Answers (2)

adario
adario

Reputation: 1

I kind of got it, it is functional, but the only things I have not fixed is:

  1. The usb-device can be accesed through 2 mount points.
  2. You need to umount the device manually
  3. I could not find a way to find out the /dev where the usb is mounted, so it is a constant.

the good thing is that you don't need to copy any file onto the usb, just plug in and the file will be copied onto it automatically. Hope this helps someone else, or can be improved. (sorry the explanation below is in spanish...)

  1. Cree una regla de udev llamada 81-audo-copy.rules con el siguiente código:

KERNEL=="sdb2", RUN+="/home/adario/paraCopiar/autocopiar"

Lo que hace es que cuando se inserta la memoria usb ejecuta el script de la ubicacion (el código 81 es importante, ya que dependiendo de lo que pongamos se ejecuta antes o despues de las demas reglas, o no se ejecuta. El resto del nombre puede ser lo que querramos).

  1. acá el contenido de "autocopiar", el archivo debe ser ejecutable

sudo mount /dev/sdb2 /mnt/acopiar -t vfat -o umask=000 cp -v /home/adario/paraCopiar/loqueQuieroCopiar.zip /mnt/acopiar

Lo que hace es asignar otro punto de montaje al usb que ya se ha montado, èsto para que sepamos el nombre de la ubicación.

  1. Por ultimo, para que el script no nos pida contraseña cada vez que se ejecute, agregamos al archivo sudoers

nombreUsuario ALL=(ALL) NOPASSWD: ALL

Upvotes: 0

lothar
lothar

Reputation: 20229

I have not written any udev rules myself, but the answer from "etola" in this Ubuntu forum thread describes exactly your use case (execute a script when a device shows up). However it's IMHO sensitive to each device that you plug in, so you may need more than one entry.

There's also a manual about writing udev rules.

Upvotes: 1

Related Questions