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
Restart PostgreSQL in Ubuntu 18.04
Publish Date:2025/04/09 Views:72 Category:PostgreSQL
-
This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n
Issues to note when installing Apache on Linux
Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM
-
As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s