Need to delete all network locations except for two (Script Help please)

rqomsiya
Contributor III

Hi all,

What would be the line/commands to delete all network locations except for two (one named "Ethernet" and one named "Wi-Fi"). I want to delete any other network locations users may have except for two that i've create on their machines.

Thanks for your help forum!!!

1 ACCEPTED SOLUTION

Chris
Valued Contributor
#!/bin/sh

defaultIFS=$IFS

IFS=$'
'

for i in $(networksetup -listlocations | grep -v -e "Location 1" -e "Location2")

do

    echo "Removing location $i"
    networksetup -deletelocation "$i"

done

IFS=$defaultIFS

quick & dirty, so test, test, test ;)

View solution in original post

7 REPLIES 7

nessts
Valued Contributor II

networksetup help
look for listallnetworkservices then loop on the result. and delete what you don't want.

rqomsiya
Contributor III

I'm actually looking to write a command to add to a script...

rqomsiya
Contributor III

I don't want to edit the "networkservices", but want to delete actual "networklocations"

nessts
Valued Contributor II

networksetup -help is still your friend

Chris
Valued Contributor
#!/bin/sh

defaultIFS=$IFS

IFS=$'
'

for i in $(networksetup -listlocations | grep -v -e "Location 1" -e "Location2")

do

    echo "Removing location $i"
    networksetup -deletelocation "$i"

done

IFS=$defaultIFS

quick & dirty, so test, test, test ;)

rqomsiya
Contributor III

I've done that.. i know how to delete locations but don't know how to write an IF statement. I guess that what i need help with. I know the basic networksetup -deletelocation "locationname" and how to use it.

I just need help writing a command/statement that says if the network location name isn't (Wi-Fi or Ethernet) then delete it...

Sorry for the confusion...

rqomsiya
Contributor III

@Chris Thats exactly what i needed. Thank you for the quick response! I'll test and reply back..Thanks again!