Ln command
Purpose: link a file.
Syntax
1> link a file to a file
Ln [-f |-N] [-S] sourcefile [targetfile]
2> 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
Or link the file specified in the targetdirectory
Files in another directory specified in the parameter.
By default, the ln command creates a hard link. If you want to use 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 the target file is not specified, ln
Command to create a new file in the current directory. The new file inherits
The file name in the parameter.
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, then ln
Command to treat 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 path from somepath/lname to symdir
.
Parameters
-F promotes ln
Command to replace any existing destination path. If the target path already exists and-F is not specified
Flag, ln
The command does not create a new link, but writes a Diagnostic message to a standard error and continues to link the remaining
Sourcefiles.
-N is specified. If the link is an existing file, do not overwrite the file content. -F
The flag resets this flag. This is the default action.
-S promotes 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. For Symbolic Links
The stat call will return the target file of the link. The lstat must be completed.
To obtain the link information. You can use readlink
To read the contents of the symbolic link. A symbolic link may span the file system and point to a directory.
Note: When sourcefile is specified for the-s flag
The absolute path must be used. If no absolute path is specified, when sourcefile and
Targetfile
When the parameters are in different directories, unexpected results may occur. 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
1> to create another link (alias) to a file, enter:
Ln-F file1 file2
This will link file1 to the new name, file2. If file2
The file name does not exist. If file2
If the file already exists, the file will be replaced with a link pointing to file1. Then file1 and
File2
The file name points to the same file. Any change to one of them will appear in the other. If a file name is
The RM command is used to delete the file. The file is not completely deleted because it still exists with other names.
2> to link the file to the same name in another directory, enter:
Ln index dir1
This will link the index to the new name dir1/index.
Note: In example 1, file2 is the name of a file; in example 2, dir1
Is an existing Directory.
3> to link several files to another directory name, enter:
Ln file2 dir2/file3/home/dir1
This will link file2 to the new name/home/dir1/file2; Link dir2/file3 to the new name
/Home/dir1/file3.
4> if you want to use the pattern matching character in the ln command, enter:
Ln dir1 /*.
This will link all the files in the dir1 directory to the current directory. (point), give them
The same name in the directory.
Note: a space must be entered between the asterisks and periods.
5> to create a symbolic link, enter:
Ln-S/tmp/test
This creates the symbolic link test in the current directory. The test file points to the/tmp/test file. If
The/tmp/test file already exists, so the cat test command can list its content.
6> if you want to get the same result without specifying the targetfile parameter, enter:
Ln-S/tmp/test
File
/Usr/bin/ln contains the ln command.