In Unix-like systems, symbolic links are very common and we will encounter various processing tasks related to symbolic links.
This article introduces some practical methods for handling symbolic links. I hope to help you write shell scripts. For more information, see.
1. Symbolic Links can be seen as pointers to other files. It is similar to an alias in Mac OS or a shortcut in windows. Remember it is similar, not just.
2. Deleting a symbolic link does not affect the original file.
Create a symbolic link:
$ Ln-s target symbolic_link_name
For example:
$ Ln-S/var/www /~ /Web
This command creates a symbolic link named web in the home directory of the logged-on user. This link points to/var/www /. The information can be seen from the following command output:
CopyCode The Code is as follows: $ ls Web
Lrwxrwxrwx 1 slynux 8 2013-02-07 web->/var/WWW
In the information shown above: Web->/var/WWW indicates that the Web points to/var/www.
For each symbolic link, the permission tag starts with the letter "L", indicating that this is a symbolic link.
Therefore, to print the symbolic links in the current directory, run the following command:
$ LS-L | grep "^ L" | awk '{print $8 }'
Grep filters the LS-L output and only displays the rows starting with L. ^ Indicates the Start mark of a string. Awk is used to print out 8th columns, that is, the file name section.
Another method is to use find to print the symbolic link, as shown below:
$ Find.-type L-print
In the preceding command, specify the type parameter of the find command as "L" and tell find to search for only symbolic link files.
-Print option: print the symbolic link list to the standard output (stdout ). "." Indicates that the search starts from the current directory.
Run the following command to print the point of the symbolic link to the target:
$ LS-l web | awk '{print $10 }'
/Var/WWW
Each line output by the LS-l command corresponds to the details of a file.
Ls-l web lists all the web details of the symbolic link file. The output 10th columns contain the object to which the object points (if this is a symbolic link ).
Therefore, to find the target associated with the symbolic link, we can use awk to print the 10th column of the detailed list of the file (LS-L output.
In addition, we can use the readlink command to complete the same task. In some cases, this is a top priority. Its usage is as follows:
$ Readlink Web
/Var/WWW
Now, we will introduce how to create and search for symbolic links in Bash. I hope this will be helpful to you.