What's the command for terminal to find the usb information?
Solution 1
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.
Solution 2
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
Solution 3
...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. !
Related videos on Youtube
user2669764
Updated on September 18, 2022Comments
-
user2669764 over 1 year
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
? -
Rinzwind about 8 yearsNo 1 is named "My Passport1" >:-D
-
Jacob Vlijm about 8 years@Rinzwind sharp :)
-
user2669764 about 8 years@JacobVlijm is it possible to just obtain the name "sdb1"
-
Jacob Vlijm about 8 years@user2669764 like this? (see edit)
-
Jacob Vlijm about 8 years@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 aboutgawk
, 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, includinggawk
. -
Jacob Vlijm about 8 years@kos Are you sure? my wife's computer is 14.04, pretty (totally) basic.
-
Sergiy Kolodyazhnyy about 8 yearsAlso
lsblk
has-r
flag, which is raw mode, used specifically when output is needed to be processed by tools like AWK -
Jacob Vlijm about 8 years@Serg thanks! In this case, it won't make much difference. In older scripts of mine however, it does save some code! Silly that I never looked into
man lsblk
to look for specific output format... -
kos about 8 yearsI booted the live images for 14.04.4 and 15.10 just to be completely sure...
readlink -f $(which awk)
outputs/usr/bin/mawk
in in both (don't have a 15.04 image handy, but IIRC it should be the same). Many people have GAWK installed though, probably some common packages install it. I always remember this because I remember the first failed attempts with stockawk
using GNU syntax... I think the rationale is that MAWK has been proven 8-9 times faster than GAWK at times (and probably the typical newcomer Linux / Ubuntu user won't even need to useawk
directly, in general). -
Jacob Vlijm about 8 years@kos isn't the live image a stripped version of Ubuntu?
-
kos about 8 yearsDoesn't it load the same squashfs filesystem that is copied during the installation? However what happens if you run this:
comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u)
? If you havegawk
in the results it means it has been installed manually after Ubuntu's installation (the command shows all the installed manually packages excluding those listed in/var/log/installer/initial-status.gz
) -
Jacob Vlijm about 8 years@kos had to increase the cache to get all the lines :), but no, no
gawk
. -
kos about 8 yearsWeird. Obviously I do trust you, but I'm also pretty sure GAWK syntax didn't work in 14.04 by default. I'm installing it in a VM now just to see what's going on, because I'm curious. Maybe it's installed with Ubuntu but
awk
is still symlinked to/usr/bin/mawk
by default? -
Jacob Vlijm about 8 yearsLet us continue this discussion in chat.
-
TheGeneral over 6 yearsThis 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.