- Unix Create a Symbolic Link
- Hard link vs. Soft link in Linux or UNIX
- UNIX create a symbolic link command
- How do I delete a symbolic link?
- UNIX create a hardlink command
- How do I delete a hard link?
- Linux ln command
- Description
- What is a link?
- Using the link command
- The difference between ln and link
- What are symbolic links?
- Creating symlinks to directories
- Syntax
- Options
- The —backup option
- Examples
- Related commands
Unix Create a Symbolic Link
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | ln command |
Est. reading time | N/A |
Hard link vs. Soft link in Linux or UNIX
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
UNIX create a symbolic link command
To create a symbolic link , enter:
$ ln -s
$ vi sales.data.txt
$ ls -l sales.data.txt
How do I delete a symbolic link?
To delete a link, enter:
$ rm
$ ls -l
$ ls -l /shared/sales/data/file.txt
If you delete the soft link itself (sales.data.txt) , the data file would still be there ( /shared/sales/data/file.txt ). However, if you delete /shared/sales/data/file.txt, sales.data.txt becomes a broken link and data is lost.
UNIX create a hardlink command
To create hard link , enter (without the -s option):
$ ln
$ ln /tmp/file link-here
How do I delete a hard link?
You can delete hard link with the rm command itself:
$ rm
$ rm link-here
If you delete a hard link, your data would be there. If you delete /tmp/file your data still be accessible via link-here hard link file.
See also:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
I am trying to create a hard link to PHP file after installation of PHP4 as a CGI .
My Current directory is clients cgi-bin and want to create a link to usr/local/php4/bin/php but getting following error:
ln: creating hard link `php’ to `/usr/local/php4/bin/php’: Invalid cross-device link
You may delete soft link using below command
# unlink ;
I could never get the order of the two things in “ln -s X Y” right, and ended up clobbering Ys with dangling links to X. So I wrote this little sanity-checker wrapper around it: lns.
It does stuff like making sure that your link won’t overwrite anything, and that it’s a link to something that already exists. Just common sense, but sense that I didn’t have.
I’ve been using it for ten years, and I think it’s the most useful program I’ve ever written. (crontab2english is a runner-up, people tell me)
If you want to create a symbolic link for multiple files under one directory e.g. want to have all jar files of java to ant lib you can use this feature of linux. This will solve your purpose as well as save the space with reduced redundancy.
One can use for loop to create a sym link of multiple files of a directory as below,
Here is a youtube comment I made based on solving a lua-5.2.3 update issue on CentOS 6.5. The issue here is that your symbolic link tutorial was the key to solving my update issue with lua-5.2.3.
Источник
Linux ln command
On Unix-like operating systems, the ln command creates links between files, associating file names with file data.
This page covers the GNU/Linux version of ln.
Description
ln creates a link to file TARGET with the name LINKNAME. If LINKNAME is omitted, a link to TARGET is created in the current directory, using the name of TARGET as the LINKNAME.
ln creates hard links by default, or symbolic links if the -s (—symbolic) option is specified. When creating hard links, each TARGET must exist.
What is a link?
Before we discuss the ln command, let’s first discuss the link command, what a link is, and how it relates to files as we know them.
A link is an entry in your file system which connects a file name to the actual bytes of data on the disk. More than one file name can «link» to the same data. Here’s an example. Let’s create a file named file1.txt:
This command echoes the string «This is a file«. Normally this would echo to our terminal, but the > operator redirects the string’s text to a file, in this case file1.txt. We can check that it worked using cat to display the contents of the file:
When this file was created, the operating system wrote the bytes to a location on the disk and also linked that data to a file name, file1.txt so that we can refer to the file in commands and arguments. If you rename the file, the contents of the file are not altered; only the information that points to it. The file name and the file’s data are two separate entities.
Here’s an illustration of the file name and the data to help you visualize it:
Using the link command
What the link command does is allow us to manually create a link to file data that already exists. So, let’s use link to create our own link to the file data recently created. In essence, we’ll create another file name for the data that already exists.
Let’s call our new link file2.txt. How do we create it?
The general form of the link command is: «link file_name linkname«. Our first argument is the name of the file whose data we’re linking to; the second argument is the name of the new link we’re creating.
Now both file1.txt and file2.txt point to the same data on the disk:
The important thing to realize is that we did not make a copy of this data. Both file names point to the same bytes of data on the disk. Here’s an illustration to help you visualize it:
If we change the contents of the data pointed to by either one of these files, the other file’s contents are changed as well. Let’s append a line to one of them using the >> operator:
Now let’s look at the contents of file1.txt:
. and now let’s look at the second file, the one we created with the link command:
Both files show the change because they share the same data on the disk. Changes to the data of either one of these files change the contents of the other.
But what if we delete one of the files? Will both files be deleted?
No. If we delete one of the files, we’re deleting one of the links to the data. Because we created another link manually, we still have a pointer to that data; we still have a way, at the user-level, to access the data we put in there. So if we use the rm command to remove our first file:
. it no longer exists as a file with that name:
. but the link to the data we manually created still exists, and still points to the data:
As you can see, the data stays on the disk even after the «file» (which is actually a link to the data) is removed. We can still access that data as long as there is a link to it. This is important to know when you’re removing files — «removing» a file makes the data inaccessible by unlink-ing it. The data still exists on the storage media, somewhere, inaccessible to the system, and that space on disk is marked as being available for future use.
The type of link we’ve been working with here is sometimes called a «hard» link. A hard link and the data it links to must always exist on the same filesystem; you can’t, for instance, create a hard link on one partition to file data stored on another partition. You also can’t create a hard link to a directory. Only symbolic links may link to a directory; we’ll get to that in a moment.
The difference between ln and link
So what about ln? That’s why we’re here, right?
ln, by default, creates a hard link like link does. So this ln command:
. is the same as the following link command:
. because both commands create a hard link named file2.txt which links to the data of file1.txt.
However, we can also use ln to create symbolic links with the -s option. So the command:
Create a symbolic link to file1.txt named file2.txt. In contrast to our hard link example, here’s an illustration to help you visualize our symbolic link:
What are symbolic links?
Symbolic links, sometimes called «soft» links, are different than «hard» links. Instead of linking to the data of a file, they link to another link. So in the example above, file2.txt points to the link file1.txt, which in turn points to the data of the file.
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.txt:
. and create a symbolic link to it:
. we can cat either one of these to see the contents:
. but if we remove file1.txt:
. we can no longer access the data it contained with our symlink:
This error message might be confusing at first, because file2.txt still exists in your directory. 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.txt), but finds nothing, and so it returns the error message.
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.
Creating symlinks to directories
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.txt.
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:
. saves us a lot of typing; now, instead of changing directory with the following command:
. we can do this, instead:
Normally, you remove directories (once they’re empty) with the rmdir command. But our symbolic link is not actually a directory: it’s a file that points to a directory. So to remove our symlink, we use the rm command:
This command removes the symlink, but the original directory and all its files are not affected.
Syntax
Options
Here are the options that can be passed to the ln command.
—backup[=CONTROL] | Use this option to additionally create a backup of each existing destination file. The style of backup is optionally defined by the value of CONTROL. See backup options below for more information. |
-b | This functions like —backup, but you cannot specify the CONTROL; the default style (simple) is used. |
-d, -F, —directory | This option allows the superuser to attempt to hard link directories (although it will probably fail due to system restrictions, even for the superuser). |
-f, —force | If the destination file or files already exist, overwrite them. |
-i, —interactive | Prompt the user before overwriting destination files. |
-L, —logical | Dereference TARGETs that are symbolic links. In other words, if you are trying to create a link (or a symlink) to a symlink, link to what it links to, not to the symlink itself. |
-n, —no-dereference | Treat LINKNAME as a normal file if it’s a symbolic link to a directory. |
-P, —physical | Make hard links directly to symbolic links, rather than dereferencing them. |
-r, —relative | Create symbolic links relative to link location. |
-s, —symbolic | Make symbolic links instead of hard links. |
-S, —suffix=SUFFIX | Use the file suffix SUFFIX rather than the default suffix « «. |
-t, —target-directory=DIRECTORY | Specify the DIRECTORY to create the links. |
-T, —no-target-directory | Always treat LINKNAME as a normal file. |
-v, —verbose | Operate verbosely; print the name of each linked file. |
—help | Display a help message, and exit. |
—version | Display version information, and exit. |
The —backup option
When using the —backup (or -b) option, the default file suffix for backups is ‘
‘. You can change this, however, using the —suffix option or setting the SIMPLE_BACKUP_SUFFIX environment variable.
The CONTROL argument to the —backup option specifies the version control method. Alternatively, it can be specified by setting the VERSION_CONTROL environment variable. Here are the values to use for either one:
none, off | Never make backups (even if —backup is given). |
numbered, t | Make numbered backups. |
existing, nil | Numbered if numbered backups exist, simple otherwise. |
simple, never | Always make simple backups. |
If you use -b instead of —backup, the CONTROL method is always simple.
If you specify the -s option (which symlinks), ln ignores the -L and -P options. Otherwise (if you are making hard links), the last option specified controls behavior when a TARGET is a symbolic link. The default is to act as if -P was specified.
Examples
Create a hard link to the file public_html/myfile1.txt in the current directory.
Create a symbolic link to the file public_html/myfile1.txt in the current directory.
Create a symbolic link to the directory public_html named webstuff.
Creates a symbolic link to the file file1.txt named file2.txt. If file2.txt already exists, it is renamed to file2.txt
before the new file2.txt symlink is created.
Related commands
chmod — Change the permissions of files or directories.
link — Create a hard link to a regular file.
ls — List the contents of a directory or directories.
readlink — Print the value of a symbolic link or canonical file name.
unlink — Remove a file.
Источник