在 Linux bash 上自动将文件复制到 USB

发布于 2024-10-03 05:49:51 字数 263 浏览 0 评论 0原文

我正在寻找一种将文件复制到 USB 驱动器然后自动插入的方法。我需要:

  • 检测何时插入新 USB
  • 检测安装点(默认为 /media/something) - 我的系统自动安装 USB 驱动器。
  • 将文件复制到其上。

我需要用 linux bash 来做到这一点。我已经搜索了一些有用的命令,如 udev、mount、udevinfo,但真的不知道如何使用它们(组合它们)来获得我需要的东西。

有什么建议吗?

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

  • Detect when a new usb is inserted
  • Detect the mount point (/media/something by default) - my system mounts usb drives automaticaly.
  • copy a file onto it.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2024-10-10 05:49:51

我自己没有编写任何 udev 规则,但是来自 Ubuntu 中的“etola”的答案论坛主题准确描述了您的用例(在设备显示时执行脚本)。然而,恕我直言,它对您插入的每台设备都很敏感,因此您可能需要多个条目。

还有一本关于编写 udev 规则的手册。

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.

ら栖息 2024-10-10 05:49:51

我有点明白了,它是有用的,但我唯一没有修复的是:

  1. USB 设备可以通过 2 个安装点访问。
  2. 你需要手动卸载设备
  3. 我找不到方法来找出USB挂载的/dev,所以它是一个常量。

好处是您不需要将任何文件复制到 USB 上,只需插入,文件就会自动复制到 USB 上。希望这对其他人有帮助,或者可以改进。 (抱歉,下面的解释是西班牙语...)

  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 dependentendo 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 queramos)。

  1. 关于“autocopiar”的内容,可以将其归档为可弹出的

sudo mount /dev/sdb2 /mnt/acopiar -t vfat -o umask=000
cp -v /home/adario/paraCopiar/loqueQuieroCopiar.zip /mnt/acopiar 是一个 USB 口的另一个口,是一个 USB 口

  1. 最后,在脚本中,不执行任何命令,将所有存档文件命名为

ALL=(ALL) NOPASSWD: ALL

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文