Linux ln command example
There are two types of links: hard link and symbolic link ). When creating a hard link, the linked file and the linked file must be in the same file system, and cannot establish a hard link to the directory. This problem does not exist for symbolic links. By default, ln generates hard links.
Ln command
Purpose
Link file.
Syntax
Link a file to a file
Ln [-f |-N] [-S] sourcefile [targetfile]
Link one or more files to a directory
Ln [-f |-N] [-S] sourcefile... targetdirectory
Description
The ln command links the file specified in the sourcefile parameter to the file specified in the targetfile parameter, or links it to the file in another directory specified in the targetdirectory parameter. By default, the ln command creates a hard link. If you need to use the ln command to create a symbolic link, specify the-s flag.
A symbolic link is an indirect pointer to a file. Its Directory item contains the file name linked to it. Symbolic Links may span the file system and may point to directories.
If you are linking a file to a new name, you can only list one file. If you link to a directory, you can list multiple files.
The targetfile parameter is optional. If no target file is specified, the ln command creates a new file in the current directory. The new file inherits the file name specified in the sourcefile parameter. See example 5.
Note:
If the-s flag is not used, files cannot be linked between file systems.
If targetdirectory is already a symbolic link to the directory, the ln command treats the existing target as a file. This means that commands similar to Ln-Fs somepath/lname symdir do not follow the existing symdir symbolic link. Instead, it creates a new symbolic link from somepath/lname to symdir.
Flag
-F prompts the ln command to replace any existing destination path. If the target path already exists without specifying the-F flag, the ln command will not create a new link, but will write a Diagnostic message to the standard error and continue to link the remaining sourcefiles.
-N is specified. If the link is an existing file, do not overwrite the file content. -F indicates that the flag is reset. This is the default action.
-S promotes the ln command to create a symbolic link. The Symbolic Link contains the name of the file to which it is linked. When you open a link, the referenced file is used. The stat call to the symbolic link returns the target file of the link. The lstat call must be completed to obtain the link information. You can use readlink to read the content of a symbolic link. A symbolic link may span the file system and point to a directory.
Note: When the sourcefile parameter is specified for the-s flag, the absolute path must be used. If no absolute path is specified, unexpected results may occur when the sourcefile and targetfile parameters are in different directories. You do not need to have a source file before creating a symbolic link.
Exit status
This command returns the following exit values:
0. All specified files are successfully linked.
> 0 indicates an error.
Example
0. To create another link (alias) to a file, enter:
Ln-F chap1 intro
This will link Chapter 1 to the new name, intro. If intro does not exist, the file name is created. If the intro already exists, the file will be replaced with a link pointing to Chapter 1. The file names of Chapter 1 and intro point to the same file. Any change to one of them will appear in the other. If a file name is deleted by the RM command, the file is not completely deleted because it still exists with other names.
1. to link the file to the same name in another directory, enter:
Ln Index Manual
This will link the index to the new name, manual/index.
Note: In example 1, intro is the name of a file; in example 2, manual is an existing Directory.
2. To link several files to another directory name, enter:
Ln chap2 Jim/chap3/home/manual
This will link chap2 to the new name/home/manual/chap2; and Jim/chap3 to the new name/home/manual/chap3.
3. If you want to use the pattern matching character in the ln command, enter:
N manual /*.
This will link all files in the manual directory to the current directory. (point) and give them the same name in the manual directory.
Note: a space must be entered between the asterisks and periods.
4. To create a symbolic link, enter:
Ln-S/tmp/Toc toc
This creates a symbolic link TOC in the current directory. The TOC file points to the/tmp/TOC file. If the/tmp/TOC file already exists, the cat TOC command can list its content.
5. If you want to get the same result without specifying the targetfile parameter, enter:
, Ln-S/tmp/TOC