Finding the ID of a device with terminal commands and redirects

It so happened that I needed to find the ID of a USB drive plugged into my laptop. There are various ways to do this, but it was instructive to do so with basic Unix terminal functions.

First I plugged the drive in. Then I ran
ls /dev > u1
in the terminal. This lists (ls) the contents of the /dev (device) directory and redirects (the > symbol) the output to a text file named "u1". (In Unix, everything is a file, even devices.)

Then I disconnected the drive and ran the command
ls /dev > u0
to list all devices without the drive connected.

I compared the contents of the two files with diff, which shows the differences between two files:
diff u0 u1 > diff_u0_u1

Finally I displayed the differences with cat:
cat diff_u0_u1

60,61d59
< sdb
< sdb1
And so my USB drive was known to the system as sdb, with one partition sdb1.


Home / Unix
Copyright © 2020 Wayne Clark. All rights reserved.