A) Types of Files in Linux:
a) Normal File: File which is created by user such as normal file, image file, binary file, compressed file. Normal or regular file indicated by “_”
b) Directory File : A file which contains another file which is indicated by “d”
c) Symbolic link file: A symbolic link is reference to another file. These are linked files to other files. They are either Directory/Regular File. The inode number for this file and its parent files are same. There are two types of link files available in Linux/Unix i.e soft and hard link.
• Soft Link: Simlink is special type of file that contains reference to another file or directory in form of absolute path or relative path. Deleting symbolic link does not remove original file.
Soft links are very similar to what we say “Shortcut” in windows, is a way to link to a file or directory. Symlinks doesn’t contain any information about the destination file or contents of the file, instead of that; it simply contains the pointer to the location of the destination file. In more technical words, in soft link, a new file is created with a new inode, which have the pointer to the inode location of the original file
• Hard Link: With Hard Link, more than one files name reference the same inode number. Files with multiple names. Hard links are same data block on the hard disk (have same inode number), while they continue to behave as independent files.
When to use Soft Link:
1. Link across file systems: If you want to link files across the file systems, you can only use symlinks /soft links.
2. Links to directory: If you want to link directories, then you must be using Soft links, as you can’t create a hard link to a directory.
When to use Hard Link:
1. Storage Space: Hard links takes very negligible amount of space, as there are no new inodes created while creating hard links. In soft links we create a file which consumes space (usually 4KB, depending upon the file system)
2. Performance: Performance will be slightly better while accessing a hard link, as you are directly accessing the disk pointer instead of going through another file.
3. Moving file location: If you move the source file to some other location on the same file system, the hard link will still work, but soft link will fail.
4. Redundancy: If you want to make sure safety of your data, you should be using hard link, as in hard link, the data is safe, until all the links to the files are deleted, instead of that in soft link, you will lose the data if the master instance of the file is deleted.
d) Named Pipe: Instead of a conventional, unnamed, shell pipeline, a named pipeline makes use of the filesystem. It is explicitly created using mknod or mkfifo, and two separate processes can access the pipe by name- one process open as reader and another as writer.
#mkfifo <file Name>. Named pipe files are indicated by ‘p”
e) Socket Files: A Socket file is used to pass information between applications for Communication purpose. Socket file is indicated by “S”
f) Device file: Device files are hardware files most of the are present under /dev directory. Devices files or special files are an interface for device driver that appears in a file system. Devices are categorized in two types
1) Character Device: or character special file provide unbuffered, and has direct access to the hardware device. These devices also called raw devices.
2) Block Device: Block special files or Block Devices provide buffered access to hardware. Block device allow you to write any sized block( Including single character or bytes)
Device nodes on Unix-like systems do not necessarily have to correspond to physical devices. Nodes that lack this correspondence form the group of pseudo-devices. They provide various functions handled by the operating system. Some of the most commonly used (character-based) pseudo-devices include:
/dev/null – accepts and discards all input; produces no output
/dev/zero – accepts and discards all input; produces a continuous stream of NUL(zero value) bytes
/dev/full – produces a continuous stream of NUL(zero value) bytes when read, and returns a “disk full” message when written to /dev/random
/dev/urandom – they produce a variable-length stream of pseudo-random numbers.
Note : In Unix like system every device has unique device number, each instance of driver and hardware component is assigned unique device number. These devices numbers are sub categorized into 1) Minor Number and Major Number
• Major Number: Each device driver is assigned a major number
• Minor Number: The Minor number is used by the driver to determine which hardware is used.
• In the Linux kernel, device numbers are currently stored in 16 bit integers. The major number component is allocated 8 bits, and the remaining 8 bits are used for the minor number. Each driver is thus allocated one of the 256 possible major numbers
• Usefull commands are mknod, mkfifo, ln, stat, file, dmsetup, udevadm, fdisk, fuser, lsof