Hot answers tagged

79

/dev/zero is an example of a "special file" — particularly, a "device node". Normally these get created by the distro installation process, but you can totally create them yourself if you want to. If you ask ls about /dev/zero: # ls -l /dev/zero crw-rw-rw- 1 root root 1, 5 Nov 5 09:34 /dev/zero The "c" at the start tells you that this is a ...


27

Most of the /dev entries are block device inodes or character device inodes. One previous answer has many details about that, which I am not going to repeat. But /dev/tcp which is mentioned in your question is not explained by any of the existing answers. /dev/tcp and /dev/udp are different from most other /dev entries. The block and character devices are ...


23

So there are basically two different types of thing here: Normal filesystems, which hold files in directories with data and metadata, in the familiar manner (including soft links, hard links, and so on). These are often, but not always, backed by a block device for persistent storage (a tmpfs lives in RAM only, but is otherwise identical to a normal ...


18

The following bash code is set to work with the byte being representred in binary. However you can easily change it to handle ocatal, decimal or hex by simply changing the radix r value of 2 to 8, 10 or 16 respectively and setting b= accordingly. r=2; b=01111110 printf -vo '\\%o' "$(($r#$b))"; </dev/zero tr '\0' "$o" EDIT - It does handle the full ...


18

You cannot easily do that. You might consider writing your own kernel module providing such a device. I don't recommend that. You could write a tiny C program writing an infinite stream of same bytes on some pipe (or on stdout) or FIFO. You could use tr(1) to read from /dev/zero and translate every 0 byte to somethng else. You could use perhaps yes(1), ...


18

Yes, both accept and discard all input, but their output is not the same: /dev/null produces no output. /dev/zero produces a continuous stream of NULL (zero value) bytes. You can see the difference by executing cat /dev/null and cat /dev/zero. Try cat /dev/null > file and you will find an empty file. Now try cat /dev/zero > file, while ...


16

In addition of device nodes explained in other answers (created with mknod(2) or supplied by some devfs), notably the one from Sepahrad Salour, Linux has other "magical" files provided by special virtual file systems, in particular in /proc/ (see proc(5), read about procfs) and in /sys/ (read about sysfs). These pseudo files (which appear -e.g. to stat(2)- ...


16

Here's a file listing of /dev/sda1 on my nearly up-to-date Arch Linux server: % ls -li /dev/sda1 1294 brw-rw---- 1 root disk 8, 1 Nov 9 13:26 /dev/sda1 So the directory entry in /dev/ for sda has an inode number, 1294. It's a real file on disk. Look at where the file size usually appears. "8, 1" appears instead. This is a major and minor device number. ...


13

Well, if you literally want to achieve this, you can use a LD_PRELOAD hook. The basic idea is to rewrite a function from the C library and use it instead of the normal one. Here is a simple example where we override the read() function to XOR the output buffer with 0x42. #define _GNU_SOURCE #include <string.h> #include <errno.h> #include ...


11

In terms of speed, the fastest I found was: $ PERLIO=:unix perl -e '$s="\1" x 65536; for(;;){print $s}' | pv -a > /dev/null [4.02GiB/s] For comparison: $ tr '\0' '\1' < /dev/zero | pv -a > /dev/null [ 765MiB/s] $ busybox tr '\0' '\1' < /dev/zero | pv -a > /dev/null [ 399MiB/s] $ yes $'\1' | tr -d '\n' | pv -a > /dev/null [26.7MiB/s] ...


11

They're called device nodes, and are created either manually with mknod or automatically by udev. They are typically file-like interfaces to character or block devices with drivers in the kernel - e.g. disks are block devices, ttys and serial ports etc are character devices. There are other "special" file types too, including named pipes and fifos and ...


7

/dev/tty is the controlling tty of the current process, for any process that actually opens this special file. It isn’t necessarily a virtual console device (/dev/ttyn), and can be a pty, a serial port, etc. If the controlling tty isn’t a virtual console, then the process has not to interact with console devices even if its pseudotty is actually implemented ...


7

It's kind of pointless to try and bitmask/xor zero bytes, isn't it? Taking a byte and xoring it with zero is a no-op. Just create a loop that gives you the bytes you want and put it behind a pipe or named pipe. It'll behave pretty much the same as a character device (won't waste CPU cycles when idle): mkfifo pipe while : ; do echo -n "a"; done > pipe ...


7

No, /dev/raw is different from the conventional /dev/sda block device. According to The Linux 2.4 SCSI subsystem HOWTO: Chapter 11. Raw devices: A raw device can be bound to an existing block device (e.g. a disk) and be used to perform "raw" IO with that existing block device. Such "raw" IO bypasses the caching that is normally associated with block ...


7

As other users have already explained in great detail, special files require code to back them up. However, nobody seems to have mentioned that Linux provides several ways to write that code in userspace: A. FUSE (Filesystem in USErspace) allows you to write something like /proc without risk of crashing the kernel and do it in a language/runtime of your ...


7

If you could call mknod arbitrarily, then you could create device files owned and accessible by you for any device. The device files give you unlimited access to the corresponding devices; therefore, any user could access devices arbitrarily. For instance, suppose /dev/sda1 holds a file system to which you have no access. (Say, it is mounted to /secret). ...


7

For all the tmpfs mounts, "Avail" is an artificial limit. The default size for tmpfs mounts is half your RAM. It can be adjusted at mount time. (man mount, scroll to tmpfs). The mounts don't share the same space, in that if you filled the /dev/shm mount, /dev would not show any more "Used", and it would not necessarily stop you from writing data to /dev ...


6

The book Linux Device Drivers (highly recommended) explains this in detail, and even has you create a kernel module that does this as an example, but in a nutshell, each device driver has specific functions that get called when a file is opened, closed, read, written, etc. The "special" files just do something special inside those functions, instead of ...


5

Historical reasons. Originally, before devfs existed, these device files were created by hand or by a script called MAKEDEV. This is also why many drivers have a fixed device number assignment; because the device numbers had to be known so that the device files would work properly. There aren't really any common use cases for the mknod command on modern ...


5

Read zeros, translate each zero to your pattern! We read zero bytes out of /dev/zero, and use tr to apply a bit mask to each of the bytes by translating each zero byte: $ </dev/zero tr '\000' '\176' | head -c 10 ~~~~~~~~~~$ Octal 176 is the ascii code of ~, so we get 10 ~. (The $ at the end of the output indicates in my shell that there was no line ...


5

At the end of the day they are all files for Unix, that´s the beauty of the abstraction. The way the files are handled by the kernel, now that is a diferent story. /proc and nowadays /dev and /run (aka /var/run) are virtual filesystems in RAM. /proc is an interface/windows to kernel variables and structures. I recommend reading The Linux Kernel ...


5

The drive names are (on a typical Linux system) decided by udev.How it decides which hardware maps to which block special file is an implementation detail that will depend your udev configuration, kernel configuration, module setup, and many other things, too (including plain luck). Typically udev will assign the first letter to whichever device it detects ...


4

A loop device is a particular type of block device, managed by the loop device driver. A loop device is a block device whose content is stored in a file, similar to the way a SCSI disk device is a block device whose content is stored on a SCSI disk, a USB storage device is a block device whose content is stored on a USB storage peripheral, etc. Linux has a ...


4

Libncurses was recently updated from /usr/lib32/libncursesw.so.5 to /usr/lib32/libncursesw.so.6. There would have been a matching bash update, but you don't have it. In fact, I just found this in my own pacman.log: [2015-09-19 23:45] [ALPM] upgraded ncurses (5.9-7 -> 6.0-3) [2015-09-19 23:45] [ALPM] upgraded readline (6.3.008-1 -> 6.3.008-3) ...


4

In addition to @RuiFRibeiro's and @BruceEdiger's answers, the distinction you make is not exactly the distinction the kernel makes. Actually, you have various kind of files: regular files, directories, symbolic links, devices, sockets (and I always forget a few so I won't try to make a full list). You can have the information on the type of a file with ls: ...


4

After the regular, FIFO and socket file types, mknod can also create device files. These are used to access devices. Granting access to devices is considered a privileged operation. Generally, we don't want to create arbitrary device nodes and make them accessible to regular users. That would be Bad. [Aside: Typically device access is granted by ...


4

With mknod, you create device-special files that allow raw access to the hardware. That is, the kernel looks at the device-special file's permissions to decide whether a given user is allowed raw acess to hardware, not to anything in configuration or some such. E.g., on Debian, devices related to optical drives are created with 0660 permission bits, user ...


4

Unix/Linux systems are multi-user. Anyone who has read access to a raw storage device can read all the files on it, regardless of who owns the files and their permissions (modulo the filesystem being encrypted, of course). So traditionally the raw devices have only allowed access to root and to members of an administrative group such as operator or disk, and ...


4

Most filesystems have unique UUIDs and have labels which you can set to a distinctive values. These allow you to refer to the volume containing the filesystem through /dev/disk/by-uuid or /dev/disk/by-label. Other types of volumes (RAID, LVM, etc.) generally have a name as well. RAID and LVM volumes are assembled based on unique identifiers in the physical ...


3

It is not --special that "should" sync the devices, it is the --devices indirect switch. For that the man page says: --devices This option causes rsync to transfer character and block device files to the remote system to recreate these devices. This option has no effect if the receiving rsync is not run as the ...



Only top voted, non community-wiki answers of a minimum length are eligible