Create and change hard links and symbolic links on Linux

Source: Internet
Author: User
Tags symlink

Overview

In this article, you learn to create and manage hard links and symbolic links. Learn:

    • Create a hard or soft link
    • Identify links and know their types
    • Understand the difference between a copy and a linked file
    • Using links to perform system administration tasks

Introduction to Links

On a storage device, a file or directory is contained in some data blocks. Information about a file is contained in an inode , which records information such as the owner, the last time the file was accessed, the size of the file, whether it is a directory, and who can read or write to it. The inode number is also known as the file sequence number, which is unique within a particular file system. A directory entry contains the name of a file or directory, and a pointer to the inode that stores the information for that file or directory.

Create a link

A hard link is a directory entry that points to the inode, and a soft link or symbolic link is a directory entry that points to the inode that provides the name of another directory entry. The exact mechanism for storing a second name may depend on the length of the file system and name. Symbolic links are also known as symlink .

You can create hard links for files only, not hard links for directories. An exception is one that contains the directory and its parent directory (. And.. Special directory entries in the directory, which are hard links that maintain the number of subdirectories. Hard links cannot be used across file systems because hard links point to the inode and the inode is unique only within a particular file system. If a file has multiple hard links, the file is deleted only if the last link to the inode is deleted, and the number of links becomes 0 o'clock.

A soft link or symlink points to another file or directory only by name, not by Inode. Soft links can be used across file system boundaries. Deleting a soft link does not delete the destination file or directory, and deleting the destination file or directory does not automatically delete any soft links.

First let's look at how to create hard and soft links. Later in this tutorial, we'll show you how to identify and use the links we create here.

Hard Links

Use the ln command to create an extra hard link to an existing file (but not a directory, even if the system sets. and.. As to a hard link).

Listing 1 shows how to create a directory containing two files and a subdirectory that contains two hard links for File1, one in the same directory, and the other in a subdirectory. We add a word to file1 and then add another word to file3 and display the contents of the link in the subdirectory to show that all the links do point to the same data.

Listing 1. Create a hard link

[Email protected] ~]$ mkdir-p lpi104-6/subdir [[email protected] ~]$ Touch lpi104-6/file1 [[email protected] ~]$ Touch L pi104-6/file2 [[email protected] ~]$ ln lpi104-6/file1 lpi104-6/file3 [[email protected] ~]$ LN lpi104-6/file1 lpi104-6/su bdir/file3sub [[email protected] ~]$ echo "Something" > lpi104-6/file1 [[email protected] ~]$ echo "Else" >> lpi1 04-6/file3 [[email protected] ~]$ cat lpi104-6/subdir/file3sub something  Else

If you try to create a cross-file system or a hard link to a directory, you get an error. Listing 2 shows that my home directory and the survey directory are on different file systems, and the attempt to create a hard link across these file systems failed, as did the hard link to create the lpi104-6 directory.

Listing 2. Hard Link creation failed

[[email protected] ~]$ DF. Filesystem     1k-blocks     used Available use% mounted on  /dev/sda5       71168700 31642752  35887712  47%/  /dev/sdb3       60326992 30677592  26578276  54%/home/ian/research  [email Protected] ~]$ mkdir-p research/lpi104-6/[[email protected] ~]$ ln lpi104-6/file1 research/lpi104-6/file3 ln:failed to Create hard link ' research/lpi104-6/file3 ' = ' lpi104-6/file1 ': Invalid cross-device link  [email protected] ~]$ Ln lpi104-6 lpidir104-6 ln: ' lpi104-6 ': Hard link isn't allowed for directory

Soft links

Use ln commands and -s options to create soft links. Soft links use file or directory names, which may be relative or absolute. If you use a relative name, you may want the current working directory to be the directory where you created the link. Otherwise, the link you create will be a relative link to another location in the file system.

Listing 3 shows the two ways to create a soft link for the file1 we just created, and how to create a soft link instead of creating two hard links that failed in Listing 2.

Listing 3. Create a soft link

