How to create, update and delete soft links in Linux and UNIX
Symbolic links or soft links in Unix is a very important concept to understand and use in various UNIX operating systems like Linux, Solaris or IBM AIX. Symbolic links provide us so much power and flexibility and make it very easy for us to maintain things. I personally feel that commands to create soft links and update soft links are must-have commands for anyone working on a UNIX machine, in addition to find , grepln -s
and other UNIX commands . Whenever I write a script or write any UNIX script, I always write for symbolic links instead of absolute paths pointing to directories in UNIX.
It gives us the flexibility to change symbolic links or soft links without making any changes to our time-tested scripts. Many different core Java projects, which run on Linux and UNIX machines, make extensive use of UNIX symbolic links or symlinks.
In this UNIX basics article, we will see how to create soft links in UNIX, how to update soft links and the difference between soft links and hard links in Unix and Linux. By the way, this article is a continuation of our previous articles Network Commands in Unix and CVS Commands Examples, if you haven’t read them yet, you might find some useful information based on our experience with Unix and Linux commands.
Symbolic links in UNIX or Linux
Although this UNIX command article is to highlight the difference between soft links in UNIX and hard links in UNIX, which is also a very popular UNIX command interview question, I feel it is necessary to show you the usage of soft links in UNIX. Here are some UNIX symbolic links examples that I have seen in projects involving UNIX soft links:
1) In our project, our Java process picks up the latest version of the package to execute, which is a UNIX soft link. So whenever we do a release, by using tar archives, we only need to update the latest UNIX symbolic link, which makes releases seamless and rollbacks very easy, which in turn increases the stability and predictability of our Java applications.
2) All our UNIX scripts take locations as parameters, so they don’t know the absolute path to the resources, and the resources are provided through UNIX soft links and environment variables. This feature of our scripts saves a lot of time whenever we need to do any migration that involves changing resource locations.
3) An important thing about UNIX soft links is that they inherit the permissions of the directory they point to. This means that if you change the permissions of the directory using the command in Unix chmod
, the permissions of the soft link will also be updated.
The difference between soft links and hard links in UNIX
In this section, we will look at some of the differences between soft links and hard links in UNIX. These differences are by no means complete, so if you know of any other differences between UNIX soft links and hard links, please let us know. You can also let us know how you use symbolic links or UNIX soft links.
Soft links and hard links in UNIX
1) The first difference between soft links and hard links is that Unix soft links are pointers to programs, files, or directories located elsewhere (just like Windows shortcuts), while Unix hard links are pointers to programs and files, not directories.
2) The second major difference between UNIX soft links and hard links is that if the original program, file, or directory is renamed, moved, or deleted, the soft link is broken and ls -lrt --color
will appear in red if you use it. On the other hand, a hard link will not be broken if the original program or file is renamed, moved, or deleted.
3) A less important difference between soft links and hard links is that if we type ls -F
we can see which files are UNIX soft links because they @
end with
4) Another difference between soft links and hard links is the way they are created. To create a soft link named "current" that points to a file or directory named "new_package", use: ln -s new_package latest
Always remember this command, remember that the name of the soft link appears as the last parameter. On the other hand, to create a UNIX hard link named myhardlink.txt that points to a file named myfile.txt , use:ln myfile.txt myhardlink.txt
5) One of the more important differences between soft links and hard links on UNIX or Linux is that soft links can also point to network mounted directories. To create a UNIX soft link, remember to use the option "**-s**" with the UNIX link command " ln ". Although hard links in UNIX cannot span disk drives, so we cannot have a hard link on /dev/hdb to reference a program or file on /dev/hda
Creating a symbolic link or soft link in UNIX
Here we will see how to create soft links and hard links in UNIX, also known as symbolic links or symlinks in Linux. For our symbolic link example, we will use a folder called symlinks which contains some directories representing different versions of a particular application, we will learn how to create symbolic links, delete symbolic links and update symbolic links or soft links in Unix and Linux.
This is an initial snapshot of our example symlink directory, with no symlinks in the current directory.
$ ls -lrt
total 0
drwxr-xr-x+ 2 Jiyik None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Jiyik None 0 Apr 22 12:14 1.3
Now we will create soft links in UNIX in the symbolic link directory.
$ ln -s 1.3 latest
This will create a soft link named " latest " pointing to the directory " 1.3 ". Let's see if this soft link is created under UNIX. We can see that a symbolic link is created in the last line.
注意
lrwxrwxrwx (the first "l" means it is a link in UNIX)
$ ls -lrt
total 1
drwxr-xr-x+ 2 Jiyik None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Jiyik None 0 Apr 22 12:14 1.3
lrwxrwxrwx 1 Jiyik None 3 Apr 22 12:17 latest -> 1.3
Updating symbolic or soft links in UNIX
Now that we have seen how to create a symbolic link in UNIX, we will now see how to update that symbolic link or soft link without deleting it.
$ ln -nsf 1.2 latest
This will update the symbolic link to point to the directory " 1.2 " instead of " 1.3 ". Note the command line options -nsf
. Now let's see if the symbolic link is updated.
$ ls -lrt
total 1
drwxr-xr-x+ 2 Jiyik None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Jiyik None 0 Apr 22 12:18 1.3
lrwxrwxrwx 1 Jiyik None 3 Apr 22 12:19 latest -> 1.2
Now let's create another link in this directory pointing to 1.2 and latest to 1.3
$ ln -nsf 1.3
$ ln -s 1.2 previous
$ ls -lrt
total 2
drwxr-xr-x+ 2 Jiyik None 0 Apr 22 12:14 1.2
lrwxrwxrwx 1 Jiyik None 3 Apr 22 12:20 latest -> 1.3
drwxr-xr-x+ 2 Jiyik None 0 Apr 22 12:21 1.3
lrwxrwxrwx 1 Jiyik None 3 Apr 22 12:24 previous -> 1.2
If you notice here when we run ls –lrt
, the total will show the number of UNIX soft links that exist in that directory, and we can see it went from 0 to 1 in the previous example, and now it's 2 because of the two symbolic links that appear here.
Deleting a symbolic link or soft link in UNIX
Removing a symbolic link is similar to deleting any file; we need to use rm
the UNIX command to remove any symbolic link. This will only remove the symbolic link and will not delete the source directory.
$ rm latest previous
$ ls -lrt
total 0
drwxr-xr-x+ 2 Jiyik None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Jiyik None 0 Apr 22 12:18 1.3
UNIX symbolic link or soft link prompt
Here are some more tips on creating soft links in UNIX
1) Update soft link using ln -nfs
. As I said before, we have an up-to-date symbolic link pointing to the latest package, and we need to update this Linux soft link every time we release it. Before knowing this option, I used to delete the old soft link first and then create a new one. The link is a two-step process, but this is the fastest way, just execute ln -nfs new_pakcage latest
and the latest soft link will point to a new package.
2) Use it in conjunction with UNIX soft links pwd
to find out the actual path your soft link points to. Many times we need to know where we are after navigating through various soft and hard links. Here pwd tells us the absolute path of the current working directory in UNIX.
3) To find out all UNIX soft links and hard links in any directory, execute the following command ls -lrt | grep "^l"
. It is an extension of my UNIX command improvisation ls -lrt | grep "^d"
to find out all directories in any directory by using command.
$ ls -lrt | grep "^l"
lrwxrwxrwx 1 Jiyik None 3 Apr 22 12:20 latest -> 1.3
lrwxrwxrwx 1 Jiyik None 3 Apr 22 12:24 previous -> 1.2
Here, we take advantage of ls
the fact that the grep command in Unix displays an "l" in front of each entry, and then we do a UNIX grep for links that begin with "l" and directories that begin with "d".
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Linux Sed Tutorial: 6 Examples of Sed Branching Operations
Publish Date:2025/04/06 Views:165 Category:OPERATING SYSTEM
-
Like any other programming language, sed also provides special branching commands to control the flow of the program. In this article, let's review the following two types of Sed branches. Sed unconditional branch Sed conditional branching
awk tutorial – 7 awk printing examples
Publish Date:2025/04/06 Views:118 Category:OPERATING SYSTEM
-
This is the first article in the new awk tutorial series. We will publish several articles on awk in the coming time, which will explain all the features of awk with practical examples. In this article, let's review the basic awk working me
Sed Tutorial: Advanced Sed Substitution Examples
Publish Date:2025/04/06 Views:80 Category:OPERATING SYSTEM
-
In this article, let's review some interesting workarounds using the "s" substitution command in sed with a few real-world examples. 1. sed replaces the delimiter As we discussed in our previous article, we can use different delimiters in s
How to Find IP Address from Host Name in Windows Linux and Unix
Publish Date:2025/04/06 Views:89 Category:OPERATING SYSTEM
-
How many times in a day do we have a hostname and want to know the IP address? While dealing with network commands in Unix, hostname to IP address and IP address to hostname conversion is one of the common things we need to do for many thin
How to install GrayLog on centos
Publish Date:2025/04/06 Views:122 Category:OPERATING SYSTEM
-
Installing Graylog on CentOS requires the following steps: 1. Add EPEL and MongoDB repositories: sudo yum install epel-release -y sudo rpm -Uvh https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.4-1.el7
export command in Linux
Publish Date:2025/04/05 Views:109 Category:OPERATING SYSTEM
-
The Linux terminal has many environment variables that contain necessary information about the system. Also, applications may require some environment variables in order to execute. export This article will use the command to define an envi
tr Command in Linux Bash
Publish Date:2025/04/05 Views:54 Category:OPERATING SYSTEM
-
In Linux, we can use Bash scripts to perform string manipulation such as concatenation, truncation, and finding words in a text. tr This article will explain how to translate or remove characters using command in Linux Bash . tr Using comma
How to Copy Files and Directories Using Linux Terminal
Publish Date:2025/04/05 Views:122 Category:OPERATING SYSTEM
-
cp We can use the and commands in Linux Terminal rsync to copy files and directories. cp The command is generally used to copy files, while rsync the command is generally used to copy directories. Use cp the command to copy files We use com
Obtaining an IPv4 Address on Unix and Linux
Publish Date:2025/04/05 Views:200 Category:OPERATING SYSTEM
-
IP stands for Internet Protocol, which specifies the principles of Internet communications. An Internet Protocol (IP) address is a unique identifier for each device on the Internet, allowing data to be transferred between connected devices.