Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

zsh

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
like image 399
greduan Avatar asked Dec 03 '22 01:12

greduan


2 Answers

find /path -type l | xargs rm

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

like image 200
cowboydan Avatar answered May 26 '23 19:05

cowboydan


How about this?

find ~ -type l | while read line
do
   rm -v "$line"
done
like image 37
n.r. Avatar answered May 26 '23 19:05

n.r.