Skip to main content
Answer

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

  • October 14, 2014
  • 7 replies
  • 40 views

rqomsiya
Forum|alt.badge.img+12

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!!!

Best answer by Chris

#!/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 ;)

7 replies

Forum|alt.badge.img+18
  • Valued Contributor
  • October 14, 2014

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


rqomsiya
Forum|alt.badge.img+12
  • Author
  • Honored Contributor
  • October 14, 2014

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


rqomsiya
Forum|alt.badge.img+12
  • Author
  • Honored Contributor
  • October 14, 2014

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


Forum|alt.badge.img+18
  • Valued Contributor
  • October 14, 2014

networksetup -help is still your friend


Forum|alt.badge.img+13
  • Valued Contributor
  • Answer
  • October 14, 2014
#!/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
Forum|alt.badge.img+12
  • Author
  • Honored Contributor
  • October 14, 2014

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
Forum|alt.badge.img+12
  • Author
  • Honored Contributor
  • October 14, 2014

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