Linux下链接文件使用RM无法删除的处理办法

 起原:网海拾贝




在中止U-boot启示的时分,遇到一个小成就。网友wanglida79前几天刚遇到过,我事前没有仿照出来,而今本人却是遇上了。不外我想出认识决的办法,只不外启事不理解答理,概略使用方式错误,概略有bug。  
  情形描绘:
 
  我中止U-boot移植的启示,为了patch苟且,将源码的名字定名为.orig,多么以示区分。但是名字太长,鄙人令行下操纵不太苟且,以是设法主意便是设立创建软链接。
 

[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



 
    上面是目录下的严厉文件夹。而今将源码链接为orig,将启示部分链接为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



 
  如上。而今想要删除develop和orig。泛起不测形态:
 

[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/



 
  看来删不失。删除orig也异样如此。转念又检验测验了使用find来删除:
 

[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



 
  看来可以成功。
 
  情形阐发与处理:
 
  上面提供的find and xargs的删除方式可以完成。但是只用rm为什么不克不及删除呢。我想应该是使用的方式上有成就,必需查阅rm和ln的用法。颠末man查阅,ln的使用和rm的使用并没有成就。****了前面的设法主意,我想从rm间接删除和find删除的差异入手找到启事。
 

[armlinux@lqm bootloader] $ find . -type l
./develop
./orig



 
  看来启事找到了。我内使用rm的时分总是风气使用TAB键补全下令,但是TAB补全下令的时分,末了因而“/”末端的。很大白的启事,rm也好,unlink也好,并不克不及很好的处理处罚这种形态,这算是一处bug。我在前面写shell脚原来完成autozip时的时分,本人遇到过这个成就,采取了awk处理。原有的脚本如下:
 

[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  ${TARGET}.tar.gz  $OPT && echo "OK"
elif [ -f  $OPT ]; then
        get_target  $OPT
        cp  $OPT tmp
        gzip tmp
        cp tmp.gz  ${TARGET}.gz
        rm tmp.gz
        if [ -x  ${TARGET}.gz ]; then
                chmod -x  ${TARGET}.gz
        fi
        echo "OK"
fi



 
  上面的get_target便是对这个形态的处理处罚。不外没有想到rm也无法处理处罚这种形态,要晓得,使用TAB键前进功效是每每用的本领啊。
 
  找到了bug,还没有看rm的源代码,却是可以使用上面的脚本的思绪来处理这个小bug。写了一个脚本rmlink,如下:
 

[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 <linkfile name>
# 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



 
  测试:
 

[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



 
    可见测试正常,rmlink可以正常使用。
 
    至此,成就最终处理。
 
  附:
 
  vmware瓦解,招致体恤查阅磁盘空间和文件巨细。而今附几个常用的小下令,备查阅。
 
    ·反省文件的巨细
 

[armlinux@lqm bootloader] $ ls -hl


 
    假定只想看到巨细,而不盼愿看到其他信息,可以使用上面的下令:
 

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


 
    ·反省单个目录占用空间的巨细
 

[armlinux@lqm bootloader] $ du -hs u-boot-1.2.0
71M u-boot-1.2.0



 
    ·反省磁盘剩余空间的巨细
 

[armlinux@lqm bootloader] $ df -hl


 
    关于选项-h,在ls等等的下令中都有,详细的寄义是分比方的,如下:
 

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



 
  从上面的体现,可以看出,假定不加-h,那么巨细因而字节体现的,假定参与-h,那么就以很大白的K,或许M来体现,也就理解答理的多了。




版权声明: 原创作品,容许转载,转载时请务必以超链接形式标明文章 原始来由 、作者信息和本声明。否则将清查执法责任。

posted @ 2011-03-07 20:41  蓝色的天空III  阅读(665)  评论(0)    收藏  举报