Special files in linux

Special file in linux?

As a beginner am asking how to know if file is special in linux? Is there any command show this data?

Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask.

3 Answers 3

One way is to use the file command:

This will output something like this: tty: character special

Telling you that the file «tty» is a special file of the character type. There are also block specials as well.

See: man file or https://linux.die.net/man/1/file for more on the file command.

Without defining what a special file is, I assume that a «file» that is not a file is a special file.

A directory is a special file, as a pipe or a symlink, etc. If so :

ls -l | grep ^- will only give you the «normal» file.

To find only the «special» files ls -l | grep -v ^-

By the way, everything is a file in linux.

If you mean device files you can use file utility to check, for example:

$ file /dev/sda /dev/sda: block special (8/0) $ file /dev/null /dev/null: character special (1/3) 

You can also use GNU ls with -l :

$ ls -l /dev/null crw-rw-rw- 1 root root 1, 3 Jan 20 20:54 /dev/null $ ls -l /dev/sda brw-rw---- 1 root disk 8, 0 Jan 20 20:54 /dev/sda 

As described in the documentation:

 The file type is one of the following characters: (. ) ‘b’ block special file ‘c’ character special file 
$ stat /dev/sda File: '/dev/sda' Size: 0 Blocks: 0 IO Block: 4096 block special file Device: 6h/6d Inode: 10245 Links: 1 Device type: 8,0 Access: (0660/brw-rw----) Uid: ( 0/ root) Gid: ( 6/ disk) Access: 2018-01-20 20:54:41.153354807 +0100 Modify: 2018-01-20 20:54:41.153354807 +0100 Change: 2018-01-20 20:54:41.153354807 +0100 Birth: - $ stat /dev/null File: '/dev/null' Size: 0 Blocks: 0 IO Block: 4096 character special file Device: 6h/6d Inode: 1029 Links: 1 Device type: 1,3 Access: (0666/crw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2018-01-20 20:54:41.124354808 +0100 Modify: 2018-01-20 20:54:41.124354808 +0100 Change: 2018-01-20 20:54:41.124354808 +0100 Birth: - 

Источник

Читайте также:  Установка арч линукс на виртуал бокс

Special file

Computer Hope

In a computer operating system, a special file is a type of file stored in a file system. A special file is sometimes also called a device file.

Determining file special type - Linux command line.

The purpose of a special file is to expose the device as a file in the file system. A special file provides a universal interface for hardware devices (and virtual devices created and used by the kernel), because tools for file I/O can access the device.

When data is red from or written to a special file, the operation happens immediately, and is not subject to conventional filesystem rules.

In Linux, there are two types of special files: block special file and character special file.

Block special files

A block special file acts as a direct interface to a block device. A block device is any device which performs data I/O in units of blocks.

Examples of block special files:

  • /dev/sdxn — mountedpartitions of physicalstorage devices. The letter x refers to a physical device, and the number n refers to a partition on that device. For instance, /dev/sda1 is the first partition on the first physical storage device.
  • /dev/loopn — loop devices. These are special devices which allow a file in the filesystem to be used as a block device. The file may contain an entire filesystem of its own, and be accessed as if it were a mounted partition on a physical storage device. For example, an ISO disk image file may be mounted as a loop device.

To know how big a block is on your system, run «blockdev —getbsz device» as root, e.g.:

sudo blockdev --getbsz /dev/sda1

In this example, the block size is 4096 bytes (4 kibibytes).

Читайте также:  Linux посмотреть процессы всех пользователей

Character special files

A character special file is similar to a block device, but data is written one character (eight bits, or one byte) at a time.

Examples of character special files:

  • /dev/stdin (Standard input.)
  • /dev/stdout (Standard output.)
  • /dev/stderr (Standard error.)
  • /dev/random (PRNG which may delay returning a value to acquire additional entropy.)
  • /dev/urandom (PRNG which always returns a value immediately, regardless of required entropy.)
  • /dev/null (The null device. Reading from this file always gets a null byte; writing to this file successfully does nothing.)

Linux file types

In the Linux kernel, file types are declared in the header file sys/stat.h. The type name, symbolic name, and bitmask for each Linux file type is listed below.

Type name Symbolic name Bitmask
Socket S_IFSOCK 0140000
Symbolic link S_IFLNK 0120000
Regular file S_IFREG 0100000
Block special file S_IFBLK 0060000
Directory S_IFDIR 0040000
Character special file S_IFCHR 0020000
FIFO (named pipe) S_IFIFO 0010000

How can I tell if a file is special?

Test for block special

In bash, the command «test -b file» returns an exit status of 0 if file is block special, or 1 if file is another type or does not exist.

test -b /dev/null; echo $? # character special files are not block special

Test for character special

To determine if a file is character special, use «test -c file«:

Using stat

You can also check a file’s type with stat:

File: /dev/sda1 Size: 0 Blocks: 0 IO Block: 4096 block special file Device: 6h/6d Inode: 7998 Links: 1 Device type: 8,1 Access: (0660/brw-rw----) Uid: ( 0/ root) Gid: ( 6/ disk) Access: 2018-07-08 06:41:25.540000000 -0400 Modify: 2018-07-08 06:41:25.540000000 -0400 Change: 2018-07-08 06:41:25.540000000 -0400 Birth: -
File: /dev/random Size: 0 Blocks: 0 IO Block: 4096 character special file Device: 6h/6d Inode: 6518 Links: 1 Device type: 1,8 Access: (0666/crw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2018-07-08 06:41:19.676000000 -0400 Modify: 2018-07-08 06:41:19.676000000 -0400 Change: 2018-07-08 06:41:19.676000000 -0400 Birth: -

Источник

Читайте также:  Create proxy on linux

Background

There are lots types of special files in Unix, for example symlinks, device files and proc files (under /proc ). /proc files are just normal files or even text files. But for the rest, I only know how to use them, but I don’t know their internal structure and what they are (in depth). And unfortunately, all the ways to access files can only get the object which they represent. In other words there is no way to get the internal representation.

Questions

For symlinks, there is no doubt that there is a string which store the paths of their targets. However, if there is only a string, how can they be distinguished from plaintext files? If there is a special header which is specified by filesystem drivers, what is it? Is there any convention? Can anyone tell me what the binary representation of a symlink pointing at /usr/bin/bash is? For device files (nodes), what are their binary structure and representation? According to their behaviors, they must include information about relevant interface number and drivers. But this will make their size vary greatly. Can anyone explain this at a binary level? For compatibility of symlinks and device file (I know it is impossible for a same device file to work in two different environment, but we can use our imagination to make an experiment) is the binary content of these files strongly dependent on file system type and the kernel of the operating system? For example, if I copy (not via cp command, just write identical binary content) to a different file system (like from ext4 to xfs), is this file (symlink or device file) still valid and functional? How about when copying it from a Linux machine to a BSD machine? Or are they not files, and just special records in file system header part?

Источник

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