Linux do all devices have device files

How do I find on which physical device a folder is located?

Specifically: I did sudo mkdir /work , and would like to verify it indeed sits on my harddrive and not mapped to some other drive. How do I check where this folder is physically located?

4 Answers 4

The df(1) command will tell you the device that a file or directory is on:

The first field has the device that the file or directory is on.

$ df /root Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 1043289 194300 795977 20% / 

If the device is a logical volume, you will need to determine which block device(s) the logical volume is on. For this, you can use the lvs(8) command:

# df /usr Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/orthanc-usr 8256952 4578000 3259524 59% /usr # lvs -o +devices /dev/mapper/orthanc-usr LV VG Attr LSize Origin Snap% Move Log Copy% Convert Devices usr orthanc -wi-ao 8.00g /dev/sda3(0) 

The last column tells you that the logical volume usr in the volume group orthanc ( /dev/mapper/orthanc-usr ) is on the device /dev/sda3 . Since a volume group can span multiple physical volumes, you may find that you have multiple devices listed.

Another type of logical block device is a md (Multiple Devices, and used to be called meta-disk I think) device, such as /dev/md2 . To look at the components of a md device, you can use mdadm —detail or look in /proc/mdstat

# df /srv Filesystem 1K-blocks Used Available Use% Mounted on /dev/md2 956626436 199340344 757286092 21% /srv # mdadm --detail /dev/md2 . details elided. Number Major Minor RaidDevice State 0 8 3 0 active sync /dev/sda3 1 8 19 1 active sync /dev/sdb3 

You can see that /dev/md2 is on the /dev/sda3 and /dev/sdb3 devices.

There are other methods that block devices can be nested (fuse, loopback filesystems) that will have their own methods for determining the underlying block device, and you can even nest multiple layers so you have to work your way down. You’ll have to take each case as it comes.

Источник

What does «all devices are files» mean?

I heard someone say that in Linux «all devices are files.» What does that mean? My understanding of a file is that it is a logical location on disk to which bytes are written and from which bytes are read. A device to my knowledge is just any physical piece of hardware. somewhat unrelated to files. Can someone explain this statement to me?

Читайте также:  Узнать свой ip linux команда

2 Answers 2

Linux treats every device as if it were a file. That is, you interact with a device programmatically in exactly the same way you’d interact with a file:

  • You specify the device via a path, usually under the /dev directory.
  • You begin by «opening» the device, just as you’d open a file, which gives you a file descriptor.
  • You can perform ioctl (input/output control) operations on the file descriptor.
  • You can send and/or retrieve data by writing and/or reading the file descriptor.
  • You «close» the device when you’re finished using it.

It means that the actual details of a device are abstracted away, such that an application can treat it as a file for IO purposes.

E.g. a serial port may be implemented in hardware in many different ways, but the operating system hides that so that an application can read and write to and from the device exactly as if it were reading and writing a file.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Find all storage devices attached to a Linux machine [closed]

I have a need to find all of the writable storage devices attached to a given machine, whether or not they are mounted. The dopey way to do this would be to try every entry in /dev that corresponds to a writable devices ( hd* and sd* ). Is there a better solution, or should I stick with this one?

Since you talk about them maybe being mounted, I guess you want a list of partitions, not just of disks?

partitions are fine, too, yes 🙂 .. if I know the partitions, I’ll also know what physical devices are available

@warren — oh no. They still should be closed. As you as the post owner want this moved you should flag it for moderator attention. The fact that you want it moved will carry some weight.

7 Answers 7

/proc/partitions will list all the block devices and partitions that the system recognizes. You can then try using file -s to determine what kind of filesystem is present on the partition, if any.

There’s kind of a limit on what you can do from a shell. Most of the other suggestions that are higher rated either don’t work from a shell, won’t work unless dbus is running, or will list devices that aren’t actually present/configured. This is just faster than checking all the /dev devices.

Читайте также:  Playmemories home for linux

You can always do fdisk -l which seems to work pretty well, even on strange setups such as EC2 xvda devices.

Here is a dump for a m1.large instance:

