歡迎您光臨本站 註冊首頁

Linux下鏈接文件使用RM無法刪除的解決辦法

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  

在進行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來顯示,也就明確的多了。
(責任編輯:A6)


[火星人 ] Linux下鏈接文件使用RM無法刪除的解決辦法已經有1130次圍觀

http://coctec.com/docs/linux/show-post-69551.html