[Email protected] ~]$ # Create symlink using absolute paths [[email protected] ~]$ ln-s ~/lpi104-6/file1 ~/lpi104-6/file 4 [[Email protected] ~]$ # Create symlink using relative paths [[email protected] ~]$ CD lpi104-6/[[email protected] Lpi1 04-6]$ ln-s file1 file5 [[email protected] lpi104-6]$ CD.  [Email protected] ~]$ # Create symlink across file systems [[email protected] ~]$ mkdir-p ~ian/research/lpi104-6 [[email] Protected] ~]$ ln-s ~/lpi104-6/file1 ~ian/research/lpi104-6/file4 [[email protected] ~]$ # Create symlink for directory [Email protected] ~]$ ln-s lpi104-6 lpidir104-6

As before, you can use any link or destination file name to refer to the file or directory. Listing 4 shows a few examples.

Listing 4. Use soft Links

[Email protected] ~]$ echo "Another line" >> ~ian/research/lpi104-6/file [[email protected] ~]$ # Cat A symlink [[E Mail protected] ~]$ cat lpi104-6/file5 something  else  [[email protected] ~]$ # Cat A hard link [email protected] ~]$ Cat Lpi104-6/file1 Something  else  [[email protected] ~]$ # Display directory contents using symlink [[Email PR otected] ~]$ ls lpidir104-6 file1  file2  file3  file4  file5  subdir

When creating a link, let's create a link using a relative path when the working directory is not the directory where we want to store the link. The next section describes how to accomplish this.

Listing 5. Create bad Soft links

[Email protected] ~]$ ln-s lpi104-6/file1 lpi104-6/file6

Identify links

In the previous section, you learned how to create links, but don't know how to differentiate between the links you create. Now let's discuss this topic.

Find information

On many systems, the ls command is equivalent to ls --color=auto printing different types of file system objects in different colors. If you use this option, you might use the blue-green text to display the symlink, as shown in Figure 1.

Figure 1. Use the--colors option of LS to identify links

On older configurations, you might use a dark blue background to display hard links. These colors can be dircolors configured using programs. If you want to customize the appearance of your terminal, you may want to change ls the color of some of the output. Listing 6 shows a way to get a hard-link blue background on a Fedora 22 system, and Figure 2 shows the result. Use the manual page to further understand the example.

Listing 6. Use Dircolors to set a blue background for hard links
[Email protected] ~]$ # Save a copy of dircolors defaults [[email protected] ~]$ dircolors-p > dircolors-defaults [[E Mail protected] ~]$ grep MULTI dircolors-defaults  multihardlink xx # Regular file with more than one link  [email Protected] ~]$ # change Multihardlink to blue background  [[email protected] ~]$ sed-e '/multi/s/00/00;44/' dircolors-d  Efaults > Dircolors-new  [[email protected] ~]$ grep MULTI dircolors-new multihardlink  00;44 # Regular file with More than one link [[E-  Mail protected] ~]$ # Set The new colors for the current terminal session  [[email protected ] ~]$ eval $ (dircolors dircolors-new)

Figure 2. Use a blue background to identify hard links

Colors can be handy for people who can distinguish between colors, but they are not very useful to other people and certainly not very useful for shell scripts or programs. Without color, more information is needed, such as using the ls -l information provided through a long list. In Listing 7, we explicitly disable the color output for the first example, but you can also explicitly invoke the command as we did for the other two examples /bin/ls .

Listing 7. Identify links

[[email protected] ~]$ ls--COLOR=NONE-LR lpi104-6 lpi104-6: Total  -rw-rw-r--. 3 ian Ian  9 14:1 9 file1  -rw-rw-r--. 1 ian Ian    0  9 14:19 file2  -rw-rw-r--. 3 ian Ian  9 14:19  file3 lrwxrwxrwx.  1 Ian Ian  9 14:26 file4  -/home/ian/lpi104-6/file1 lrwxrwxrwx. 1 Ian Ian    5  9 14:26 File5, File1  lrwxrwxrwx. 1 Ian Ian,  9 14:34 file6, Lpi104-6/file1  drwxrwxr-x. 2 Ian I An 4096-  9 14:19 subdir  lpi104-6/subdir: Total  4  -rw-rw-r--. 3 ian Ian,  9 14:19 file3sub  [email protected] ~]$/bin/ls-l ~ian/research/lpi104-6/file4 lrwxrwxrwx. 1 Ian Ian  9 14:27/home/ian/r Esearch/lpi104-6/file4-/home/ian/lpi104-6/file1  [[email protected] ~]$/bin/ls-l lpidir104-6 lrwxrwxrwx. 1 Ian Ian 8  9 14:27 lpidir104-6-lpi104-6

