在 Linux bash 上自动将文件复制到 USB
我正在寻找一种将文件复制到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己没有编写任何 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.
我有点明白了,它是有用的,但我唯一没有修复的是:
好处是您不需要将任何文件复制到 USB 上,只需插入,文件就会自动复制到 USB 上。希望这对其他人有帮助,或者可以改进。 (抱歉,下面的解释是西班牙语...)
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)。
sudo mount /dev/sdb2 /mnt/acopiar -t vfat -o umask=000
cp -v /home/adario/paraCopiar/loqueQuieroCopiar.zip /mnt/acopiar 是一个 USB 口的另一个口,是一个 USB 口
。
ALL=(ALL) NOPASSWD: ALL
I kind of got it, it is functional, but the only things I have not fixed is:
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...)
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).
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.
nombreUsuario ALL=(ALL) NOPASSWD: ALL