linux下怎么删除链接文件,Linux下链接文件使用RM无法删除的解决办法
发布日期:2025-04-08 00:15:46 浏览次数:7 分类:精选文章

本文共 4108 字,大约阅读时间需要 13 分钟。

http://www.xici.net/main.asp?url=/u13287071/d78561671.htm

在进行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

# 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 "  " $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)

上一篇:Linux下怎样检查某软件包是否安装
下一篇:Linux下彻底卸载mysql详细分析

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年05月18日 09时07分59秒