The second column of the output is a link count that shows the number of hard links for this file, so we know that file1, File3, and file3sub have multiple hard links to the objects they represent. We still don't have enough information to know that they all represent the same object. If you delete a file with a link number greater than 1, the number of links in the inode will be reduced by 1, but the file will not be deleted until the number of links becomes 0 o'clock. All other hard links of the same file will show a number of links minus 1 now.

In the first column of the output, you see that the first character is the "L" (the lowercase form of L) that represents the symbolic link. You will also see the target of the link displayed after the character. For example, File4-/home/ian/lpi104-6/file1. Another hint is that the size is the number of characters in the name of the link target. Note that for symbolic links, the number of links in the catalog list is not updated. Deleting the link does not affect the destination file. Symlink does not prevent files from being deleted. If the target file is moved or deleted, then the symlink will be destroyed. For this reason, many systems use colors in the catalog list, usually using light blue to indicate normal links, and red for broken links.

You can use ls the options of the command -i to display the number of Inode for file and directory entries. Listing 8 also gives the short and long output of our lpi104-6 directory.

Listing 8. Display Inode Information

[Email protected] ~]$ ls-i lpi104-6 1988884 file1  1988884 file3  1988892 file5 1988605  subdir  1988886 fi Le2  1988885 file4  1988891 file6  [[email protected] ~]$ ls-il lpi104-6 total  1988884-rw-rw-r--. 3 ian Ian  9 14:19 file1  1988886-rw-rw-r--. 1 ian Ian    0  9 14:19 file2  1988884-rw-rw-r-- . 3 Ian Ian  9 14:19 file3  1988885 lrwxrwxrwx. 1 Ian Ian   9 14:26 file4-  /home/ian /lpi104-6/file1  1988892 lrwxrwxrwx. 1 Ian Ian    5  9 14:26 file5, file1  1988891 lrwxrwxrwx. 1 ia N Ian  9 14:34 file6  -Lpi104-6/file1 1988605 drwxrwxr-x. 2 Ian Ian 4096  9 14:19 subdir

You can also use find a -type l find expression to search for symbolic links using commands, as shown in Listing 9.

Listing 9. Find Symlink using Find
[[email protected] ~]$ find lpi104-6 research/lpi104-6-type l lpi104-6/file4  lpi104-6/file6  lpi104-6/file5  research/lpi104-6/file4

Destruction of the Symlink

In Listing 5, we declare that we want to create a bad soft link. This is an example of the destruction of the symlink. Because hard links always point to an inode that represents a file, they are always valid. However, Symlink may have been compromised for a number of reasons, including:

    • When you create a link, the original file or target of the link does not exist (as in Listing 5).
    • The target of the link is deleted or renamed.
    • An element in the path of the target is deleted or renamed.

None of these conditions throws an error, so you need to carefully consider what might have happened when you created the symlink. Specifically, the choice of an absolute or relative path may be affected by what you want the linked object to do during the link lifetime.

If you use color output, the broken symlink may appear as red text on a black background, as in the case of file6 in Figure 1. Otherwise, you need to use ls the -H or -L option to dereference the link and provide information about the destination. -Hoption dereference the link on the command line, and the -L option dereference the sitelink that is included in the display. Listing 10 illustrates the differences in the output from these two options.

Listing 10. Dereference links using ls-h and ls-l

