Special File Types   «Prev  Next»
Lesson 2 Regular files and directories
Objective Describe how UNIX treats Regular Files and Directories differently than other Operating Systems.

Regular Files versus Directories in Unix

In Unix/Linux systems, "everything is treated as a file", including regular files, directories, devices, and even processes. However, there are specific nuances regarding how Unix treats directories and files in a recursive context, especially with commands like `ls -R`.
Here's how Unix specifically handles recursive listings compared to other operating systems:
Unix's Unified File Model:In Unix/Linux systems, everything is represented as a file:
The Unix Unified File Model, it is real, it exists and it is wonderful.
  • Regular files: Documents, images, binaries.
  • Directories: Special files containing filenames and pointers (inodes).
  • Special files: Device files, sockets, symbolic links, and pipes.

Due to this design, directories themselves are just files containing references to other files or directories.
Example:
- When you perform:
ls -R

Unix traverses directories as if navigating a tree structure, recursively listing each node (directory) and its leaf nodes (files/subdirectories).
How Recursive Listing Works (`ls -R`):
When you execute:
ls -R <directory>

Unix performs the following steps internally: For the following list of 3 elements, put the elements in a HTML unordered list .
  • Lists the current directory's contents (files and subdirectories).
  • Then enters each subdirectory encountered, repeating the listing procedure.
  • This continues until all nested subdirectories are listed.

Example directory:
project/
├── file1.txt
└── src/
    ├── file2.txt
    └── subdir
        └── file3.txt

Output with `ls -R`:
directory:
subdir  file2.txt

directory/subdir:
file3.txt

Unix explicitly separates each directory’s contents in its recursive listing.
③ Comparison to Other Operating Systems:
Aspect Unix/Linux (ls -R) Windows (dir /s)
File Model Unified (everything as files/directories) Separate entities (files vs folders)
Recursive Behavior Clearly structured tree listing Similar structured listing, but more verbose
Output Clarity Compact and readable Often verbose and cluttered (timestamps, file sizes, etc.)
Special Files Treated uniformly (links, devices) Special files have different commands/tools

③ Example Scenario: Recursive Directory Traversal Here's a simple demonstration of how Unix actually implements recursion:
  • Start at the specified directory.
  • List contents (files/directories).
  • For each subdirectory encountered, repeat the listing step.

Unix handles directories seamlessly due to its unified model—treating directories like "special files" that contain references to other files/directories.
③ Advantages of Unix Recursive Listing:
  • Simple syntax (ls -R) is concise and easy to remember.
  • Clear hierarchical structure, easy to read or process programmatically.
  • Consistent output, regardless of file types or devices.

④ Special Considerations in Unix Recursive Listings:
  • Recursive listings can become lengthy in deeply nested structures, potentially overwhelming the user.
  • Combine commands with filters (grep, awk, or find) to refine listings:

  ls -R | grep '.txt$'
  

(Lists only text files recursively.)
Summary of How Unix Implements Recursive Listings:
  • Directories are treated as special files containing references to other files/directories.
  • Recursive traversal is straightforward because of Unix’s unified file structure.
  • The ls -R command takes advantage of this structure to generate concise, readable recursive listings efficiently.

This fundamental Unix philosophy simplifies file management and script automation compared to other operating systems that differentiate strongly between files and directories.


Regular Files

A regular file is anything that is not one of the special file types we will look at below. In UNIX, all regular files are stored as sequences of bytes. Unlike some operating systems, UNIX makes no distinctions between binary files and text files. In addition, file "extensions" have no significance in UNIX. All filename extensions are managed by convention. For example, most C compilers expect C source code programs to be named file.c, but the operating system does not treat .c files in any special way. Files may have multiple extensions, such as file.tar.gz (a commonly used naming convention for archive files created with the tar program and compressed with the gzip program).
  • Directories
    A directory file contains a list of the files in the directory. As we have seen, directory files are marked in ls -l listings by a d in the initial position of the permission string (for example, drwxr-xr-x). So far in this course, we have concentrated on regular files and directories, which are the two files you will deal with most often. However, thanks to the UNIX "everything is a file" philosophy, you will find a number of other things that look like files, but have some special properties.

Home and Working Directories

When a user logs in, the UNIX system puts the user in the home directory. The directory in which the user is working is called the working directory (also known as the current directory). For the C and Korn shells, the tilde (~) character can be used as the shell metacharacter for the home directory. The working directory can be substituted with . (dot). The parent of the working directory is substituted with .. (double dots).
Unix Tree
The diagram represents a typical Unix directory hierarchy with a standard structure. Here's a detailed breakdown:
- Root Directory `/`:The top-level directory in Unix/Linux systems. All files and directories branch from here.
Main Subdirectories:
  1. bin/
    • Contains executable programs (commands) used by all users, like common tools and utilities.
  2. dev/
    • Stores device files representing hardware components and system resources.
    • Example devices listed: arp, audio, dsk.
  3. home/
    • Houses personal directories for individual users (e.g., u1, u2, u3).
  4. local/
    • Typically holds software installed locally (outside of default Unix/Linux packages).
    • Example subdirectories include:
      • share/ (shared files)
      • sybase/ (related to database software)
  5. usr/
    • Contains user-related programs, libraries, documentation, and source code.
    • Common subdirectories include:
      • 4lib/ (libraries)
      • bin/ (additional user binaries)
      • local/ (locally installed software)
  6. var/
    • Contains variable data files, like logs, spool files, databases, and temporary files.
    • Common subdirectories include:
      • adm/ (administrative log files)
      • crash/ (system crash dumps and logs)
  7. home/
    • Houses personal directories for individual users, like:
    • u1/, u2/, u3/, etc.

Key Characteristics of this Structure:
  • Hierarchical and Organized:
    • Clearly defined directories make navigation intuitive and organized.
  • Functional separation:
    • Directories are grouped by purpose, separating binaries (bin), user files (home), configuration (etc), variable data (var), etc.
  • Standardized:
    • Unix adheres to a standardized file hierarchy, simplifying administration and automation.

Quick Summary Table:
Directory Purpose
/bin Essential system binaries and commands
/dev Device files
/etc System-wide configuration files
/home User-specific directories
/local Local installations of software
/tmp Temporary files
/usr Secondary hierarchy for user software, libraries, and binaries
/var Variable data such as logs and crash dumps

This clear and consistent hierarchical structure is a fundamental aspect of Unix/Linux operating systems, making administration, scripting, and automation straightforward and predictable. Unix Tree

File-Based Concepts

To gain a full picture of the internal operation of filesystems, it is necessary to understand what the user sees, why things are presented they way they are, and what the main concepts are. Users experienced in UNIX may wish to skip this module. On the other hand, users new to UNIX and those starting to program in the UNIX environment will find these concepts useful. A basic implementation of the ls program helps to reinforce the material presented and provides an introduction to file-related libraries and system calls, a topic that will be expanded upon in the next module. One peculiarity that UNIX introduced was the notion that everything in the UNIX namespace (file tree) is visible as a file and that the same operations can be applied to all file types. Thus one can open and read a directory in the same way in which a file can be opened and read. Of course, this does not always have the desired effect. For example, running the UNIX command cat on a directory will likely produce a screen full of unreadable characters. However, these and other simple concepts are one of the great strengths of UNIX. The following sections provide introductory material which describe file-based concepts and start to paint a picture of how these components fit together.

SEMrush Software 2 SEMrush Banner 2