2

I want to format a flash drive as exFAT, but I want to make sure that I get the device right. So, I decided to check the contents of /dev/sda, where I believe the flash drive is. However, when I tried to cd into it, it gave me an error saying that it wasn't a directory. So I thought "Oh, it's a file that contains all the information about the contents of the drive." Therefore, I tried to use Vim on it. But that gave me an error saying that it wasn't a file!

Is /dev/sda a file or a directory? (Also, is there a better way to make sure that I'm using the correct device?)

2 Answers2

4

/dev/sda is formally a file because in Unix/Linux almost everything is a file. Normally it's a block special file associated with a block device. You can call /dev/sda a "device node". By "normally" I mean "by convention", because you can create /dev/sda as any other file type if you wish; still every user that has a clue will expect it to be a block special file.

A relatively common scenario where /dev/sdX turns out to be a regular file is after a user wrote something to this path without making sure it existed as a special file in the first place. Writing to a nonexistent path can create a regular file. Compare this answer of mine.

What Vim meant was your /dev/sda is not a regular file; cd told you it's not a directory. It's most likely a block special device as it should be. You can confirm this by ls -l or file. Example:

$ ls -l /dev/sda
brw-rw---- 1 root disk 8, 0 Oct 28 15:35 /dev/sda
$ # That 'b' in front indicates a block special file.
$
$ file /dev/sda
/dev/sda: block special (8/0)
$

is there a better way to make sure that I'm using the correct device?

lsblk. Also see how to detect whether a hard drive is external USB or internal.


You cannot cd to /dev/sda to see its content. The device may contain one or more filesystems but to browse a filesystem you need to mount it first. Example:

mount /dev/sda /mnt
# then browse /mnt/

This exact command will make sense if there's a filesystem on the entire device. Usually there is a partition table; then filesystems reside in partitions referred to and mounted via /dev/sda1, /dev/sda2 etc. Compare this question.

1

/dev/sda is a code name for a device. You can not browse these codenames. You can only mount them to a real folder (e.g. /media/my-usb) and then browse the mounted folder.

Dzhuneyt
  • 109