[Email protected] ~]$/bin/ls-lh lpidir104-6 Total 12-rw-rw-r--. 3 Ian Ian 9 14:19 File1-rw-rw-r--. 1 Ian Ian 0 9 14:19 File2-rw-rw-r--. 3 Ian Ian 9 14:19 File3 lrwxrwxrwx. 1 Ian Ian 9 14:26 file4-/home/ian/lpi104-6/file1 lrwxrwxrwx. 1 Ian Ian 5 14:26 9 File5, File1 lrwxrwxrwx. 1 Ian Ian 9 14:34 File6-Lpi104-6/file1 drwxrwxr-x. 2 Ian Ian 4096 9 14:19 subdir [[email protected] ~]$/bin/ls-ll lpidir104-6/bin/ls:cannot Access Lpidir104-6/file 6:no such file or directory total 20-rw-rw-r--. 3 Ian Ian 9 14:19 File1-rw-rw-r--. 1 Ian Ian 0 9 14:19 File2-rw-rw-r--. 3 Ian Ian 9 14:19 File3-rw-rw-r--. 3 Ian Ian 9 14:19 File4-rw-rw-r--. 3 Ian Ian 9 14:19 file5 L??????????   ? ?      ?            ? ? File6 Drwxrwxr-x. 2 Ian Ian 4096 9 14:19 SubDir

Note that the error message indicates that FILE6 does not exist, and note that the output for it contains all the "?" characters, which also indicates that the file does not exist.

For the symbolic link of the destruction, also to illustrate the last point. An attempt to read the file will fail because it does not exist. However, if you have the appropriate permissions for the target file, the attempt to write to it will be valid, as shown in Listing 11. Note that we need to create the lpi104-6/lpi104-6 before writing to the file.

Listing 11. Read and write a corrupted symlink
[email protected] ~]$ cat lpi104-6/file6 cat:lpi104-6/file6:no such file or directory  [[email protected] ~]$ echo " Testing File6 "> Lpi104-6/file6 bash:lpi104-6/file6:no such file or directory  [[email protected] ~]$ mkdir lpi104 -6/lpi104-6 [[email protected] ~]$ cat lpi104-6/file6 cat:lpi104-6/file6:no such file or directory  [[email protected ] ~]$ echo "Testing File6" > lpi104-6/file6 [[email protected] ~]$ Cat Lpi104-6/file6 testing File6  [email protect ed] ~]$ ls lpi104-6/lpi104-6 file1  [[email protected] ~]$ ls-l lpi104-6/file6 lrwxrwxrwx. 1 Ian Ian  9 14:3 4 Lpi104-6/file6-Lpi104-6/file1

Who links to me?

To determine which files are hard links to a specific inode, you can use find commands and -samefile options and a file name, or -inum an option and an inode number, as shown in Listing 12.

Listing 12. Find hard links to the same file
[[email protected] ~]$ find lpi104-6-samefile lpi104-6/file1 lpi104-6/file1  lpi104-6/file3  lpi104-6/subdir/ File3sub  [[email protected] ~]$ ls-i lpi104-6/file1 1988884 lpi104-6/file1  [[email protected] ~]$ Find lpi104-6- Inum 1988884 lpi104-6/file1  lpi104-6/file3  lpi104-6/subdir/file3sub

To find out which files are linked to a specific file through symbolic links, you can use find commands and -lname options and a file name, as shown in Listing 13. Links can use relative or absolute paths, so you might want to put a leading asterisk in the name to find all the matching results.

Listing 13. Find a symbolic link to a file or directory
[[email protected] ~]$ find lpi104-6 research/lpi104-6-lname "*file1" lpi104-6/file4 lpi104-6/file6 lpi104-6/  File5  Research/lpi104-6/file4

Copy or link

Depending on the task you want to accomplish, links are sometimes used, and sometimes it may be better to copy a file. The main difference is that the link provides multiple names for a file, and the copy creates two sets of identical data under two different names. For backup and test purposes, a copy is definitely used, and you want to experiment with a new program without risking your operational data. You can use links when you need an alias for a file (or directory), which may be used to provide a more or less path. In the next section, we'll cover some other uses of the link.

As you've seen, when you update a file, all of its links see the update information, and the copy file doesn't. You also see that symbolic links can be broken, and subsequent write operations can create new files. Please use the link carefully.


Link and System Management

Links (especially symbolic links) are often used in Linux system management. Commands often have aliases, so users do not need to know the version number of the current command, but can use a longer name to access other versions when necessary. As shown in Listing 14, the python command is python2 symlink, which python2 itself is version 2.7 of python2.7 symlink.

