How does ln command work




















As you'd have understood by now, the ln command lets you make links between files. Following is the syntax or rather different syntax available for this tool:. But before that, it's good you get a understanding of what's the difference between hard links and soft links. That's pretty straightforward - all you have to do is to use the ln command in the following way:.

By default, ln won't let you create a link if a file of the same name already exists in the destination directory. However, if you want, you can make ln override this behavior by using the -f command line option.

Note : You can use the -i command line option if you want to make all this deletion process interactive. If you don't want ln to delete existing files of same name, you can make it create backup of these files.

A normal hard link is a new directory entry that refers to the same file, either in the directory that currently contains the file or in a different directory. The result is a new path name that refers to the file. You can access the file using either the old path name or the new one; both path names are of equal importance.

If you rm either name, the other one remains and the file contents are still available under that name. The contents of the file do not disappear until you remove the last link. A file may have any number of links to it. Thus you can establish any number of different path names for any file. A symbolic link is similar to a normal hard link but it can only be created for directories.

See the description of the -s option below for more information on symbolic links. In the first form given in the synopsis, new becomes a new path name for the file old. In the second form, ln creates entries for all the old files under the directory dir. This has several potential benefits. For one thing, symbolic links also called "symlinks" for short can link to directories.

Also, symbolic links can cross file system boundaries, so a symbolic link to data on one drive or partition can exist on another drive or partition. You should also be aware that, unlike hard links, removing the file or directory that a symlink points to breaks the link.

So if we create file1. This error message might be confusing at first, because file2. It's a broken symlink, however — a symbolic link which points to something that no longer exists. The operating system tries to follow the symlink to the file that's supposed to be there file1. While hard links are an essential component of how the operating system works, symbolic links are generally more of a convenience.

You can use them to refer, in any way you'd like, to information already on the disk somewhere else. To create a symbolic link to a directory, specify the directory name as the target. For instance, let's say we have a directory named documents , which contains one file, named file. Let's create a symbolic link to documents named dox.

This command will do the trick:. We now have a symlink named dox which we can refer to as if it's the directory documents.

For instance, if we use ls to list the contents of the directory, and then to list the contents of the symlinked directory, they will both show the same file:. When we work in the directory dox now, we are actually working in documents , but we see the word dox instead of documents in all pathnames.

Symbolic links are a useful way to make shortcuts to long, complicated pathnames. For instance, this command:. Develop and improve products. List of Partners vendors. Gary Newell. Updated on February 11, Tweet Share Email. Linux Switching from Windows. Was this page helpful? Thanks for letting us know! Email Address Sign up There was an error. Please try again.

You're in! Thanks for signing up. There was an error. Tell us why! More from Lifewire. How to Redirect Command Output to a File.

Using the ls Command to List Files in Linux. How to Change Folder Icons in Windows



0コメント

  • 1000 / 1000