Ubuntu – What’s the command for terminal to find the usb information

command lineusb

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?

Best Answer

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.

Related Question