{"canonical_url":"https://engineer.company/glossary/file/","content_markdown":"# Computer file\n\n## What it is\n\nA computer file is what a program asks the operating system for when it wants the same bytes back\ntomorrow: it opens the file by name, reads or writes, closes it, and trusts that the next program to\nopen that name will see what was left there. Documents, photos, programs, settings and logs are all\nfiles, and on Unix-like systems so are several things that are stored nowhere at all.\n\nPOSIX, the standard behind Linux, macOS and the other Unix-like systems, defines a file as an object\nthat can be written to, read from, or both, with attributes that include its access permissions and\nits type. It then lists the types: regular files, directories, symbolic links, FIFOs, sockets, and\ncharacter and block special files, which stand for devices such as a terminal or a disk. When people\nsay file they nearly always mean the first kind, which the standard describes as a randomly\naccessible sequence of bytes on which the system imposes no structure of its own. The system neither\nknows nor cares whether those bytes are a spreadsheet, a photo or a page of a database; structure\nbelongs to the program that reads them. POSIX even declines to separate text files from binary ones,\nwhile noting that many utilities only give meaningful output on text.\n\nThe name is held outside the file. A directory is a list of entries, and each entry links a filename\nto a file. A filename is a single component that may contain any byte except a slash and the null\nbyte, up to a limit the system sets; a pathname such as `/srv/site/index.html` is the chain of names\nthe system walks to reach it. Because names live in directories, one file can have several of them,\nand the system keeps a count. When the last name is removed and no process still has the file open,\nthe space is freed.\n\nEverything else the system knows about a file is metadata, and a long listing shows most of it:\n\n```text\n$ ls -li notes.txt\n1048577 -rw-r----- 2 ana staff 4096 Oct 16 09:14 notes.txt\n```\n\nThe first number is the file serial number, unique within its file system: that number is the file,\nand the names only point at it. Then come the permission bits for the owner, the group and everyone\nelse, the number of names the file has, the owner and group, the size in bytes, and the time of last\ndata modification. POSIX keeps two more times beside that one: the last data access and the last\nchange to the file's status. A program that has opened a file holds a file descriptor, a small\ninteger private to the process. Descriptors 0, 1 and 2 are standard input, output and error, which\nis why a shell can send a program's output into a file without the program knowing.\n\n## Why it matters\n\nFiles are the oldest interface between programs that were never introduced to each other. A tool\nwritten decades ago can hand data to one written yesterday through a file, with no agreement beyond\nthe bytes and the name. Configuration, logs, exports, backups and the content of most websites all\ntravel this way, so keeping files trustworthy is most of keeping a system trustworthy.\n\nThe metadata is where the subtle trouble lives. The modification time says when the bytes were last\nwritten, which is a different fact from when what they say last changed, and the two part company\nthe moment a tool rewrites a file with identical content. Ownership is stored as a number, so the\nsame file can belong to a different person on another machine.\n\nThen there is durability. A write that returns has handed the bytes to the operating system, which\nmay keep them in a cache. POSIX says cached data reaches storage when a program asks for it with a\ncall such as `fsync`, and may get there earlier on its own, and it warns that after a crash later\noperations may be on disk while earlier ones are missing. It gives the example of a rename that\nleaves the new name created and the old one still in place.\n\nFinally, a file is one copy on one device until someone arranges otherwise. Storage fails in ways\nthat depend on the medium, and the only protection that holds across all of them is a second copy\nsomewhere else that has been read back.\n\n## What it looks like in practice\n\nA file's modification time records when its bytes were last written, which is a different moment\nfrom when its content last changed, and the site's sitemap once showed the gap: its dates put 172 of\nthe 176 pages at one identical moment of change.\n[The fix traced the date back to the file itself](/portfolio/fixed-a-sitemap-with-one-shared-timestamp/):\nthe content export writes every file afresh on each run, so whatever a page said, one sync gave the\nwhole site a single timestamp. The date now comes from version history through a\nfallback chain that tries an explicit field, then the commit history, then the file. The page\nrecords the trap in that chain: the literal field name has to be in the list, or the generator never\nreads it. A missing date leaves the field empty rather than holding a guess, and, at the recorded implementation stage, a re-sync after a\nmonth of edits touched eight files of 186.\n\nThe company's\n[restore-test implementation ends with a file being read back](/portfolio/backups-that-have-actually-been-restored/).\nIts systemd units support a database dump where configured, an encrypted, deduplicated restic\nsnapshot sent over SFTP to storage at another provider, retention pruning and an integrity\ncheck. A separate monthly workflow restores a known file to a scratch location and compares\nits bytes, so the intended test is a restore rather than an upload. The implementation record\ndated 2026-10-27 says the timers were disabled without a configured repository and records no\ncompleted live restore. Even the passphrase is a file, on purpose: the units read it from a\nprotected file rather than a systemd-parsed environment value, whose escape handling could\nturn a backslash into a different passphrase from the one used to create the repository.\n\nYears earlier the job was rescuing files, often the only copy, from failed hardware:\n[data recovery](/portfolio/recovered-data-from-sd-cards-hdds-ssds-and-raid/) across SD cards,\nspinning hard drives, SSDs, RAID arrays, external drives and Mac systems. Each of those fails and\ngives up its data differently, and a Mac filesystem has its own quirks, so the work was as much\nabout the right approach for the medium as about any single technique. The measure was the plain\none: files handed back intact.\n\n## Where it goes wrong\n\n**Every page looks changed at once.** A copy, checkout, restore or export rewrites the file and\nresets its modification time, and every system downstream that trusts that time sees a change that\nnever happened. A copy that preserves times does the opposite and hides a change that did. Take\ncontent dates from the content or from version history, and leave the file time to mean what it\nmeans.\n\n**A reader finds half a file.** A program that rewrites a file in place leaves a window in which\nanother process, or the next boot, sees part of the old version and part of the new. Write a\ntemporary file in the same directory, call `fsync` on it, then rename it over the original. POSIX\nrequires that the replaced name stays visible throughout a rename and points at either the old file\nor the new one.\n\n**A script trips over a legal name.** A filename may contain spaces, newlines and a leading dash,\nand a script that splits names on whitespace or passes them to a command unquoted will act on the\nwrong files. POSIX's own advice is to avoid a leading hyphen and to stick to letters, digits, the\nfull stop, the underscore and the hyphen for names that must travel. Quote every expansion, and end\noption parsing with `--` before a list of names.\n\n**A secret file is readable by everyone.** The permission bits on a new file come from the defaults\nof the process that created it, and those defaults usually let the group and others read. A key or\npassphrase written that way is shared with every account on the host. Create secret files with\nowner-only permissions from the start rather than tightening them afterwards.\n\n## Further reading\n\n- [NIST CSRC glossary: file](https://csrc.nist.gov/glossary/term/file) — the definitions used in\n  NIST security publications, each with the document it comes from.\n- [POSIX.1-2024 Base Definitions: Definitions](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html)\n  — the formal meaning of file, regular file, filename, pathname, directory entry and file\n  descriptor.\n- [POSIX.1-2024 Base Definitions: General Concepts](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html)\n  — permissions, the three file times, filename portability and what a crash can leave behind in the\n  cache.\n- [POSIX.1-2024: rename()](https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html) —\n  the guarantee that a name being replaced points at either the old file or the new one throughout,\n  which safe saving relies on.\n\n\u003chttps://engineer.company/glossary/file/\u003e\n","date_modified":"2026-11-12T05:34:23Z","description":"What a file is to the operating system, why its name and dates are not its content, and the faults that lose data or give it to the wrong reader.","formats":{"gemtext":"https://engineer.company/glossary/file/index.gmi","gopher":"https://engineer.company/glossary/file/gophermap","html":"https://engineer.company/glossary/file/","json":"https://engineer.company/glossary/file/index.json","markdown":"https://engineer.company/glossary/file/index.md","pdf":"https://engineer.company/glossary/file/index.pdf"},"kind":"page","language":"en","title":"What is a computer file?","url":"https://engineer.company/glossary/file/","version":1}