bash,搜索 USB 存储设备。输出位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15830048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:07:14  来源:igfitidea点击:

bash, search for usb storage devices. output location

linuxbashusb

提问by user2225483

I am looking for a way to list any usb connected devices or removable storage media.

我正在寻找一种方法来列出任何 USB 连接的设备或可移动存储介质。

I will be using this list for a gtk boot media writer, so a user can easily write an iso to a usb.

我将使用此列表作为 gtk 启动媒体编写器,因此用户可以轻松地将 iso 写入 USB。

This creates a perfect list of ALL partitions:

这将创建所有分区的完美列表:

ls /dev | grep "[sh]d[a-z][1-9]"

How can I get a similar looking list that is only removable media?

我怎样才能得到一个类似的列表,它只是可移动媒体?

回答by Olaf Dietsche

On my system (Ubuntu 12.04), I can get a list of USB devices and partitions with

在我的系统 (Ubuntu 12.04) 上,我可以获得 USB 设备和分区的列表

ls /dev/disk/by-path/*usb*

giving

给予

/dev/disk/by-path/pci-0000:00:02.1-usb-0:1.1:1.0-scsi-0:0:0:0  
/dev/disk/by-path/pci-0000:00:02.1-usb-0:1.1:1.0-scsi-0:0:0:0-part1

or partitions alone

或单独分区

ls /dev/disk/by-path/*usb*part*

These are symbolic links, pointing to the real device files, /dev/sddand /dev/sdd1for example.

这些符号链接,指向真实的设备文件,/dev/sdd以及/dev/sdd1例如。

I have tested this with a USB stick and an external USB hard disk only. I cannot say, whether or how this works with eSATA or Firewire disks.

我仅使用 USB 记忆棒和外部 USB 硬盘对此进行了测试。我不能说这是否或如何与 eSATA 或 Firewire 磁盘配合使用。

回答by Tarc

Based on the answer of Olaf Dietsche, I end up with the following:

根据 Olaf Dietsche 的回答,我最终得出以下结论:

devs=`ls -al /dev/disk/by-path/*usb*part* 2>/dev/null | awk '{print()}'`; for dev in $devs; do dev=${dev##*\/}; echo -n "$dev ("; echo -n `mount | grep \`echo -E ${dev}\` | awk '{print()}'`; echo ")"; done

For me the above code is showing the usb devices and where they are mounted (between parentheses). It worked on Ubuntu 13.04 and 12.04.2, but I do not know if it will work on any other system.

对我来说,上面的代码显示了 USB 设备及其安装位置(在括号之间)。它适用于 Ubuntu 13.04 和 12.04.2,但我不知道它是否适用于任何其他系统。

回答by amit

To get the mounted path of usb storage use

获取usb存储使用的挂载路径

mount|grep /media|awk '{print $3}'

mount|grep /media|awk '{print $3}'

explanation to command

命令的解释

mount will print all the mounted drives and grep will display only drives mounted in /media, (considering /media is default mount point ) this output is piped to awk which will print the mounted path of usb drive

mount 将打印所有已安装的驱动器,grep 将仅显示安装在 /media 中的驱动器,(考虑 /media 是默认安装点)此输出通过管道传输到 awk,它将打印 USB 驱动器的安装路径

回答by Jules Randolph

bootisoBASH utility just do that when called with -loption. Here is the output of a slightly modified bash snippet:

bootisoBASH 实用程序只是在使用-l选项调用时执行此操作。这是稍微修改的 bash 片段的输出:

/dev/sdd /dev/sde

Relevant snippet:

相关片段:

printUSBDevices() {
  typeset -a usbDevices
  typeset -a devices
  getDeviceType() {
    typeset deviceName=/sys/block/${1#/dev/}
    typeset deviceType=$(udevadm info --query=property --path="$deviceName" | grep -Po 'ID_BUS=\K\w+')
    echo "$deviceType"
  }
  mapfile -t devices < <(lsblk -o NAME,TYPE | grep --color=never -oP '^\K\w+(?=\s+disk$)')
  for device in "${devices[@]}" ; do
    if [ "$(getDeviceType "/dev/$device")" == "usb" ]; then
      usbDevices+=("/dev/$device")
    fi
  done
  echo "${usbDevices[@]}"
}

printUSBDevices

回答by édouard Lopez

Maybe you can use the output of lsusbcommand :

也许您可以使用lsusb命令的输出:

lsusb
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0db0:3870 Micro Star International 
Bus 002 Device 003: ID 0000:0000  
Bus 002 Device 004: ID 14cd:6116 Super Top M6116 SATA Bridge