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

Source: Internet
Author: User
Tags uncompress
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. But I figured out a solution.

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 for the development of U-boot porting. 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.

  1. [Armlinux @ lqm bootloader] $ tree-L 1
  2. .
  3. | -- Patch
  4. | -- U-boot-1.1.3
  5. | -- U-boot-1.2.0
  6. | -- U-boot-1.2.0.orig
  7. | -- Vivi
  8. '-- Vivi_origin
  9. 6 directories, 0 files

The above is the main folder under the Directory, now link the source code to orig, link the development part to develop.

  1. [Armlinux @ lqm bootloader] $ ln-s u-boot-1.2.0.orig/orig
  2. [Armlinux @ lqm bootloader] $ ln-s u-boot-1.2.0 develop
  3. [Armlinux @ lqm bootloader] $ ls
  4. 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, if you want to delete develop and orig, an exception occurs:

  1. [Armlinux @ lqm bootloader] $ rm develop/
  2. Rm: cannot remove 'develop/': Not a directory
  3. [Armlinux @ lqm bootloader] $ rm-f develop/
  4. Rm: cannot remove 'develop/': Not a directory
  5. [Armlinux @ lqm bootloader] $ unlink develop/
  6. Unlink: cannot unlink 'develop/

It seems that it cannot be deleted, and the same is true for deleting orig. the conversion experiment uses find to delete:

  1. [Armlinux @ lqm bootloader] $ find.-type l | xargs rm-f
  2. [Armlinux @ lqm bootloader] $ ls
  3. 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 deletion method of the find and xargs provided above can be implemented, but why can't it be deleted only by using rm? I think there should be a problem with the method used. I must check the usage of rm and ln, according to man's query, the use of ln is no problem with the use of rm, which overwrites the previous idea. I want to find the reason from the difference between rm direct deletion and find deletion.

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

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

  1. [Armlinux @ lqm bin] $ cat autozip
  2. #! /Bin/bash
  3. # Copyright 2007 (c), Shandong University
  4. # All rights reserved.
  5. #
  6. # Filename: autozip
  7. # Description: Compress files, and print "OK" out if the file
  8. # Can be compressed successfully.
  9. # Syntax: autozip [filename | directory name]
  10. # Author: Liu Qingmin
  11. # Version: 1.0
  12. # Date: 07-04-29
  13. #
  14. # Func: get_target ()
  15. # Desc: Obtain the name of target file
  16. # Para: $1 -- file name that will be compressed
  17. # Ret: TARGET -- current file name
  18. Get_target ()
  19. {
  20. TARGET = 'echo $1 |
  21. Awk-F/'{if ($ NF = "") print $(NF-1 );
  22. Else print $ (NF )}''
  23. }
  24. # Handle Parameters
  25. If [$ #! = 1]; then
  26. Echo "Usage: 'basename $0 '"
  27. Exit 1
  28. Fi
  29. # Assign the parameter to the Macro OPT
  30. OPT = $1
  31. # Uncompress files
  32. If [-d $ OPT]; then
  33. Get_target $ OPT
  34. Tar zcvf implements target).tar.gz $ OPT & echo "OK"
  35. Elif [-f $ OPT]; then
  36. Get_target $ OPT
  37. Cp $ OPT tmp
  38. Gzip tmp
  39. Cp tmp.gz running successfully target).gz
  40. Rm tmp.gz
  41. If [-x ${target).gz]; then
  42. Chmod-x specify target).gz
  43. Fi
  44. Echo "OK"
  45. Fi

The above get_target is used to handle this situation, but it is not possible for rm to handle this situation. you must know that it is often used to improve efficiency by using the TAB key.

After finding the bug and looking at the rm source code, we can use the above script idea to solve this small bug and write a script rmlink, as shown below:

  1. [Armlinux @ lqm bin] $ cat rmlink
  2. #! /Bin/sh
  3. # Copyright 2007 (c), Shandong University
  4. # All rights reserved.
  5. #
  6. # Filename: rmlink
  7. # Description: solve the bug of "rm" and "unlink"
  8. # Syntax: rmlink
  9. # Author: Liu Qingmin
  10. # Version: 1.0
  11. # Date: 07-09-19
  12. #
  13. # Func: get_target ()
  14. # Desc: Obtain the name of target file
  15. # Para: $1 -- file name that will be compressed
  16. # Ret: TARGET -- current file name
  17. Get_target ()
  18. {
  19. TARGET = 'echo $1 |
  20. Awk-F/'{if ($ NF = "") print $(NF-1 );
  21. Else print $ (NF )}''
  22. }
  23. # Handle Parameters
  24. If [$ #! = 1]; then
  25. Echo "Usage: 'basename $0 '"
  26. Exit 1
  27. Fi
  28. # Assign the parameter to the Macro OPT
  29. OPT = $1
  30. # Uncompress files
  31. If [-d $ OPT]; then
  32. # Eliminate the "/" at the ending
  33. Get_target $ OPT
  34. # You also can use "unlink" instead of "rm"
  35. Rm $ {TARGET}
  36. Fi
  37. # OK
  38. Exit 0
  39. // Test:
  40. [Armlinux @ lqm bootloader] $ ls
  41. Develop orig patch u-boot-1.1.3 u-boot-1.2.0 u-boot-1.2.0.orig vivi vivi_origin
  42. [Armlinux @ lqm bootloader] $ rmlink develop
  43. [Armlinux @ lqm bootloader] $ rmlink orig
  44. [Armlinux @ lqm bootloader] $ ls
  45. 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. at this point, the problem is finally solved.

Appendix:Vmware crashes, causing concern about viewing disk space and file size. now we have a few frequently used commands for secondary check.

· View the file size

[Armlinux @ lqm bootloader] $ ls-hl

· If you only want to see the size but not other information, run the following command:

[Armlinux @ lqm bootloader] $ ls-hl | awk '{print $5 "t" $ NF }'

· View the space occupied by a single directory

[Armlinux @ lqm bootloader] $ du-hs u-boot-1.2.0

U-boot-1.2.0 71 M

· View the remaining disk space

[Armlinux @ lqm bootloader] $ df-hl

Options-h are available in ls and other commands. the specific meanings are the same, as shown below:

-H, -- human-readable

With-l, print sizes in human readable format (e.g., 1 K 234 M 2G)

As shown in the preceding figure, if-h is not added, the size is displayed in bytes. if-h is added, it is displayed in K or M, it is much clearer.

Related Article

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.