删除包含符号链接的linux中的目录

时间:2012-12-18 01:18:25

标签: linux shell

我想在linux中删除一个非空目录,其中包含一系列符号链接的目录结构。我跑的时候

rm -rf /direcory

它不仅删除了所有符号链接和目录,还删除了这些符号链接指向的所有目录。是否只有一个包含符号链接的目录的命令?

1 个答案:

答案 0 :(得分:2)

反示例:

$ mkdir directory
$ cd directory
$ mkdir to-be-removed to-be-left-behind
$ cd to-be-left-behind
$ mkdir a b c
$ cd ../to-be-removed
$ ln -s ../to-be-left-behind/* .
$ cd ..
$ ls -lR
total 0
drwxr-xr-x  5 jleffler  staff  170 Dec 17 17:25 to-be-left-behind
drwxr-xr-x  5 jleffler  staff  170 Dec 17 17:25 to-be-removed

./to-be-left-behind:
total 0
drwxr-xr-x  2 jleffler  staff  68 Dec 17 17:25 a
drwxr-xr-x  2 jleffler  staff  68 Dec 17 17:25 b
drwxr-xr-x  2 jleffler  staff  68 Dec 17 17:25 c

./to-be-left-behind/a:

./to-be-left-behind/b:

./to-be-left-behind/c:

./to-be-removed:
total 24
lrwxr-xr-x  1 jleffler  staff  22 Dec 17 17:25 a -> ../to-be-left-behind/a
lrwxr-xr-x  1 jleffler  staff  22 Dec 17 17:25 b -> ../to-be-left-behind/b
lrwxr-xr-x  1 jleffler  staff  22 Dec 17 17:25 c -> ../to-be-left-behind/c
$ rm -fr to-be-removed
$ ls -lR
total 0
drwxr-xr-x  5 jleffler  staff  170 Dec 17 17:25 to-be-left-behind

./to-be-left-behind:
total 0
drwxr-xr-x  2 jleffler  staff  68 Dec 17 17:25 a
drwxr-xr-x  2 jleffler  staff  68 Dec 17 17:25 b
drwxr-xr-x  2 jleffler  staff  68 Dec 17 17:25 c

./to-be-left-behind/a:

./to-be-left-behind/b:

./to-be-left-behind/c:
$

删除符号链接,但不删除它们指向的目录。

因此,您的示例中还有其他功能。例如,如果符号链接指向to-be-removed下的文件或目录,那么符号链接和目标都将被删除,但这只是因为目标都在火线中。

相关问题