6

I know I can find the name of my flash drive by going /media/user/nameOfFlashdrive in the file manager. However, Is there a terminal command where I can enter the name of the flash drive and it will tell me where the drive is connected such as /dev/sdb1?

3 Answers 3

4

There's several commands for that actually. One can always filter out the output, using text processing tools, generally their output is small enough to read in one screenfull.

blkid

This neat command by itself , as the name suggests, shows info about block devices. With -L you can search for specific device with a label (name).

$ blkid -L DTSE9                                               
/dev/sdb1

df

This neat command is part of coreutils package, shows the block size and usage of the "device files". It shows only informations about those devices that are mounted ( in other words, linked to a folder somewhere ). For instance,

/dev/sdb5      115247656 84753976  24616332  78% /media/WINDOWS

Tells me that my windows partition on the second hard drive is linked to /media/WINDOWS partition.

udisksctl

$ udisksctl status                                             
MODEL                     REVISION  SERIAL               DEVICE
--------------------------------------------------------------------------
Radeon R7                 1.01      A22MD061520000172    sda     
TSSTcorp CDDVDW SU-208GB  TF01      S16T6YEFB00NKH       sr0 

Very convenient command that lists models and device file to which a disk is linked. In the example above my Radeon R7 SSD is linked to /dev/sda device.

If we go into more details, udisksctl info -b /dev/sda will list lots of additional info , including size and symlinks.

If we wanna go wild, udisksctl dump will produce verbose output on all the disks.

parted and fdisk

Both commands are disks utilities, used for partitioning, resizing, and many more other fun things. Both however, require use of sudo. Both output great verbose information

find

This is a more "hands on" approach. All the devices have a special device file under Linux ( remember Unix philosophy that says everything is a file ? It applies here the best ). Knowing that there are files /dev/disk/by-label we could search that directory, or we could just search /dev/disk in general. Definitely a tool that more advanced users can appreciate

$ find -L /dev/disk/by-label/ -name "DTSE9" -exec readlink -e {} \;               
/dev/sdb1

lsblk

Already covered by Jacob.

mount

$ mount | grep "DTSE9"                                                            
/dev/sdb1 on /media/xieerqi/DTSE9 type vfat (rw,nosuid,nodev,uid=1000,gid=1000,shortname=mixed,dmask=0077,utf8=1,showexec,flush,uhelper=udisks2)

Lists all mounted filesystems. Can be filtered with grep to look for specific filesystem. It's analogous to doing grep 'DISKNAME OR UUID' /proc/mounts

lshw

This command provides info about all the hardware on the system. In particular, you can view info about disks using lshw -c disk for whole device, or lshw -c volume for partitions, and you should see output with lines something like this:

logical name: /dev/sdc1
   logical name: /media/xieerqi/BA02-AF80
0
3

You can simply use:

lsblk | grep <flashdrive>

This will output in my situation, running

$ lsblk | grep Passport
└─sdb1   8:17   0   1,8T  0 part /media/jacob/My Passport1
└─sdc1   8:33   0 698,6G  0 part /media/jacob/My Passport

where you can see both the device and the mount point. As you can see, I have two usb drives named My Passport

Only get the device

$ lsblk | grep Passport | awk '{ print $1 }'
└─sdb1
└─sdc1

The same, but with a more precise output:

$ lsblk | grep Passport | awk -F'[─ ]' '{ print $2 }'
sdb1
sdc1

Or, as suggested by @kos (thanks!) even simpler, using lsblk with the -l option (which will leave out the └─ in the output, before the devices):

$ lsblk -l | grep Passport | awk '{ print $1 }'
sdb1
sdc1

Or (also as suggested by @kos), you could do without the grep command, only using lsblk and awk:

$ lsblk -l | awk '/Passport/{ print $1 }'
sdb1
sdc1

Explanation

  • lsblk will list all your mounted devices
  • grep <flashdrive> will only list the line(s), matching with your device name, looking like:

    └─sdc1   8:33   0 698,6G  0 part /media/jacob/My Passport
    
  • awk -F'[─ ]' '{ print $2 }' will split the line by two delimiters:

    (which is the second character from └─)

    and a space.

Subsequently, we can easily get the section we need.

15
  • 1
    No 1 is named "My Passport1" >:-D
    – Rinzwind
    Feb 24, 2016 at 18:52
  • @Rinzwind sharp :) Feb 24, 2016 at 18:53
  • @JacobVlijm is it possible to just obtain the name "sdb1" Feb 24, 2016 at 18:55
  • 1
    @kos I had the feeling someone could improve me here, in this foreign country, outside python thanks! I will edit it into the answer :). I am not sure about gawk, not being on Ubuntu by default. This one (my computer) is pretty much non- standard, but my wife's computer has a totally basic installation, including gawk. Feb 25, 2016 at 6:52
  • 1
    Also lsblk has -r flag, which is raw mode, used specifically when output is needed to be processed by tools like AWK Feb 25, 2016 at 7:58
1

...appending to the comprehensive answers above:

lsusb

lsusb is a utility for displaying information about USB buses in the system and the devices connected to them.

See man page. !

1
  • This is a good answer for the title of this question, but not really the question itself. That's why I upvoted this and why the other answers probably didn't include it.
    – TheGeneral
    Aug 28, 2017 at 14:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .