Solution to the failure to delete linked files using RM in Linux

Source: Internet
Author: User
Tags uncompress
Solution to the problem that RM cannot be deleted in Linux-Linux general technology-Linux programming and kernel information. For details, refer to the following section. A small problem occurs during U-boot development. The netizen wanglida79 met me a few days ago. I didn't simulate it at the time. Now I have met myself. However, I have come up with a solution, but the reason is not clear. Maybe the method is incorrect or there may be bugs.

Symptom description:
For the convenience of patch, the source code is named. orig. However, the name is too long and it is not convenient to operate in the command line, so the idea is to create a soft link.

[Armlinux @ lqm bootloader] $ tree-L 1
.
| -- Patch
| -- U-boot-1.1.3
| -- U-boot-1.2.0
| -- U-boot-1.2.0.orig
| -- Vivi
'-- Vivi_origin

6 directories, 0 files

The above is the main folder under the directory. Now, link the source code to orig and link the development part to develop.

[Armlinux @ lqm bootloader] $ ln-s u-boot-1.2.0.orig/orig
[Armlinux @ lqm bootloader] $ ln-s u-boot-1.2.0 develop
[Armlinux @ lqm bootloader] $ ls
Develop orig patch u-boot-1.1.3 u-boot-1.2.0 u-boot-1.2.0.orig vivi vivi_origin

As shown above. Now you want to delete develop and orig. Unexpected situation:

[Armlinux @ lqm bootloader] $ rm develop/
Rm: cannot remove 'develop/': Not a directory
[Armlinux @ lqm bootloader] $ rm-f develop/
Rm: cannot remove 'develop/': Not a directory
[Armlinux @ lqm bootloader] $ unlink develop/
Unlink: cannot unlink 'develop/

It seems that it cannot be deleted. The same is true for deleting orig. After an experiment, find is used to delete the file:

[Armlinux @ lqm bootloader] $ find.-type l | xargs rm-f
[Armlinux @ lqm bootloader] $ ls
Patch u-boot-1.1.3 u-boot-1.2.0 u-boot-1.2.0.orig vivi vivi_origin

It seems to be successful.

Symptom Analysis and Solution:
The preceding find and xargs deletion methods can be implemented. But why can't I delete it with only rm. I think there is a problem with the method used. Check the usage of rm and ln. The use of ln and rm are correct after man's check. I tried to find out the reason from the difference between rm direct deletion and find deletion.

[Armlinux @ lqm bootloader] $ find.-type l
./Develop
./Orig

It seems that the cause is found. I always use the TAB key to complete the command when using rm, but the TAB completion command ends. The obvious reason is that rm or unlink cannot handle this situation well. This is a bug. When I wrote a shell script to implement autozip, I encountered this problem and used awk to solve it. The original script is as follows:

[Armlinux @ lqm bin] $ cat autozip
#! /Bin/bash
# Copyright 2007 (c), Shandong University
# All rights reserved.
#
# Filename: autozip
# Description: Compress files, and print "OK" out if the file
# Can be compressed successfully.
# Syntax: autozip [filename | directory name]
# Author: Liu Qingmin
# Version: 1.0
# Date: 07-04-29
#

# Func: get_target ()
# Desc: Obtain the name of target file
# Para: $1 -- file name that will be compressed
# Ret: TARGET -- current file name
Get_target ()
{
TARGET = 'echo $1 | \
Awk-F/'{if ($ NF = "") print $(NF-1 );\
Else print $ (NF )}''
}

# Handle Parameters
If [$ #! = 1]; then
Echo "Usage: 'basename $0 '"
Exit 1
Fi

# Assign the parameter to the Macro OPT
OPT = $1

# Uncompress files
If [-d $ OPT]; then
Get_target $ OPT
Tar zcvf implements target).tar.gz $ OPT & echo "OK"
Elif [-f $ OPT]; then
Get_target $ OPT
Cp $ OPT tmp
Gzip tmp
Cp tmp.gz running successfully target).gz
Rm tmp.gz
If [-x ${target).gz]; then
Chmod-x specify target).gz
Fi
Echo "OK"
Fi

The above get_target is used to handle this situation. However, rm cannot handle this situation without thinking about it. You must know that using the TAB key to improve efficiency is a frequently used method.

After finding the bug and looking at the source code of rm, we can use the above script to solve this small bug. A script named rmlink is written as follows:

[Armlinux @ lqm bin] $ cat rmlink
#! /Bin/sh
# Copyright 2007 (c), Shandong University
# All rights reserved.
#
# Filename: rmlink
# Description: solve the bug of "rm" and "unlink"
# Syntax: rmlink
# Author: Liu Qingmin
# Version: 1.0
# Date: 07-09-19
#

# Func: get_target ()
# Desc: Obtain the name of target file
# Para: $1 -- file name that will be compressed
# Ret: TARGET -- current file name
Get_target ()
{
TARGET = 'echo $1 | \
Awk-F/'{if ($ NF = "") print $(NF-1 );\
Else print $ (NF )}''
}

# Handle Parameters
If [$ #! = 1]; then
Echo "Usage: 'basename $0 '"
Exit 1
Fi

# Assign the parameter to the Macro OPT
OPT = $1

# Uncompress files
If [-d $ OPT]; then
# Eliminate the "/" at the ending
Get_target $ OPT
# You also can use "unlink" instead of "rm"
Rm $ {TARGET}
Fi

# OK
Exit 0

Test:

[Armlinux @ lqm bootloader] $ ls
Develop orig patch u-boot-1.1.3 u-boot-1.2.0 u-boot-1.2.0.orig vivi vivi_origin
[Armlinux @ lqm bootloader] $ rmlink develop
[Armlinux @ lqm bootloader] $ rmlink orig
[Armlinux @ lqm bootloader] $ ls
Patch u-boot-1.1.3 u-boot-1.2.0 u-boot-1.2.0.orig vivi vivi_origin

It can be seen that the test is normal and rmlink can be used normally.

So far, the problem is finally solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.