Method and apparatus for automatically and safely operating linux soft links
This script automatically finds and safely deletes Linux symbolic links, resolving the issue of accidentally deleting files or folders with the same name in Linux systems and ensuring that applications function normally.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHINA UNITECHS
- Filing Date
- 2022-12-05
- Publication Date
- 2026-06-02
AI Technical Summary
In Linux systems, deleting symbolic links can easily lead to accidentally deleting files or folders with the same name, causing applications to malfunction.
The script automatically finds the Linux symbolic link paths to be deleted, uses the `ls -li` and `grep -e '->'` commands to filter out the symbolic links, safely deletes them, and then recreates new links.
This avoids the problem of accidentally deleting files or folders with the same name, ensuring that the application runs normally.
Smart Images

Figure CN115794748B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software, and in particular to a method and apparatus for automatically and securely operating Linux soft links. Background Technology
[0002] In a real-world production environment, an application might access a third-party tool through a specified directory. This tool's library could be large and consume significant disk space. If the library is later migrated to another location, the data remains unchanged during the migration, but the access path has changed, causing the application to malfunction. Modifying the client application is obviously impractical or extremely difficult. To solve this problem, Linux symbolic links are typically used. A Linux symbolic link creates a synchronously updated link to a file in another location. When the file access path changes, the symbolic link can be deleted and rebuilt based on the new access path without modifying the application. Normally, deleting a Linux symbolic link does not affect the original file or other files. For example, when using the command `ln -s / var / log / cinder cinder` to create a Linux link, the result is displayed as `cinder -> / var / log / cinder`. After creating the symbolic link, the relationship between the original directory and the symbolic link is represented in this form.
[0003] in:
[0004] / var / log / cinder is the original directory where the source files are stored.
[0005] Cinder is a symbolic link that can be understood as a Windows shortcut. It is also a directory where the source files can be seen. In other words, you can see the source files by directly opening the cinder directory or the / var / log / cinder directory.
[0006] Normally, executing `rm -rf cinder` to delete a Linux symbolic link does not affect the original files or other files under ` / var / log / cinder`. However, if there is a folder or file in the current directory with the same name as the symbolic link `cinder`, executing `rm -rf cinder` may cause the `cinder` file or folder with the same name as the Linux symbolic link to be mistakenly deleted. For example, in the following example, a query will find that there is a folder or file with the same name as the symbolic link `cinder`.
[0007] `ls -li` displays folders;
[0008] The symbolic links displayed by `ls -li cinder`;
[0009] [root@localhost ~]# ls -li
[0010] Total usage: 4,186,672 ...
[0012] 33602130 drwxr-xr-x 2 root root 20 Oct 22 10:52 cinder #folder
[0013] 7088902 drwxr-xr-x 2 root root 20 October 22 10:44 scinder ...
[0015] [root@localhost ~]# ls -li cinder # View Linux symbolic links (cinder), where cinder is the keyword.
[0016] Total usage 0
[0017] 33602390 lrwxrwxrwx 1 root root 15 October 22 10:52 cinder -> / var / log / cinder
[0018] [root@localhost ~]# Summary of the Invention
[0019] To address the problems existing in the prior art, this invention provides a method and apparatus for automatically and safely operating Linux symbolic links. The method uses a script to automatically find a safe path for operating Linux symbolic links to be deleted, thus avoiding accidental deletion.
[0020] To achieve the above objectives, the present invention adopts the following technical solution:
[0021] In one embodiment of the present invention, a method for automatically and securely operating Linux symbolic links is proposed, the method comprising:
[0022] S01. To obtain the Linux symbolic link keyword, use find to locate the path containing the keyword, remove the keyword part at the end of the path and store it in list1.
[0023] S02. Loop through list1 and use ls -li loop variable | grep -e '->' to get the path that matches the result of the Linux symbolic link keyword, and concatenate it with the Linux symbolic link keyword and store it in a new list list2;
[0024] S03. Loop through list2 to safely delete the Linux symbolic link. After safely deleting the Linux symbolic link, you can create a new Linux symbolic link according to the new directory path.
[0025] Furthermore, in S02, the ls command in the ls-li loop variable lists the files or folders that exist in the current directory. Adding |grep -e '->' will filter and only display folders or files containing '->', and the displayed files or folders are symbolic links; those that do not contain '->' will return an empty value.
[0026] Further, S02 includes:
[0027] S021. If the result of executing `ls -li loop variable | grep -e '->'` is empty, then the Linux connection method is not a symbolic link.
[0028] S022. If the result of `ls -li loop variable | grep -e '->'` is not empty, then the Linux connection method is a symbolic link.
[0029] In one embodiment of the present invention, an apparatus for automatically and securely operating Linux soft links is also provided, the apparatus comprising:
[0030] The list1 module is created by obtaining the Linux symbolic link keyword, using the find method to locate the path containing the keyword, removing the keyword part from the end of the path and storing it in the list1;
[0031] The list2 module is created, and the list1 list is looped through. The variable is obtained by using ls -li | grep -e '->'. The path that is consistent with the result of the Linux symbolic link keyword is obtained and concatenated with the Linux symbolic link keyword and stored in the new list list2.
[0032] The symbolic link deletion module, by looping through the list list2, can safely delete Linux symbolic links. After safely deleting a Linux symbolic link, you can create a new Linux symbolic link according to the new directory path.
[0033] Furthermore, in the list2 creation module, the ls command in the ls-li loop variable lists the files or folders that exist in the current directory. Adding |grep -e '->' will filter and only display folders or files containing '->', and the displayed files or folders are symbolic links; those that do not contain '->' will return an empty value.
[0034] Furthermore, the list2 creation module includes:
[0035] If a non-soft link module or the `ls -li` loop variable `|grep -e '->'` returns an empty value, then the Linux linking method is not a soft link.
[0036] If the symbolic link module and the ls-li loop variable | grep-e '->' result are not empty, then the Linux connection method is a symbolic link.
[0037] In one embodiment of the present invention, a computer device is also proposed, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the aforementioned method for automatically and securely operating Linux soft links.
[0038] In one embodiment of the present invention, a computer-readable storage medium is also provided, which stores a computer program for performing a method of automatically securely operating Linux soft links.
[0039] Beneficial effects:
[0040] This invention finds all paths containing the keyword "linux symbolic link", removes the keyword from the end of the path and stores it in a list. The command `ls -li path without keyword` is executed to obtain paths matching the result of `ls -li keyword`. Finally, `rm -rf path with keyword` is executed to perform the deletion operation. Afterwards, a new Linux symbolic link is created based on the new directory path. The script automatically finds safe paths to delete Linux symbolic links, avoiding accidental deletion and other problems. Attached Figure Description
[0041] Figure 1 This is a flowchart illustrating the method for automatically and securely operating Linux soft links according to the present invention;
[0042] Figure 2 This is a schematic diagram of the device structure for automatically and securely operating Linux soft links according to the present invention;
[0043] Figure 3 This is a schematic diagram of a computer device structure according to an embodiment of the present invention. Detailed Implementation
[0044] The principles and spirit of the present invention will now be described with reference to several exemplary embodiments. It should be understood that these embodiments are provided merely to enable those skilled in the art to better understand and implement the present invention, and are not intended to limit the scope of the present invention in any way. Rather, these embodiments are provided to make this disclosure more thorough and complete, and to fully convey the scope of this disclosure to those skilled in the art.
[0045] Those skilled in the art will recognize that embodiments of the present invention can be implemented as a system, apparatus, device, method, or computer program product. Therefore, this disclosure can be specifically implemented in the following forms: entirely hardware, entirely software (including firmware, resident software, microcode, etc.), or a combination of hardware and software.
[0046] According to an embodiment of the present invention, a method and apparatus for automatically and safely operating Linux symbolic links are proposed. The method uses a script to automatically find a safe path for operating Linux symbolic links to be deleted, thereby avoiding accidental deletion.
[0047] The principles and spirit of the present invention will be explained in detail below with reference to several representative embodiments.
[0048] like Figure 1 As shown, the method includes:
[0049] S01. To obtain the Linux symbolic link keyword, use find to locate the path containing the keyword, remove the keyword part at the end of the path and store it in list1.
[0050] S02. Loop through list1 and use ls -li loop variable | grep -e '->' to get the path that matches the result of the Linux symbolic link keyword, and concatenate it with the Linux symbolic link keyword and store it in a new list list2;
[0051] S03. Loop through list2 to safely delete the Linux symbolic link. After safely deleting the Linux symbolic link, you can create a new Linux symbolic link according to the new directory path.
[0052] In S02, the ls command in the ls-li loop variable lists the files or folders in the current directory. Adding |grep -e '->' will filter and only display folders or files containing '->', and the displayed files or folders will be symbolic links; those not containing '->' will return an empty value.
[0053] The S02 includes:
[0054] S021. If the result of executing `ls -li loop variable | grep -e '->'` is empty, then the Linux connection method is not a symbolic link.
[0055] S022. If the result of `ls -li loop variable | grep -e '->'` is not empty, then the Linux connection method is a symbolic link.
[0056] It should be noted that although the operation of the method of the present invention has been described in a specific order in the above embodiments and figures, this does not require or imply that the operations must be performed in that specific order, or that all the operations shown must be performed to achieve the desired result. Additionally or alternatively, certain steps may be omitted, multiple steps may be combined into one step, and / or one step may be broken down into multiple steps.
[0057] To provide a clearer explanation of the above-described method for automatically and securely operating Linux soft links, a specific embodiment will be used for illustration below. However, it is worth noting that this embodiment is only for better illustrating the present invention and does not constitute an improper limitation of the present invention.
[0058] First, enter the Linux symbolic link keyword to be manipulated. Use the `find` command to search for all paths containing the keyword, remove the keyword from the end of the path, and store the result in a list. Loop through the list and execute `ls -li remove_keyword_path` to find paths matching the result of `ls -li keyword`, then concatenate these paths with the Linux symbolic link keyword and store them in a new list. Loop through the new list and execute `rm -rf loop_variable` to safely delete the Linux symbolic link. Afterward, create a new Linux symbolic link based on the new directory path. The following example uses the above data for further explanation:
[0059] S201: First, obtain the Linux symbolic link keyword, use the find method to locate the path containing the keyword, remove the keyword portion from the end of the path, and store it in list1:
[0060] [root@localhost ~]# find / -name cinder
[0061] / etc / selinux / targeted / active / modules / 100 / cinder
[0062] / root / scinder / cinder
[0063] / root / cinder
[0064] / root / cinder / cinder
[0065] / var / log / cinder
[0066] / apps / logs / cinder
[0067] / apps / logs / cinder / cinder
[0068] [root@localhost ~]#
[0069] Remove trailing keyword paths:
[0070] / etc / selinux / targeted / active / modules / 100
[0071] / root / scinder
[0072] / root
[0073] / root / cinder
[0074] / var / log
[0075] / apps / logs
[0076] / apps / logs / cinder
[0077] The path is stored in list1:
[0078] [' / etc / selinux / targeted / active / modules / 100',' / root / scinder',' / root / ',' / root / cinder',' / var / log',' / apps / logs',' / apps / logs / cinder']
[0079] S202: Loop through list1, use 'ls -li loop variable | grep -e '->'' to get the path that matches the result of 'ls -li keyword', and concatenate it with the Linux symbolic link keyword to store it in a new list.
[0080] Loop through list1 and use 'ls -li loop variable' to get the path that matches the result of 'ls -li keyword':
[0081] If the output of `ls -li loop_variable | grep -e '->'` is empty, it means it's not a symbolic link; otherwise, it is a symbolic link and can be safely deleted.
[0082] Further explanation:
[0083] The `ls` command in the `ls -li` loop lists the files and folders in the current directory. Adding `| grep -e '->'` will filter and only show folders or files containing '->'. These are symbolic links and can be deleted. Files or folders without '->' will return an empty value and cannot be deleted.
[0084] [root@localhost ~]# ls -li / etc / selinux / targeted / active / modules / 100 |grep -e '->'
[0085] [root@localhost ~]# ls -li / root / scinder | grep -e '->'
[0086] 7088904 lrwxrwxrwx 1 root root 15 October 22 10:44 cinder -> / var / log / cinder
[0087] [root@localhost ~]# ls -li / var / log | grep -e '->'
[0088] [root@localhost ~]# ls -li / root | grep -e '->'
[0089] [root@localhost ~]# ls -li / root / cinder | grep -e '->'
[0090] 33602390 lrwxrwxrwx 1 root root 15 October 22 11:34 cinder -> / var / log / cinder
[0091] [root@localhost ~]# ls -li / apps / logs | grep -e '->'
[0092] [root@localhost ~]# ls -li / apps / logs / cinder | grep -e '->'
[0093] 7088905 lrwxrwxrwx 1 root root 15 October 22 11:59 cinder -> / var / log / cinder
[0094] [root@localhost ~]#
[0095] Get the path that matches the result of executing 'ls -li keyword':
[0096] After removing the empty return values, the remaining part is the path that matches the result of executing 'ls -li keyword', which is the Linux symbolic link that can be safely deleted.
[0097] [root@localhost ~]# ls -li / root / scinder | grep -e '->' # Get the path / root / scinder
[0098] 7088904 lrwxrwxrwx 1 root root 15 October 22 10:44 cinder -> / var / log / cinder
[0099] [root@localhost ~]# ls -li / root / cinder | grep -e '->' # Get the path / root / cinder
[0100] 33602390 lrwxrwxrwx 1 root root 15 October 22 11:34 cinder -> / var / log / cinder
[0101] [root@localhost ~]# ls -li / apps / logs / cinder | grep -e '->' # Get the path / apps / logs / cinder
[0102] 7088905 lrwxrwxrwx 1 root root 15 October 22 11:59 cinder -> / var / log / cinder
[0103] The obtained path and keywords are concatenated, and the concatenated result is stored in list2:
[0104] [' / root / scinder / cinder',' / root / cinder / cinder',' / apps / logs / cinder / cinder']
[0105] S203: Loop through list2 to safely delete Linux symbolic links. After safely deleting a Linux symbolic link, you can create a new Linux symbolic link according to the new directory path.
[0106] for j in list2:
[0107] `rm -rf j` # Delete Linux symbolic links in a loop
[0108] `ln -s file_new_directory_cinder` # Creates a Linux symbolic link using a new directory; the symbolic link `cinder` remains unchanged.
[0109] Based on the same inventive concept, this invention also proposes an apparatus for automatically and securely operating Linux soft links. The implementation of this apparatus can refer to the implementation of the methods described above, and repeated details will not be repeated. The term "module" as used below can refer to a combination of software and / or hardware that implements a predetermined function. Although the apparatus described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0110] Figure 2 This is a schematic diagram of the device structure for automatically and securely operating Linux soft links according to the present invention. Figure 2 As shown, the device includes:
[0111] Module 110: In the process of obtaining the Linux symbolic link keyword, use the find method to locate the path containing the keyword, remove the keyword part at the end of the path and store it in the list list1.
[0112] The list2 module 120 is created by looping through the list1 list. The list1 list is obtained by using ls -li to loop through the variable | grep -e '->' to get the path that is consistent with the result of the Linux soft link keyword, and then concatenating it with the Linux soft link keyword and storing it in a new list list2.
[0113] The symbolic link deletion module 130 reads the list list2 in a loop to safely delete Linux symbolic links. After safely deleting a Linux symbolic link, you can create a new Linux symbolic link according to the new directory path.
[0114] Furthermore, in the list2 creation module 120, the ls command in the ls-li loop variable lists the files or folders existing in the current directory. Adding |grep -e '->' will filter and only display folders or files containing '->', and the displayed files or folders are symbolic links; those not containing '->' will return an empty value.
[0115] Furthermore, the list2 creation module 120 includes:
[0116] If a non-soft link module or the `ls -li` loop variable `|grep -e '->'` returns an empty value, then the Linux linking method is not a soft link.
[0117] If the symbolic link module and the ls-li loop variable | grep-e '->' result are not empty, then the Linux connection method is a symbolic link.
[0118] It should be noted that although several modules of the apparatus for automatically and securely operating Linux symbolic links have been mentioned in the detailed description above, this division is merely exemplary and not mandatory. In fact, according to embodiments of the present invention, the features and functions of two or more modules described above can be embodied in a single module. Conversely, the features and functions of a single module described above can be further divided and embodied by multiple modules.
[0119] Based on the aforementioned inventive concept, such as Figure 3 As shown, the present invention also proposes a computer device 200, including a memory 210, a processor 220, and a computer program 230 stored in the memory 210 and executable on the processor 220. When the processor 220 executes the computer program 230, it implements the aforementioned method for automatically and securely operating Linux soft links.
[0120] Based on the aforementioned inventive concept, the present invention also proposes a computer-readable storage medium storing a computer program that performs the aforementioned method for automatically and securely operating Linux soft links.
[0121] This invention finds all paths containing the keyword "linux symbolic link", removes the keyword from the end of the path and stores it in a list. The command `ls -li path without keyword` is executed to obtain paths matching the result of `ls -li keyword`. Finally, `rm -rf path with keyword` is executed to perform the deletion operation. Afterwards, a new Linux symbolic link is created based on the new directory path. The script automatically finds safe paths to delete Linux symbolic links, avoiding accidental deletion and other problems.
[0122] While the spirit and principles of the invention have been described with reference to several specific embodiments, it should be understood that the invention is not limited to the disclosed specific embodiments, and the division of aspects does not imply that features in these aspects cannot be combined for benefit; such division is merely for ease of description. The invention is intended to cover various modifications and equivalent arrangements included within the spirit and scope of the appended claims.
[0123] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.
[0124] The program code used to implement the methods of this disclosure may be written in any combination of one or more programming languages. This program code may be provided to a processor or controller of a general-purpose computer, special-purpose computer, or other programmable data processing apparatus, such that when executed by the processor or controller, the program code causes the functions / operations specified in the flowcharts and / or block diagrams to be implemented. The program code may be executed entirely on a machine, partially on a machine, as a standalone software package partially on a machine and partially on a remote machine, or entirely on a remote machine or server.
[0125] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0126] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device for displaying information to the user (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor); and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the computer. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).
[0127] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as a data server), or computing systems that include middleware components (e.g., an application server), or computing systems that include frontend components (e.g., a user computer with a graphical user interface or web browser through which a user can interact with embodiments of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., a communication network). Examples of communication networks include local area networks (LANs), wide area networks (WANs), and the Internet.
[0128] Computer systems can include clients and servers. Clients and servers are generally located far apart and typically interact via communication networks. Client-server relationships are created by computer programs running on the respective computers and having a client-server relationship with each other. Servers can be cloud servers, servers in distributed systems, or servers incorporating blockchain technology.
[0129] It should be understood that the various forms of processes shown above can be used to rearrange, add, or delete steps. For example, the steps described in this invention disclosure can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution disclosed in this disclosure can be achieved, and this is not limited herein.
[0130] The specific embodiments described above do not constitute a limitation on the scope of protection of this disclosure. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this disclosure should be included within the scope of protection of this disclosure.
[0131] Regarding the limitation of the scope of protection of this invention, those skilled in the art should understand that various modifications or variations that can be made by those skilled in the art without creative effort based on the technical solution of this invention are still within the scope of protection of this invention.
Claims
1. A method for automatically and securely operating Linux symbolic links, characterized in that, The method includes: S01. To obtain the Linux symbolic link keyword, use find to locate the path containing the keyword, remove the keyword part at the end of the path and store it in list1. S02. Loop through list1 and use ls -li loop variable | grep -e '->' to get the path that matches the result of the Linux symbolic link keyword, and concatenate it with the Linux symbolic link keyword and store it in a new list list2; S03. Loop through list2 to safely delete the Linux symbolic link. After safely deleting the Linux symbolic link, you can create a new Linux symbolic link according to the new directory path.
2. The method for automatically and securely operating Linux symbolic links according to claim 1, characterized in that, In S02, the ls command in the ls-li loop variable lists the files or folders in the current directory. Adding |grep -e '->' will filter and only display folders or files containing '->', and the displayed files or folders will be symbolic links; those not containing '->' will return an empty value.
3. The method for automatically and securely operating Linux symbolic links according to claim 2, characterized in that, The S02 includes: S021. If the result of executing `ls -li loop variable | grep -e '->'` is empty, then the Linux connection method is not a symbolic link. S022. If the result of `ls -li loop variable | grep -e '->'` is not empty, then the Linux connection method is a symbolic link.
4. A device for automatically and securely operating Linux symbolic links, characterized in that, The device includes: The list1 module is created by obtaining the Linux symbolic link keyword, using the find method to locate the path containing the keyword, removing the keyword part from the end of the path and storing it in the list1; The list2 module is created, and the list1 list is looped through. The variable is obtained by using ls -li | grep -e '->'. The path that is consistent with the result of the Linux symbolic link keyword is obtained and concatenated with the Linux symbolic link keyword and stored in the new list list2. The symbolic link deletion module, by looping through the list list2, can safely delete Linux symbolic links. After safely deleting a Linux symbolic link, you can create a new Linux symbolic link according to the new directory path.
5. The apparatus for automatically and securely operating Linux soft links according to claim 4, characterized in that, In the `list2` creation module, the `ls` command in the `ls-li` loop variable lists the files or folders in the current directory. Adding `|grep -e '->'` will filter and only display folders or files containing '->', and the displayed files or folders will be symbolic links; those not containing '->' will return an empty value.
6. The apparatus for automatically and securely operating Linux soft links according to claim 5, characterized in that, The list2 creation module includes: If a non-soft link module or the `ls -li` loop variable `|grep -e '->'` returns an empty value, then the Linux linking method is not a soft link. If the symbolic link module and the ls-li loop variable | grep-e '->' result are not empty, then the Linux connection method is a symbolic link.
7. A computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the method according to any one of claims 1-3.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that performs the method according to any one of claims 1-3.