SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Links in UNIX: Hard and Symbolic Links Explained

chad avatar
11 diapositives · 272 Views · Aug 14, 2014

This guide elucidates the concept of links in UNIX, detailing both hard and symbolic links. A link acts as a pointer to a file; while hard links are direct references to the same file, symbolic links point to a pathname. Learn how to create links using the `ln` command, recognize the distinctions between hard and soft links, and understand their behaviors when files are removed. This resource is essential for anyone looking to navigate file management efficiently in a UNIX environment.

Understanding Links in UNIX: Hard and Symbolic Links Explained - slide 1
01 / 11Understanding Links in UNIX: Hard and Symbolic Links Explained
1 / 17

Télécharger la présentation

Understanding Links in UNIX: Hard and Symbolic Links Explained
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Software Tools Links

  2. Links • A link is a pointer to a file. • In fact, in UNIX all filenames are just links to a file. Most files only have one link. -rw-r--r-- 1 jbond cs 154 Feb 4 15:00 letter3 -rw-r--r-- 1 jbond cs 64 Feb 4 15:00 names drwxr-xr-x 2 jbond cs 512 Feb 4 15:00 secret/ • Additional links to a file allow the file to be shared. • The ln command creates new links. $ ln names NAMES $ ls -l total 8 -rw-r--r-- 2 jbond cs 64 Feb 6 18:36 NAMES -rw-r--r-- 1 jbond cs 154 Feb 4 15:00 letter3 -rw-r--r-- 2 jbond cs 64 Feb 4 15:00 names drwxr-xr-x 2 jbond cs 512 Feb 4 15:00 secret/

  3. Links • ln creates a new link, not a new file. The new link and the original filename are equivalent pointers to the file. • The last argument is the link destination, and can be: • A pathname of a new regular file $ ln names NAMES • A pathname of an existing directory (a link with the same basename as the original file is created in the directory) $ ln names secret • No second argument (same as giving a second argument of “.”) $ ln secret/letter1

  4. jbond NAMES letter3 names Links • A link has two pieces of information • A name • An inode number • An inode number is an index into a system table that has all the information about the file (e.g., owner, size). $ ln names NAMES system table file contents inode: 42979 user: 4501 group: 1501 address: ... 007 Golden Eye Tomorrow Never Dies

  5. Links • You can use ls -i to see if two links point to the same inode: $ ls -li total 8 42979 -rw-r--r-- 3 jbond cs 64 Feb 6 18:36 NAMES 42976 -rw-r--r-- 1 jbond cs 34 Feb 4 15:00 letter3 42979 -rw-r--r-- 3 jbond cs 64 Feb 4 15:00 names 59980 drwxr-xr-x 2 jbond cs 512 Feb 4 17:10 secret/ • So, using rm actually only removes a link. When the last link to a file is removed, the operating system actually removes the file.

  6. Symbolic Links • A symbolic link is a pointer to a pathname, not a pointer to the file itself. • ln -s original target creates a symbolic link. • A symbolic link is not equivalent to a hard link. The symbolic link has a different inode. $ ln -s names snames $ ls -li total 10 42979 -rw-r--r-- 3 jbond cs 64 Feb 6 18:36 NAMES 42976 -rw-r--r-- 1 jbond cs 34 Feb 4 15:00 letter3 42979 -rw-r--r-- 3 jbond cs 64 Feb 4 15:00 names 59980 drwxr-xr-x 2 jbond cs 512 Feb 4 17:10 secret/ 42916 lrwxrwxrwx 1 jbond cs 5 Feb 8 17:09 snames -> names • Symbolic links are sometimes called soft links, and “regular” links are sometimes called hard links.

  7. Differences Between Hard and Soft Links • You can’t make a hard link to a directory, but you can make a symbolic link to a directory. $ ln secret secrethlink ln: ‘secret’ : hardlink not allowed for directory $ ln -s secret secretslink $ ls -li total 12 42979 -rw-r--r-- 3 jbond cs 64 Feb 6 18:36 NAMES 42976 -rw-r--r-- 1 jbond cs 34 Feb 4 15:00 letter3 42979 -rw-r--r-- 3 jbond cs 64 Feb 4 15:00 names 59980 drwxr-xr-x 2 jbond cs 512 Feb 4 17:10 secret/ 42917 lrwxrwxrwx 1 jbond cs 6 Feb 8 17:21 secretslink -> secret/ 42916 lrwxrwxrwx 1 jbond cs 5 Feb 8 17:09 snames -> names $ cd secretslink $ pwd /homes/jbond/secret

  8. Differences Between Hard and Soft Links • You can also make symbolic links across file systems. $ pwd /homes/jbond/secret $ ls -l /tmp total 26 -rw-rw-r-- 1 root sys 13636 Feb 2 01:41 ps_data $ ln /tmp/ps_data ps_data ln: ps_data is on a different file system $ ln -s /tmp/ps_data ps_data $ ls -li total 4 59944 -rw-r--r-- 1 jbond cs 154 Feb 4 16:38 letter1 59597 lrwxrwxrwx 1 jbond cs 12 Feb 8 17:39 ps_data -> /tmp/ps_data • There is no way to tell how many symbolic links there are to a file.

  9. Biggest Difference Between Hard and Soft Links • The most important difference between hard and symbolic links occur when a link is removed. • For a hard link: $ echo 123 > first $ ln first second $ rm first $ cat second 123 $ echo 456 > first $ cat first 456 $ cat second 123

  10. Biggest Difference Between Hard and Soft Links • For a symbolic link: $ echo 123 > first $ ln -s first second $ rm first $ cat second cat: cannot open second $ echo 456 > first $ cat first 456 $ cat second 456

  11. Appending and Pattern Matching • We have seen input redirection (cat <file) and output redirection (cat >file). We can also append to a file using >> $ date > file $ who >> file • Simple file pattern matching • The * pattern matches any number of characters: $ ls -l letter* lists all files in the working directory that start with “letter” • The ? pattern matches any single character: $ ls -l letter? lists all files in the working directory that start with “letter” followed by exactly one character.