6

I'm new to Linux. When I try to create a directory in the /dev folder it's happening smoothly but after reboot all the created folders have disappeared.

I tried to create a folder in another folder, like /etc. There, created folders do not disappear. I tried it as a normal user and as a root user but the same thing happens.

How to create a directory under /dev that stays there permanently?

Edward
  • 2,509
  • 4
    "Everywhere directories are not disappearing." /proc and /sys work just like /dev (aren't real physical storage), it's just that they won't let you create any directories there. – val - disappointed in SE Dec 25 '20 at 16:06
  • 3
    @val: To be fair, back in the old days, /dev was usually just a directory that was part of the root filesystem, and what the OP was doing would just work. Any (Unix) filesystem can contain block-device or char-device file types, just like named pipes or symlinks or other non-regular file types. /dev is not as special as /proc and /sys are: you can create your own dirs in it. On some systems, it's just a normal tmpfs mount that user-space software (udev) uses mknod to populate with device files. (devtmpfs lets the kernel create device nodes there for you, without udev) – Peter Cordes Dec 26 '20 at 02:35
  • Anyway, mount or df /dev will clearly show that /dev is a special mount, so your overall point is certainly fair. – Peter Cordes Dec 26 '20 at 02:36

1 Answers1

27

Yes, you can create files and/or directories under /dev/. But you can't expect them to still be there after a reboot.

Here is why: the dev filesystem is responsible for device access. It's not a block filesystem (with underlying "real" storage) but a memory-based filesystem. As it exists only in RAM everything under /dev/ is erased upon shutdown and recreated on boot.

The population of /dev/ can be done in three ways:

  • statically (nowadays very uncommon)
  • on the userspace level by using the udev software
  • on the the kernel level using devtmpfs.

Here are some links to excellent posts detailing this:

Edward
  • 2,509
  • 22
    Shorter version: /dev is special; if you don't know how to create persistent files and directories in there, you really shouldn't be doing so to begin with. – Shadur-don't-feed-the-AI Dec 25 '20 at 10:24
  • 3
    On modern Linux systems the kernel manages /dev because it's mounted as devtmpfs. – iBug Dec 26 '20 at 09:08
  • @iBug yes, that's a very good addition, I edited my answer to properly reflect that – Edward Dec 26 '20 at 09:18
  • 2
    On some older Unix (not Linux) systems, /dev was a real directory (not tmpfs), but it's contents was re-generated on each reboot by a special script/tool run on boot that was detecting hardware and fill in /dev appropriately. – raj Jan 20 '21 at 11:40