moo*_*765 34
您可以使用find- 命令来执行此操作:
find /path/to/directory -maxdepth 1 -type l -delete
Run Code Online (Sandbox Code Playgroud)
为了安全起见,首先检查不带-delete- 选项:
find /path/to/directory -maxdepth 1 -type l
Run Code Online (Sandbox Code Playgroud)
-maxdepth 1确保find只在其子文件夹中查找/path/to/directory而不是在其子文件夹中查找符号链接。随意看一下man find。
sud*_*dus 14
列出当前目录别名文件夹中的链接并检查您是否确实要删除它们,
find -type l -ls # search also in subdirectories
find -maxdepth 1 -type l -ls # search only in the directory itself
Run Code Online (Sandbox Code Playgroud)
如果情况看起来不错,并且您想删除这些链接,请运行
find -type l -delete # delete also in subdirectories
find -maxdepth 1 -type l -delete # delete only in the directory itself
Run Code Online (Sandbox Code Playgroud)
如果你想交互式删除,可以使用下面的命令行(这样比较安全)
find -type l -exec rm -i {} + # delete also in subdirectories
find -maxdepth 1 -type l -exec rm -i {} + # delete only in the directory itself
Run Code Online (Sandbox Code Playgroud)
mar*_*elm 10
rm *(@)将会实现这一点。Zsh 支持glob 限定符,这些限定符限制 glob(例如*)适用的文件类型,例如(/)目录、(x)可执行文件、(L0)空文件和(@)符号链接。
对于符号链接:
% ll
lrwxrwxrwx 1 test test 3 Aug 8 15:51 bar -> foo
-rw-r--r-- 1 test test 0 Aug 8 15:51 baz
-rw-r--r-- 1 test test 0 Aug 8 15:52 foo
lrwxrwxrwx 1 test test 4 Aug 8 15:51 qux -> /etc/
% rm *(@)
removed 'bar'
removed 'qux'
% ll
-rw-r--r-- 1 test test 0 Aug 8 15:51 baz
-rw-r--r-- 1 test test 0 Aug 8 15:52 foo
Run Code Online (Sandbox Code Playgroud)
在bash(和大多数 shell)中 \xe2\x80\xa6 内置命令test及其变体[有一个选项-h(或者-L如果它\xe2\x80\x99s更容易记住)将返回成功(exit 0 )符号链接 \xe2\x80\xa6 因此它可以在 shell 循环中使用,如下所示:
for f in *\n do\n if [ -h "$f" ]\n then \n echo rm -- "$f"\n fi\ndone\nRun Code Online (Sandbox Code Playgroud)\n或者像这样的单行:
\nfor f in *; do if [ -h "$f" ]; then echo rm -- "$f"; fi done\nRun Code Online (Sandbox Code Playgroud)\n甚至更紧凑( bash 特定的 \xe2\x80\xa6 尽管据报道也可以在zsh 和 ksh中工作所示:
\nfor f in *; { [ -h "$f" ] && echo rm -- "$f"; }\nRun Code Online (Sandbox Code Playgroud)\n注意:
\necho是否可以进行空运行...当对输出感到满意时,删除echo以删除链接。