Find and delete all symlinks in home folder, having trouble making it work

大兔子大兔子 提交于 2019-12-21 19:19:01

问题


I have the following:

read -p 'Delete old symlinks? y/n: ' prompt

if [ $prompt == [yY] ]
then    
    current_dir=$(pwd)
    script_dir=$(dirname $0)

    if [ $script_dir = '.' ]
    then
        script_dir="$current_dir"
    fi

    for sym in {find $PATH -type l -xtype d -lname "~"}
    do
        rm $sym
        echo "Removed: $sym"
    done
fi

What I'm trying to achieve is the following:

  • If prompt equals y (yes) Then: Find all the symlinks in ~ (home directory) and do a for loop on them which would delete them and output which one it deleted.

Although right now it doesn't do the deleting part, the rest works fine. I didn't include the part that works, it's just a bunch of ln -s.

I am in Mac OS X Mountain Lion (10.8.2), using Zsh as shell, this goes in a .sh file.

The problem I am having is that nothing happens, and so it just says:

ln: /Users/eduan/.vim/vim: File exists
ln: /Users/eduan/.vimrc: File exists
ln: /Users/eduan/.gvimrc: File exists
Done...

etc.

Many thanks for any help you can provide!

EDIT:
I now have the following:

read -p 'Delete old symlinks? y/n: ' prompt

if [ $prompt == [yY] ]
then    
   find ~ -type l | while read line
   do
      rm -v "$line"
   done
fi

回答1:


find /path -type l | xargs rm

The first part lists the symlinks. Xargs says for each of the returned values issue an rm




回答2:


How about this?

find ~ -type l | while read line
do
   rm -v "$line"
done



回答3:


I was able to solve the problem, turns out it was the if conditional that wasn't working. Thanks for your help anyway guys! :)



来源:https://stackoverflow.com/questions/13484825/find-and-delete-all-symlinks-in-home-folder-having-trouble-making-it-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!