script to delete files in special directory

samaus
New Contributor

The Mac sometimes cannot connect to a wifi network even though there is no profile showing for that particular network.  A search on the web seems to suggest that the following files need to be deleted from /Library/Preferences/SystemConfiguration/:

com.apple.airport.preferences.plist
com.apple.network.eapolclient.configuration.plist
com.apple.wifi.message-tracer.plist
NetworkInterfaces.plist
preferences.plist

I just wonder if there could be a script to do this.  I tried using a bash script but always has "no such directory"  "no permission" etc errors.

Any help would be appreciated.

1 ACCEPTED SOLUTION

AVmcclint
Honored Contributor

Click on the link I posted. It will take you directly to the current release which works very well in all the latest releases including Monterey.

View solution in original post

14 REPLIES 14

AVmcclint
Honored Contributor

You have to be careful deleting files pertaining to the network configuration while performing actions that come over the network. I've often found that difficulty connecting to networks (wifi or ethernet) can be caused by too many remembered SSIDs. Take a look at this for one possible avenue.  That being said, if you post the script you're trying to use, we can take a look to determine what might be askew.

Hi AVmcclint

I have this problem on several macs at different times and each time I managed to solve the problem by deleting the files.  It just tedious to do it manually.  Here is one of the script I tried:

#!/bin/bash
sudo chmod -R 755 /Library/Preferences/SystemConfiguration
cd /Library/Preferences/SystemConfiguration
rm -f 'com.apple.airport.preferences.plist'
rm -f 'com.apple.network.eapolclient.configuration.plist'
rm -f 'com.apple.wifi.message-tracer.plist'
rm -f 'NetworkInterfaces.plist'
rm -f 'preferences.plist'

and here is the error message:

mac script error.jpg

Other things that I've tried:

using the full path in the rm statements

use sudo chown before chmod.

not much success either.

Thanks.

AVmcclint
Honored Contributor

It does appear that the com.apple.Boot.plist file is probably protected by SIP (I checked on my Macs too). If that's the case, there is nothing you can do to delete SIP protected files other than disable SIP (which is not a good idea). It does look like the other files are free from SIP protection.  A better way to do your script would be to skip the chmod and chown altogether. If all you're doing is running the script as sudo with "rm filename" commands in the script,  then chmod and chown are irrelevant. If you only focus on deleting the specific files you want to remove, that will avoid the error you're seeing.

Hi AVmcclint

I've modified the script to 

#!/bin/bash
sudo rm -f '/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist'
sudo rm -f '/Library/Preferences/SystemConfiguration/com.apple.network.eapolclient.configuration.plist'
sudo rm -f '/Library/Preferences/SystemConfiguration/com.apple.wifi.message-tracer.plist'
sudo rm -f '/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist'
sudo rm -f '/Library/Preferences/SystemConfiguration/preferences.plist'

 

Now there is no error message.  However, the files are not deleted either.

 

Thanks.

AVmcclint
Honored Contributor

You don't need to put them in quotes. If I were to do it, I'd make it like this:

#!/bin/bash
rm -f /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist
rm -f /Library/Preferences/SystemConfiguration/com.apple.network.eapolclient.configuration.plist
rm -f /Library/Preferences/SystemConfiguration/com.apple.wifi.message-tracer.plist
rm -f /Library/Preferences/SystemConfiguration/NetworkInterfaces.plist
rm -f /Library/Preferences/SystemConfiguration/preferences.plist

and then run the script as a whole with a sudo command:

sudo scriptname.sh

 

Hi AVmcclint

Thanks again.  When I type at the terminal on the usb drive (ICT02):

sudo rmwifi1.sh 

It said: sudo: rmwifi1.sh: command not found

when I type:

sudo ICT02/rmwifi1.sh

It said sudo: ICT02/rmwifi1.sh: command not found

AVmcclint
Honored Contributor

My apologies. I should have been more clear. You should run: 

sudo /path/to/scriptname.sh

 The easiest way to do that is to type sudo (and a space) then just drag the script file into the Terminal window so there is no doubt as to what you want to run.

Hi AVmcclint

when I do as you suggested, now it said:

sudo: unable to execute /Volumes/ICT02/rmwifi1.sh: Operation not permitted

when I ls -lO, it shows:

-rwxrwxtwx@

 

AVmcclint
Honored Contributor

Try copying the script to the desktop and running it from there.

rmwifi.jpg

 Hi AVmcclint, just another bunch of errors.  The last one does not have an error but the files are not deleted either.

AVmcclint
Honored Contributor

What program did you use to edit the script? Rule of thumb: Never use Apple's TextEdit to work on scripts. Use something like TextMate. I especially like TextMate because when you save scripts, it automatically sets the execute bit.

Hi AVmcclint

I've looked up TextMate but it said it is not suitable for OSX 10.9 or later.  I am using 11.2.3 so no luck.  The script was created using TextEdit but I am using the plain text mode, not rich text

Thanks again

AVmcclint
Honored Contributor

Click on the link I posted. It will take you directly to the current release which works very well in all the latest releases including Monterey.

Hi AVmcclint

Thank you for all your help.  This works.