{{Short description|File system structure for locating files}} [[File:Dir command in Windows Command Prompt.png|300px|thumb|Screenshot of a Microsoft Windows command prompt window showing a directory listing.|alt=]]

In computing, a '''directory''' is a file system cataloging structure that contains references to other computer files, and possibly other directories. On many computers, directories are known as '''folders''' or '''drawers''',<ref>{{cite book |title=Using The AMIGA Workbench |date=July 1991 |publisher=Commodore-Amiga |chapter=Chapter 1: Tutorial |page=46 |quote=The path specifies the disk name, or location, and all of the drawers that lead to the specified file.}}</ref> analogous to a workbench or the traditional office filing cabinet. The name derives from books like a telephone directory that lists the phone numbers of all the people living in a certain area.

Files are organized by storing related files in the same directory. In a hierarchical file system (that is, one in which files and directories are organized in a manner that resembles a tree), a directory contained inside another directory is called a '''subdirectory'''. The terms '''parent''' and '''child''' are often used to describe the relationship between a subdirectory and the directory in which it is cataloged, the latter being the parent. The top-most directory in such a filesystem, which does not have a parent of its own, is called the '''root''' directory.

The freedesktop.org media type for directories within many Unix-like systems&nbsp;– including but not limited to systems using GNOME, KDE Plasma 5, or ROX Desktop as the desktop environment&nbsp;– is "inode/directory".<ref>{{cite web|url=https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html#idm45530160423200|title=Shared MIME-info Database|last=Leonard|first=Thomas|at=Non-regular files|date=2018-10-02|publisher=X Desktop Group|access-date=2023-03-13}}</ref> This is not an IANA registered media type.

==Overview== [[File:Files11_directory_hierarchy.svg|thumb|Diagram of a hierarchical directory tree. The root directory is here called "MFD", for Master File Directory. Usually a file can only be in one directory at a time, but here File 2 is hard linked so it appears in two directories.]] <!-- this Files-11 structure is not a tree, this image has to be replaced, or the problematic link from Dir 3 to File 2 needs to be modified and explained as a symbolic link (Unix parlance) or shortcut (Microsoft Windows parlance). --> Historically, and even on some modern embedded systems, the file systems either had no support for directories at all or had only a "flat" directory structure, meaning subdirectories were not supported; there was only a group of top-level directories, each containing files. In modern systems, a directory can contain a mix of files and subdirectories.

A reference to a location in a directory system is called a path.

In many operating systems, programs have an associated working directory in which they execute. Typically, file names accessed by the program are assumed to reside within this directory if the file names are not specified with an explicit directory name.

Some operating systems restrict a user's access only to their home directory or project directory, thus isolating their activities from all other users. In early versions of Unix, the root directory was the home directory of the root user, but modern Unix usually uses another directory such as {{mono|/root}} for this purpose.

In keeping with Unix philosophy, Unix systems treat directories as a type of file.<ref>{{cite web|title=Everything is a File |date=c. 2002 |website=Behavior Genetics Association|url=http://www.bga.org/~lessem/psyc5112/usail/concepts/filesystems/everything-is-a-file.html |url-status=dead |archive-url=https://web.archive.org/web/20120310211947/http://www.bga.org/~lessem/psyc5112/usail/concepts/filesystems/everything-is-a-file.html |archive-date=March 10, 2012 |access-date=April 30, 2021}}</ref> Caveats include not being able to write to a directory file except indirectly by creating, renaming, and removing file system objects in the directory and only being able to read from a directory file using directory-specific library routines and system calls that return records, not a byte-stream.<ref>{{cite web |url=https://man7.org/linux/man-pages/man3/readdir.3.html |title=readdir(3) — Linux manual page |author=<!--Not stated--> |date=2021-03-22 |publisher=The Linux man-pages project |access-date=November 27, 2022 |quote=}}</ref>

===Folder metaphor=== [[File:Breezeicons-apps-48-system-file-manager.svg|thumb|right|Sample folder icon (from KDE).]] The name ''folder'', presenting an analogy to the file folder used in offices, and used in a hierarchical file system design for the Electronic Recording Machine, Accounting (ERMA) Mark 1 published in 1958<ref>{{cite conference|last1=Barnard III|first1=G. A.|last2=Fein|first2=L. |title=Organization and Retrieval of Records Generated in a Large-Scale Engineering Project |date=1958|pages=59–63|doi=10.1109/AFIPS.1958.75|book-title=International Workshop on Managing Requirements Knowledge |location=Philadelphia, PA, USA|url=http://www.computer.org/csdl/proceedings/afips/1958/5053/00/50530059-abs.html}}</ref> as well as by Xerox Star,<ref>{{cite web|url=https://www.youtube.com/watch?v=Cn4vC80Pv6Q/ |archive-url=https://ghostarchive.org/varchive/youtube/20211221/Cn4vC80Pv6Q |archive-date=2021-12-21 |url-status=live|title="Xerox Star User Interface (1982)"|work=YouTube|date=28 August 2009 |access-date=19 November 2014}}{{cbignore}}</ref> is used in almost all modern operating systems' desktop environments. Folders are often depicted with icons that visually resemble physical file folders.

There is a difference between a ''directory'', which is a file system concept, and the graphical user interface metaphor that is used to represent it (a ''folder'').{{OR|date=July 2020}} For example, Microsoft Windows uses the concept of special folders to help present the contents of the computer to the user in a fairly consistent way that frees the user from having to deal with absolute directory paths, which can vary between versions of Windows, and between individual installations. Many operating systems also have the concept of "smart folders" or virtual folders that reflect the results of a file system search or other operation. These folders do not represent a directory in the file hierarchy. Many email clients allow the creation of folders to organize email. These folders have no corresponding representation in the filesystem structure.

If one is referring to a ''container of documents'', the term ''folder'' is more appropriate. {{cn|date=January 2021}} The term ''directory'' refers to the way a structured list of document files and folders are stored on the computer. The distinction can be due to the way a directory is accessed; on Unix systems, {{mono|/usr/bin/}} is usually referred to as a directory when viewed in a command line console, but if accessed through a graphical file manager, users may sometimes call it a folder.

== Lookup cache == {{More citations needed section|date=October 2025}} Operating systems that support hierarchical file systems implement a form of caching in RAM to speed up file path resolution. This mechanism reduces the overhead associated with repeatedly traversing file system hierarchies to resolve pathnames into inode references.

In Unix-like systems, this may be called the '''directory name lookup cache''' (DNLC), '''directory entry cache''', or '''dcache'''.<ref name="usenix-sun-nfs">{{cite conference |url=https://www.cs.toronto.edu/~demke/2227/S.21/Handouts/nfs-sandberg85.pdf |title=Design and Implementation of the Sun Network Filesystem |author1=Russel Sandberg |author2=David Goldberg |author3=Steve Kleiman |author4=Dan Walsh |author5=Bob Lyon |conference=Summer 1985 USENIX Conference and Exhibition |location=Portland, Oregon, USA}}</ref><ref name="dlnc">{{cite web |url=http://www.citi.umich.edu/projects/nfs-perf/results/cel/dnlc.html |title=Close-To-Open Cache Consistency in the Linux NFS Client |publisher=Center for Information Technology Integration, University of Michigan |access-date=19 November 2014}}</ref>

=== Overview === Directory lookup caches store mappings between absolute or relative path components (such as `/usr/bin`) and their corresponding inode or directory entry objects. This allows the system to bypass full path traversal for frequently accessed files or directories, dramatically improving performance for workloads with heavy metadata operations.

The cache is typically implemented using fast lookup structures such as hash tables or radix trees, and may utilize aging algorithms such as LRU for cache eviction.

=== Historical background === SunOS introduced a lookup cache as part of the implementation of NFS;<ref name="usenix-sun-nfs" /> however, it was not only used by NFS. It has since been adapted and extended in systems such as Linux, BSD, Solaris, and macOS.

=== Implementations ===

==== Windows ==== Windows uses internal path caching mechanisms as part of its NTFS and kernel object manager layers, though these are not always exposed directly.

==== BSD variants ==== BSD variants, including FreeBSD, NetBSD,<ref>{{man|9|namecache|NetBSD}}</ref> OpenBSD,<ref>{{man|9|vfs_cache|OpenBSD}}</ref> and DragonFly BSD implement directory lookup caches.

==== macOS ==== macOS integrates lookup caching into its VFS layer.<ref>{{cite web |url=https://developer.apple.com/documentation/kernel/1562195-cache_lookup |title=cache_lookup |website=Apple Developer}}</ref>

==== Linux ==== The Linux kernel's Virtual File System (VFS) layer uses a structure called the '''dcache''' to cache directory entry objects (dcache entries). Each dentry links to an inode and can represent a file or subdirectory. These dentries are organized in a tree structure that mirrors the directory hierarchy and are stored in memory as long as there is no memory pressure.

In addition to speeding up file lookups, the dcache also plays a role in maintaining consistency across mounted file systems, including complex setups with overlays or bind mounts.

=== Cache coherence in distributed file systems === For distributed file systems such as NFS or AFS, the cache must remain coherent across multiple clients. In such systems, a change made on one client must be reflected in other clients' lookup caches. To achieve this, network file systems use a variety of mechanisms:

* Attribute timeouts * Lease-based locking (e.g., NFSv4 delegations) * Callback invalidations (e.g., AFS)

Failure to ensure coherence may result in stale file metadata or incorrect path resolution, leading to application errors or data loss.

=== Performance impact === Optimized directory lookup caches can significantly reduce system call latency for file operations such as `open()`, `stat()`, and `execve()`. In benchmark tests, systems with aggressive dentry caching have shown orders-of-magnitude improvements in access times for large codebases and system boot times.

== See also == ;Lookup cache related * Inode * Page cache * VFS

;File system structure * Filesystem Hierarchy Standard * Directory structure * Root directory * Home directory * Working directory * Virtual folder * Path traversal * Mount (Unix)

;Operating system concepts * Kernel (operating system) * File folder <!-- Might redirect to "Directory (computing)" -->

;Common commands {{col-begin}} {{col-break|width=33%}} * cd (command) * pwd * mkdir * tree (command) {{col-break}} * dir (command) * pushd and popd * chroot {{col-end}}

==References== {{Reflist}}

==External links== {{Commons category|File system directories}} * [http://www.linfo.org/directory.html Definition of directory] by The Linux Information Project (LINFO)

{{Computer files}}

{{Authority control}} {{DEFAULTSORT:Directory (computing)}} Category:Computer file systems Category:File system directories Category:File system management