If I have symbolic links in some subtree of my directories and decide to recursively removed a driectory and its contents, will that remove items symbolically linked in the tree?
Does rm -r follow symbolic links
bashunix
Related Solutions
Example 1: Deleting a directory containing a soft link to another directory.
susam@nifty:~/so$ mkdir foo bar
susam@nifty:~/so$ touch bar/a.txt
susam@nifty:~/so$ ln -s /home/susam/so/bar/ foo/baz
susam@nifty:~/so$ tree
.
├── bar
│ └── a.txt
└── foo
└── baz -> /home/susam/so/bar/
3 directories, 1 file
susam@nifty:~/so$ rm -r foo
susam@nifty:~/so$ tree
.
└── bar
└── a.txt
1 directory, 1 file
susam@nifty:~/so$
So, we see that the target of the soft-link survives.
Example 2: Deleting a soft link to a directory
susam@nifty:~/so$ ln -s /home/susam/so/bar baz
susam@nifty:~/so$ tree
.
├── bar
│ └── a.txt
└── baz -> /home/susam/so/bar
2 directories, 1 file
susam@nifty:~/so$ rm -r baz
susam@nifty:~/so$ tree
.
└── bar
└── a.txt
1 directory, 1 file
susam@nifty:~/so$
Only, the soft link is deleted. The target of the soft-link survives.
Example 3: Attempting to delete the target of a soft-link
susam@nifty:~/so$ ln -s /home/susam/so/bar baz
susam@nifty:~/so$ tree
.
├── bar
│ └── a.txt
└── baz -> /home/susam/so/bar
2 directories, 1 file
susam@nifty:~/so$ rm -r baz/
rm: cannot remove 'baz/': Not a directory
susam@nifty:~/so$ tree
.
├── bar
└── baz -> /home/susam/so/bar
2 directories, 0 files
The file in the target of the symbolic link does not survive.
The above experiments were done on a Debian GNU/Linux 9.0 (stretch) system.
To remove the links (from man find
under -type
):
l symbolic link; this is never true if the -L option or the
-follow option is in effect, unless the symbolic link is
broken. If you want to search for symbolic links when -L
is in effect, use -xtype.
I think this should do the trick:
find /usr/local/lib/ -maxdepth 1 -follow -type l
Does the output produce a list of the files you want to delete? If so, when you are 100% sure:
find /usr/local/lib/ -maxdepth 1 -follow -type l -delete
This will remove only broken links. To delete all links, remove the -follow
stanza, but I wouldn't do that under /usr/local/lib
.
Best Answer
From the man file
Version OS X 10.8.2
Look this SuperUser question.