Hard link

from Wikipedia, the free encyclopedia

Hard link (also hard link , English hard link , in the German jargon and hard link ) is a term from the field of file systems in computer science . In modern file systems there is no fixed association between file names and the actual file . Rather, when creating the file - for example on a hard disk  - initially merely a number for reference on the file used (depending on the operating system inode - or file record -number called) and in a second step directory entry generated by the file name on this number refers. In the real sense, hard link refers to this link between file name and file (the latter represented by the inode or file record number ). It is interesting - and this is usually what is meant when the term hard link is used - that several hard links can refer to the same file, i.e. several directory entries or file names can exist for one and the same file.

introduction

The operating system (more precisely: the file system handler) automatically generates the first hard link when the file is created, since without it no file name would exist and the application programs would therefore have no access to the file. With the creation of a hard link to an already existing file, another name (sometimes in a different directory) is established for the file; then both names form an equivalent access to the file; it is not easy to say which name came first. This is in contrast to methods with similar objectives such as symbolic links (also called soft links ), in which there is an actual file and a link to the same. With this mechanism, a file or directory can appear in different places in the directory structure at the same time. Another hard link just goes with one more name. Other metadata belonging to the file - such as modification date or access rights - exist only once and are stored in the inode or file record. Hard links can be created in Unix and Linux systems (including macOS ) as well as in the NTFS file system under Windows .

In general, one speaks of hard links when several entries refer to the same inode or file record, the number of which identifies a file or directory within the file system. Multiple hard links to the same inode or file record are equivalent and can be renamed or deleted in any order. (When deleting, a check is made to see if the last remaining hard link is being deleted; the actual file is only then deleted.) Apart from the directory entries for the current and parent directory (" ." and " .."), hard links almost always refer to files . Since inode or file record numbers are managed within partitions , hard links are only possible within the same partition. If you want to link files on different partitions, you have to use symbolic links.

Structure (Unix)

Each directory entry consists of the file name and the assigned inode number. There can be almost any number of file paths (directory entries) for each inode , i.e. different names for the same file.

One advantage of this concept becomes apparent when removing links:

Strictly speaking, on Unix systems there is no operation to delete a file, only an unlink operation to delete links. The current number of links is saved in the inode of a file. Only when the last link to a file has been deleted, i.e. the counter in the inode has the value zero, is the file itself (inode and data cluster) released for rewriting and thus logically deleted.

What is remarkable is the behavior of hard links when a user tries to delete a file that one (or more) programs are still open: Since opening a file increases the counter by one, the attempted deletion leads to the file "disappearing" ( more precisely: the deleted link) from the directory. However, the file still exists and can be used by the program as long as it does not close the file. Only after all programs have closed the file will the hard disk space concerned be released.

To avoid consistency problems, ordinary users are not allowed to create additional hard links for directories: The links are automatically managed by a directory inode, and the unique actual directory name appears exactly once as a link in the higher-level directory. In addition, there are always the following two links in each directory: A link called " .", which represents the directory itself and consequently links to it, and the higher-level directory, represented by a link called " ..".

Application examples

Unix

The command ln can be used to create additional links for other files at any time, e.g. B. a link / home / wiki2 / same-picture to the existing file / home / wiki1 / picture :

ln /home/wiki1/picture /home/wiki2/same-picture

Other ways to create hard links are cp --linkand rsync --link-dest.

The stat command can be used to determine how many hard links point to a file. On many Unix systems, the command ls -l shows the number of hard links in the associated file in the second column.

Windows

Hard links are supported on partitions with the NTFS file system, but not on FAT or ReFS formatted partitions. Up to 1023 additional hard links are possible per file. The tools included in Windows XPfsutil (including the command extension hardlink create), the command ( included from Vista ) mklink(see the following examples) or applications from other manufacturers are suitable for creation .

In order to generate the hard link "New Linkfile.txt" with the Microsoft tool fsutil , which also refers to the file "Zieldatei.txt", enter the following command:

C:\>fsutil hardlink create "Neue Linkdatei.txt" "Zieldatei.txt"

or from Vista:

C:\>mklink /H "Neue Linkdatei.txt" "Zieldatei.txt"

In contrast to Unix (and Unix-like ones ), a hard link cannot be deleted as long as the file in question is opened by an application (i.e. a so-called filehandle is set on it).

Remarks

  • A typical operation on a computer is to open, change and save a file - for example, to change a letter in a word processor and then to save it . Depending on how the application software - here the word processor - is designed internally, either the old content in the opened file is overwritten with the changed content - or a new file with a temporary name is created, the old one is deleted and then the new file with given the old name. In accordance with the properties of hard links described above, the new content will appear for all links in the file in the first-mentioned method; in the second method, on the other hand, the deletion only removes one of the links and the new content appears in a new file with no further hard links. The other links in the original file still point to the same original file with the old content; Both versions are now available on the data carrier - an effect that may or may not be wanted.
  • Hard links are replaced by copies of the linked files during data backup , unless the backup program specifically handles this case.
  • Symbolic links (soft links) are also available for multiple references to a file .
  • A common use of hard links is to take snapshots . Instead of a complete copy of all files (complete backup), only new or changed files are saved (incremental backup) and older files are represented as hard links to files that have already been saved (backup set) . Since hard links hardly require any storage space, significantly less storage is required compared to a full backup, but all changes to a directory tree can still be reconstructed.
  • The version control system Git uses hard links when cloning local repositories. Since Git usually keeps the entire history of a project locally available, this saves storage space.
  • Under NTFS fulfill junction points ( junctions ) a function similar to hard links when directories are to be linked to different partitions or hard disks on the same computer. Junction points, however, do not allow the linking of file names or links via network drives.

See also

Individual evidence

  1. Vivek Gite: Understanding UNIX / Linux filesystem Inodes. November 10, 2005, accessed May 15, 2016 .
  2. Raymond Chen: Why is the file size reported incorrectly for files that are still being written to? MSDN , December 26, 2011, accessed May 15, 2016 .
  3. joscon, The Windows Servicing Guy: How hardwork left. Microsoft TechNet , January 6, 2011, accessed May 15, 2016 .
  4. The first [fixed / hard] reference (or [hard] link ) always points to the file that has already been created and thus accessible / accessible, see also Windows 7: Directory of hardlinks on NTFS appears to be consuming more space than it should . (English) - Server Fault , on January 15, 2012, with […] Create a file with 1024 hard links (1023 + original file) .
  5. Mike Rubel: Easy Automated Snapshot-Style Backups with Linux and Rsync. January 4, 2004, accessed May 15, 2016 .
  6. Rob Flickenger: Linux Server Hacks . O'Reilly Media, 2003, ISBN 978-0-596-00461-3 , Hack 42: Automated Snapshot-Style Incremental Backups with rsync, p. 242 ( oreilly.com ).
  7. git-clone (1) manual page. June 6, 2015, accessed May 15, 2016 .