好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Linux下链接文件使用RM无法删除的解决办法 - lin

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]$ df -hl

    关于选项-h,在ls等等的命令中都有,具体的含义是一致的,如下:

    -h, --human-readable

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

    从上面的显示,可以看出,如果不加-h,那么大小是以字节显示的,如果加入-h,那么就以很明显的K,或者M来显示,也就明确的多了。

    查看更多关于Linux下链接文件使用RM无法删除的解决办法 - lin的详细内容...

  •   阅读:68次