root@ip-10-126-247-82:~# fdisk -l Disk /dev/xvda1: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/xvda1 doesn't contain a valid partition table Disk /dev/xvda2: 365.0 GB, 365041287168 bytes 255 heads, 63 sectors/track, 44380 cylinders, total 712971264 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/xvda2 doesn't contain a valid partition table Disk /dev/xvda3: 939 MB, 939524096 bytes 255 heads, 63 sectors/track, 114 cylinders, total 1835008 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/xvda3 doesn't contain a valid partition table 
root@ip-10-126-247-82:~# mount /dev/xvda1 on / type ext4 (rw) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) fusectl on /sys/fs/fuse/connections type fusectl (rw) none on /sys/kernel/debug type debugfs (rw) none on /sys/kernel/security type securityfs (rw) udev on /dev type devtmpfs (rw,mode=0755) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620) tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755) none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880) none on /run/shm type tmpfs (rw,nosuid,nodev) /dev/xvda2 on /mnt type ext3 (rw) 
root@ip-10-126-247-82:~# cat /proc/partitions major minor #blocks name 202 1 10485760 xvda1 202 2 356485632 xvda2 202 3 917504 xvda3 

Side Note

How fdisk -l works is something I would love to know myself.

Источник

How to List USB Devices in Linux

Nowadays, many computer peripherals such as webcams, mice, scanners, printers, hard drives, USB (Pendrive) now come as USB devices. Once these devices are connected to the Desktop or server it’s important to know the device name or device path. This helps to identify USB devices for the tasks such as formatting.

In Linux, all device files are stored in /dev directory and must be available to the OS during the system boot.

In this guide, we will show you the various ways to list USB devices on Linux. Most commands mentioned here should work on all Linux distributions.

List USB Device Names Using df Command

The df command is a useful command that can help list all mounted volumes, including your USB drives.

Once a USB device is plugged into a Linux system, especially for Desktop, it is automatically mounted in the /media partition and becomes ready for use.

Читайте также:  Команды терминала linux шпаргалка

df command check plugged in USB devices

From the output above, I have 1 USB drive /dev/sdb with 2 partitions /dev/sdb1 and /dev/sdb2

List USB Device Name using lsblk Command

Lsblk command is used to list all block devices on a Linux system. From the list, you can filter USB devices using the grep command.

lsblk command check plugged in USB devices

To retrieve additional information such as the UUID, manufacturer and filesystem type, use the blkid command as shown.

blkid command output retrieve information on USB block devices

List USB Device Names Using fdisk Command

You can use the good old fdisk command that is used for partitioning volumes to list all the partitions on the Linux system, including the USB drives.

The command will display detailed information about your USB volume including the partitions, size of the volume, sectors, and the filesystem type.

fdisk command list USB devices

List USB Devices Details using lsusb Command

The lsusb command, also known as the “List USB” command, is used in Linux to list all the USB devices attached to the system.

lsusb command

The output above displays the Bus ID, Device ID, USB ID, and the vendor or manufacturer of the USB devices

The lsusb command simply lists the connected devices and does not provide further information about the usb devices.

For more information about the attached USB devices use the dmesg command. The dmesg command also known as “driver message” or “display the message” is used for examining the boot messages. Additionally, it is used for debugging hardware-related issues and printing messages generated by device drivers.

You can use the dmesg command and grep to narrow down to USB devices.

dmesg command

Also, you can pipe the output of dmesg command to less for easier scrolling.

On the output, you can search for a specific string by pressing the forward slash key ( / ) on your keyboard followed by the name or Device ID of the USB device.

In my case, I’m searching for more information regarding a USB device called SanDisk, which is actually my removable pen drive.

Search for a USB drive

List USB controllers and devices using usb-devices

The usb-devices command is a shell script that allows you to list all the USB controllers and the USB devices connected to your PC. It prints out details of the USB device such as the manufacturer, product name, serial number, and so much more. Here’s the output of the command:

usb-devices command to list usb devices

Conclusion

In this guide, we have demonstrated different ways to list the USB devices attached to the Linux system.

Which is your favorite command to list USB? Please provide your suggestions and feedback in the comment section.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

Оцените статью
Adblock
detector