Linux - Files and Directories
Organizing hard disk into multiple partitions
Each hard disk can be organized into multiple partitions while installing the Linux OS. Filesystems are created within partitions and are used to organize files and directories. A filesystem is a hierarchical organization of directories and files. A file is a labeled and related set of bytes stored on a disk or other media. The OS uses the file name to refer to the file and find its location on the hard disk. Files may be broadly classified as text files, binary applications/programs, and data files.
All characters except the '/' and null may be used to construct filenames. Spaces should also be avoided as spaces normally delineate the different parts of a Linux command. Files that start with a '.' or dot normally contain configuration information and are invisible when the default directory listing command ('ls') is issued.
The Linux OS will execute any file with any extension. However, text files may be protected from execution by the appropriate permission assignment. The permissions on a file determine who has access to read, write, and execute a file. Normally, the permissions for a text file do not allow anyone to execute the file. Further, before executing a file, Linux looks for a special marker that indicates that the file may be executed.
A directory is a special type of file that allows users to logically group files. A directory does not physically contain 'its' files; it is just a namespace that contains references to the files (via file names) and pointers to their locations on the disk. All the files within a directory should have unique names. Directory entries are sorted alphabetically and the proximity of entries in the directory is no indication of their actual location on the disk. Conventionally, directories are said to 'contain' files when they actually only contain references to the files. The Linux directory hierarchy is delimited using the slash ('/'). The root directory is represented by a single '/' and subdirectories append their name to this slash (/bin or /etc). Each subsequent directory is separated by a slash (e.g. /home/LinuxUser/mydocuments/txt/). The user may issue commands to navigate around the Linux directory tree. The shell keeps track of the current directory. The portion of the shell process that holds a reference to the current directory changes when a command is used to change directories.
The Linux OS treats every device (printer, tape drive etc.) including the hard disk as a device file or node. The OS gets to the hardware device via the respective device driver. Almost every computing entity is treated like a file by Linux. The hard disk, printers, and tape drives are all accessed as though they are files. To read a file 'A' from the hard disk, the OS first opens the device file that corresponds to the hard disk, the device file that corresponds to the partition, and filesystem on which file 'A' resides. Finally, 'A' is opened. Every time a user logs in, a device file that references the input from the user's keystrokes (standard in), output relayed to the user in response to his commands (standard out), and error messages generated by the system (standard error) is opened. This device file is opened for both reading and writing as the input from the user should be read and output and errors should be written to this device file
A Linux pipe (represented by '|') takes input from one command, program, or device and sends the output to another program, command, or device. Some pipes are permanent, others are temporary. Pipes are also represented as files in the Unix filesystem.