8.4.20 ln command (creates a link file for a file or directory)
Syntax
- Syntax 1
-
ln [-f] [-i] [-L] [-n] [-P] [-s] [-T] [-v] [--follow={yes | no}] [--no-exist-directory] [--no-exist-file] link-destination-path-name ... [target]
- Syntax 2
-
ln [-f] [-i] [-L] [-n] [-P] [-s] [-v] [--follow={yes | no}] [--no-exist-directory] [--no-exist-file] -t target-directory-name link-destination-path-name ...
Description
This command creates a link file for a file or directory. This command creates a hard link or symbolic link according to the specification of the -s option.
If you create a symbolic link with the ln command, the operation must be executed as a user having the administrative role for creating symbolic links. A user who has the administrative role for creating symbolic links cannot create a symbolic link. In addition, a user who does not have the administrative role for creating symbolic links cannot create a symbolic link in an environment where user account control (UAC) is enabled. When creating a symbolic link in an environment where UAC is enabled, the administrator privilege needs to be granted to assign to the user the administrative role for creating symbolic links.
For syntax 1, a link file is created for the path specified for the argument target. If the argument target is a directory, a link file is created in the directory.
For syntax 2, a link file is created in the target directory.
Arguments
-f
- --force
-
If the file specified for the argument target already exists, a link file is created after removing the existing file.
If the command is specified with the -i option, the specification of the -i option will be valid.
However, you can enable the option that was last specified by specifying LAST for the ADSH_CMDLN_OPT_I_F environment variable.
-i
- --interactive
-
If there is a file specified for the argument target, whether to delete the file is asked. If the reply character from the standard input starts with y or Y, the existing file is deleted and a link file is created. If other characters are used for the reply and the standard input cannot be used, the process is interrupted.
If this command is specified with the -f option, the specification of the -i option will be valid.
However, you can enable the option that was last specified by specifying LAST for the ADSH_CMDLN_OPT_I_F environment variable.
-L
- --logical
-
If a symbolic link is specified for the argument link destination path name, the symbolic link is followed and a hard link for the entity indicated by the link destination is created. However, no hard link is created if the file the link destination points to does not exist.
This option is a valid option when creating a hard link. If this command is specified with the -s option, the specification is ignored.
If neither the -L option nor -P option are specified, the -L option is considered as being specified.
When the -L option and -P option are specified simultaneously, the options specified last become valid.
-n
- --no-dereference
-
This is a meaningless option. The specification is ignored.
- --follow={yes | no}
-
This is the option used for selecting the behavior when a symbolic link for a directory is specified for the argument target.
If yes is selected, the symbolic link for the directory specified for the argument target is followed and a link file is created in the directory of the link destination.
If no is specified, the symbolic link for the directory specified for the argument target is not followed and the option is handled as a link file name.
If the --follow option is not specified, yes is considered to be specified.
-P
- --physical
-
-
In Windows, Linux, and Solaris
If a symbolic link is specified for the argument link destination path name, a hard link for a the symbolic link itself is created without following the symbolic link.
This option is a valid option when creating a hard link. If this command is specified with the -s option, the specification is ignored.
If neither the -L option nor -P option are specified, the -L option is considered as being specified.
When the -L option and -P option are specified simultaneously, the options specified last become valid.
-
In AIX and HP-UX
The specification is ignored and the behavior will be the same as when the -L option has been specified.
-
-s
- --symbolic
-
Creates a symbolic link for the file or directory specified as the argument link destination path name.
If the -s option is not specified, a hard link is created. However, the following hard links cannot be created:
-
The target specified for the argument link destination path name does not exist.
-
Hard links for a directory
-
In UNIX: Files having a different file system
-
In Windows: Files having a different drive letter
If the -s option is specified, a symbolic link is created.
- In UNIX
-
A symbolic link can be created regardless of the existence of the target specified for the argument link destination path name.
- In Windows
-
Symbolic links cannot be created if the target that is specified for the argument link destination path name does not exist. When creating a symbolic link in a state where the target specified for the argument link destination path name does not exist, specify the --no-exist-directory option or the --no-exist-file option.
The created hard link will inherit access permissions.
Access permissions for symbolic links that are created are as follows:
-
In UNIX: Grants full control to all users.
-
Windows: Grants full control to Everyone upon inheriting access permissions of the directory being created
-
-t target-directory-name
- --target-directory = target-directory-name
-
Specify the directory for which the link file is to be created. If any of the following cases occurs, an error will result:
-
The target directory name is simultaneously specified with the -T option.
-
A path that is neither the directory nor a symbolic link for a directory has been specified for the target directory name.
-
A directory that does not exist has been specified for the target directory name.
-
This option has been specified multiple times.
-
-T
- --no-target-directory
-
The symbolic link for the directory specified for the argument target is not followed and the option is handled as a link file name.
If any of the following conditions are met, an error occurs:
-
The directory having the same name as that specified for the argument target exists.
-
The target directory name is simultaneously specified with the -t option.
-
-v
- --verbose
-
The argument link destination path name and the link file to be created are output to the standard output. The output formats are as follows:
-
When creating a hard link
`Argument link destination path name' => `Link file to be created'
-
When creating a symbolic link
`Argument link destination path name' -> `Link file to be created'
-
- --no-exist-directory
-
- In Windows
-
When this option is specified, a symbolic link for a directory is created if the file or directory specified for the argument link destination path name does not exist when creating a symbolic link.
Specify this option to create a symbolic link for the directory in advance in a state where a symbolic link pointing to the directory does not exist.
If any of the following conditions are met, this option is ignored:
- The file or directory specified for the argument link destination path exists.
- The -s option is not specified.
When the --no-exist-directory option and --no-exist-file option are specified simultaneously, the options specified last become valid.
- In UNIX
-
The specification is ignored.
- --no-exist-file
-
- In Windows
-
When this option is specified, a symbolic link for a file is created if the file or directory specified for the argument link destination path name does not exist when creating a symbolic link.
Specify this option to create a symbolic link for the file in advance in a state where a symbolic link pointing to the file does not exist.
If any of the following conditions are met, this option is ignored:
- The file or directory specified for the argument link destination path exists.
- The -s option is not specified.
When the --no-exist-file option and --no-exist-directory option are specified simultaneously, the options specified last become valid.
- In UNIX
-
The specification is ignored.
- link-destination-path-name
-
Specify the file path name or directory path name that the link file being created will point to. Multiple link destination path names can be specified. When specifying 2 or more link destination path names, specify a directory for the argument target or specify a directory by using the -t option with syntax 2.
- target
-
Specifies the link file name to be created or the directory name in which the link file will be created.
If a link file name is specified, the link file is created with the specified file name. If a name for the directory in which to create the link file is specified, a link file having the same name as the file specified for the argument link destination path name is created in the specified directory.
If the specification of the target is omitted, a link file is created in the current directory.
If a symbolic link for the directory is specified, track the symbolic link and create the symbolic link for the entity indicated by the link destination. However, the following options can be specified to handle the target as a link file name without following the symbolic link:
-
-T option
-
Specify no for the --follow option.
-
Specify No for the ADSH_CMDLN_FOLLOW environment variable.
-
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination. |
1 or more |
Termination with an error. |
Notes
Notes common to all platforms
-
Multiple hard links can be created for a single file. However, depending on the OS or file system, there is an upper limit for the number of hard links that can be created for a single file. The creation of a hard link will fail if the number of links exceeds the upper limit.
-
If the number of times a symbolic link is nested exceeds the upper limit of the OS upon tracking the argument link destination path name or the symbolic link specified for the argument target, an error occurs and creation of the hard link fails.
-
Specify the directory delimiter according to the OS to be executed for the file path to be specified for the argument link destination path name and argument target. If specification of the directory delimiter is incorrect, the link destination may not be tracked. For details of the directory delimiter that can be used in JP1/Advanced Shell, see the description regarding Cautions regarding specification of file and path in 2.2.3 (1) List of files that are used by .
-
If a relative path name is specified for the argument link destination path name, a symbolic link indicates the link destination starting from that directory. Therefore, be careful if the current directory is different from the directory in which the symbolic link is being created. An example is as follows:
For this reason, if the current directory is different from the directory in which the symbolic link is being created, specify the argument link destination path name with an absolute path or specify the relative path from the directory in which the symbolic link is being created.
-
If the -f option is specified or y or Y is returned as a reply of the -i option in an environment where a file or directory having the same name as the link file being created exists, the ln command creates a temporary file in the directory in which the link file is being created. In this case, the temporary file might remain if execution of the ln command is terminated. In such case, the file will need to be deleted manually.
-
Creation of the temporary file might fail depending on the length of the directory path name in which the link file is being created. In such case, delete the existing file having the same name, change the name, or change the name of the link file being created.
-
When a symbolic link for a directory is specified for the argument target, its interpretation can be changed by specifying the -T option, the --follow option, or the ADSH_CMDLN_FOLLOW environment variable. When specified simultaneously, priority will be in order of the -T option, the --follow option, and the ADSH_CMDLN_FOLLOW environment variable.
-
When the -i option and -f option are specified at the time, the specification of the -i option will always take effect. However, you can enable the option that was specified last by specifying LAST for the ADSH_CMDLN_OPT_I_F environment variable.
Notes for the UNIX edition only
Hard links cannot be created for the following items:
-
Directories
-
Files that do not exist
-
Files having different file systems
Notes for the Windows edition only
-
Hard links cannot be created for the following items:
-
Directories
-
Files that do not exist
-
Files having different drive letters
-
-
Link files cannot be created in file systems other than NTFS.
-
For symbolic links created by using the ln command, access permissions of full control are granted to Everyone after inheriting access permissions of the created directory.
-
When creating a hard link or symbolic link for an executable file, add one of the following extensions: ".bat", ".com", ".cmd", or ".exe". In addition, you need to enter the link file name to which an extension has been added in the job definition script.
-
If you attempt to create a symbolic link upon specifying a file or directory that does not exist in the argument link destination path name, an error occurs and the symbolic link cannot be created. When creating a symbolic link for a file or directory that does not exist, specify the --no-exist-directory option or the --no-exist-file option.
-
The UNC format cannot be specified for the argument target or target directory. If the UNC format is specified, an error occurs. In addition, if a hard link is created upon specifying the UNC format for the argument link destination path name, an error occurs.
-
If the -f option is specified or y or Y is returned as a reply upon specifying the -f option, due to the access permissions for existing files, deletion might not be possible and creation of a new link file might fail.
-
If assignment of full control fails while creating a symbolic link, an invalid symbolic link might remain.
-
Symbolic links may not be used in an environment where a product designed for protecting the file system has been installed. If you use this command in an environment in which a product for protecting the file system is installed, the "Failed to rename a temporary file" message is output and the system may finish with an error. In addition, if this message is output, a temporary file may remain. In this case, delete the temporary file by using the rm command provided by JP1/Advanced Shell and recreate the symbolic link.
Examples
Creating a hard link for a file.
C:\TEMP>%ADSH_OSCMD_DIR%ln test.txt hlink.txt C:\TEMP>%ADSH_OSCMD_DIR%ls -l total 714 -rw------- 2 Administrators 357 Jun 01 15:05 hlink.txt -rw------- 2 Administrators 357 Jun 01 15:05 test.txt
Omitting the argument target and creating a hard link for a file.
C:\TEMP>%ADSH_OSCMD_DIR%ln testdir\new.txt C:\TEMP>%ADSH_OSCMD_DIR%ls -l total 3572 -rw------- 2 Administrators 3572 Jun 04 11:19 new.txt drwx------ 1 Administrators Jun 04 11:19 testdir C:\TEMP>%ADSH_OSCMD_DIR%ls -l testdir total 3572 -rw------- 2 70247321 3572 Jun 04 11:19 new.txt
Creating a symbolic link for a file.
C:\TEMP>%ADSH_OSCMD_DIR%ln -s test.txt slink.txt C:\TEMP>%ADSH_OSCMD_DIR%ls -l total 357 lrw------- 1 Administrators 0 Jun 01 15:07 slink.txt -> test.txt -rw------- 1 Administrators 357 Jun 01 15:05 test.txt
Creating a symbolic link for a directory.
C:\TEMP>%ADSH_OSCMD_DIR%ln -s testdir slinkdir C:\TEMP>%ADSH_OSCMD_DIR%ls -l total 0 lrw------- 1 Administrators 0 Jun 01 15:08 slinkdir -> testdir drwx------ 1 Administrators Jun 01 15:05 testdir
The option error message is displayed. This message might vary depending on the platform in which the command is being executed. The following shows an example for Windows:
C:\TEMP>%ADSH_OSCMD_DIR%ln -z ln: illegal option -- z usage: ln [-f] [-i] [-L] [-n] [-P] [-s] [-T] [-v] [--follow={yes | no}] [--no-exist-directory] [--no-exist-file] linkpath ... [target] ln [-f] [-i] [-L] [-n] [-P] [-s] [-v] [--follow={yes | no}] [--no-exist-directory] [--no-exist-file] -t target_directory linkpath ...