Listing 14. To alias a command to a specific version
[email protected] ~]$ which Python/usr/bin/python  [[email protected] ~]$ ls-l/usr/bin/python lrwxrwxrwx. 1 root Ro OT 7 14:12/usr/bin/python  -Python2 [[email protected] ~]$ ls-l/usr/bin/python2 lrwxrwxrwx. 1 root root 9 14:12/usr/bin/python2  -python2.7 [[email protected] ~]$ ls-l/usr/bin/python2.7-rwxr-xr-x. 1 root Roo T 7120 May 14:12/usr/bin/python2.7

Other uses can be useful when multiple command names use the same underlying code, such as various commands for storing and restarting the system. Sometimes, a new command name (such as genisoimage ) supersedes the old command name, but the old name (mkisofs) remains a link to the new command. Some alternative tools use links extensively, so you can choose one of several alternative tools for a command, such as java . Listing 15 shows some examples.

Listing 15. Example of a command alias
[email protected] ~]$ which halt/usr/sbin/halt [[email protected] ~]$ ls-l/usr/sbin/halt lrwxrwxrwx. 1 root root: 9 09:16/usr/sbin/halt. /bin/systemctl [[email protected] ~]$ find/usr/sbin/usr/bin-lname "*/systemctl"/usr/sbin/halt/usr/sbin/telinit/us r/sbin/shutdown/usr/sbin/runlevel/usr/sbin/poweroff/usr/sbin/reboot [[email protected] ~]$ which mkisofs/usr/bin/m kisofs [[email protected] ~]$ ls-l/usr/bin/mkisofs lrwxrwxrwx. 1 root root June 08:02/usr/bin/mkisofs-/etc/alternatives/mkisofs [[email protected] ~]$ Alternatives--displa  Y mkisofs mkisofs-status is auto. Link currently points to/usr/bin/genisoimage/usr/bin/genisoimage-priority slave Mkisofs-mkhybrid:/usr/bin/geniso Image slave Mkisofs-mkhybridman:/usr/share/man/man1/genisoimage.1.gz slave Mkisofs-mkisofsman:/usr/share/man/man1/ genisoimage.1.gz current ' best ' version is/usr/bin/genisoimage.

Library names are often also managed using symlink, whether the program is allowed to link to a generic name when getting the current version, or a 64-bit system that can run 32-bit programs. Listing 16 shows some examples. Note that some situations use absolute paths, while others use relative paths.

Listing 16. Library Links
[[email protected] ~]$ ls-l/usr/lib*/libm.so* lrwxrwxrwx. 1 root root 10:31/usr/lib64/libm.so. /.. /lib64/libm.so.6 lrwxrwxrwx. 1 root root 10:33/usr/lib64/libm.so.6 libm-2.21.so lrwxrwxrwx.  1 root root 10:35/usr/lib/libm.so.6 libm-2.21.so [[email protected] ~]$ find/usr/lib//usr/lib64/ -lname "*libstdc++*"/usr/lib/gcc/x86_64-redhat-linux/5.1.1/libstdc++.so/usr/lib/gcc/x86_64-redhat-linux/5.1.1/ 32/libstdc++.so/usr/lib/gcc/x86_64-redhat-linux/5.1.1/32/libstdc++.a/usr/lib64/libstdc++.so.6 [[email  Protected] ~]$ ls-l/usr/lib64/libstdc++.so.6 lrwxrwxrwx. 1 root root June 06:52/usr/lib64/libstdc++.so.6-libstdc++.so.6.0.21 [[email protected] ~]$ ls-l/usr/li b64/libwbclient* lrwxrwxrwx. 1 root root 1 10:37/usr/lib64/libwbclient.so.0, libwbclient.so.0.12 lrwxrwxrwx. 1 root root 1 10:37/usr/lib64/libwbclient.so.0.12-/etc/alternatives/libwbclient.so.0.12-64

Exhausted, probably this is enough, there is an interest can be more to understand! Linux is very attractive!











Create and change hard links and symbolic links on Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.