how to script folder deletion on MAC - macos

I believe it should be straight forward but either I am having a bad day or I simply can't find what I am looking for.
Please help.
I need to run following commands in MAC Terminal in order to get rid of following entries:
sudo rm -Rf /Applications/Network\ Connect.app
sudo rm -Rf /Library/Frameworks/net.juniper.DSApplicationServices.framework
sudo rm -Rf /Library/Frameworks/net.juniper.DSCoreServices.framework
sudo rm -Rf /Library/Frameworks/net.juniper.DSNetworkDiagnostics.framework
sudo rm -Rf /Library/Internet\ Plug-ins/net.juniper.DSSafariExtensions.plugin
sudo rm -Rf /Library/Widgets/Network\ Connect.wdgt
sudo rm -Rf /usr/local/juniper
sudo rm -Rf /private/var/db/receipts/net.juniper.NetworkConnect.bom
sudo rm -Rf /private/var/db/receipts/net.juniper.NetworkConnect.plist
sudo rm -Rf ~/Library/Preferences/ncproxyd.plist
It does it's job but it's not exactly elegant. I was also thinking about providing this to my colleagues so I wanted to create some sort of .bat file for MAC.
I really spent about half day trying to figure it out but it doesn't work :(
Can somebody help me to create a .sh file or bash file which will do execute the commands above?

Create a script file, let's say it's called deletion.sh and add the lines: -
#!/bin/bash
rm -Rf /Applications/Network\ Connect.app
rm -Rf /Library/Frameworks/net.juniper.DSApplicationServices.framework
rm -Rf /Library/Frameworks/net.juniper.DSCoreServices.framework
rm -Rf /Library/Frameworks/net.juniper.DSNetworkDiagnostics.framework
rm -Rf /Library/Internet\ Plug-ins/net.juniper.DSSafariExtensions.plugin
rm -Rf /Library/Widgets/Network\ Connect.wdgt
rm -Rf /usr/local/juniper
rm -Rf /private/var/db/receipts/net.juniper.NetworkConnect.bom
rm -Rf /private/var/db/receipts/net.juniper.NetworkConnect.plist
rm -Rf ~/Library/Preferences/ncproxyd.plist
Then, in terminal you need to set the executable flag to the script: -
chmod +x deletion.sh
Note that the executable flag may be removed when the script is copied to another machine or network drive, so you may have to do that after copying.
Finally, you can call the script with sudo
sudo ./deletion.sh

If you want to create a batch file, you have one. That list of commands is your shell script. To execute it, just save hem into a file add the bash command to the front of that file's name:
$ bash commands_I_want_to_execute.txt
If you want to get fancy, you can put a shebang on the top and set the execution bit using chmod. That will make your script a real shell script.
However, in order for your shell script to be found, you need to either prefix it with a path, or put it in a directory that's included in your PATH. Here, I'll just prefix it:
$ chmod a+x commands_I_want_to_execute.txt # Suffix doesn't really matter. It's executable
$ ./commands_I_want_to_execute.txt # Now this will be executed
If you are really bothered by the suffix, change it with the mv command:
$ mv commands_I_want_to_execute.txt commands_I_want_to_execute.sh
NOTE: If you create a file with Text Edit, create it as a plain text file and not as a RTF file.

Related

How to delete contents of user home dir safely via bash

I am writing a bash script to do a account restore. The contents of the home dir is zipped up using this command.
sudo sh -c "cd /home/$username; zip -0 -FS -r -b /tmp /home/0-backup/users/$username.zip ."
This works as expected.
If the user requests a restore of their data, I am doing the following
sudo sh -c "cd /home/$username; rm -rf *"
Then
sudo -u $username unzip /home/0-backup/users/$username.zip -d /home/$username/
This works as expected.
However you can see the flaw in the delete statement, if the username is not set. We delete all users home dir. I have if statements that do the checking to make sure the username is there. I am looking for some advice on a better way to handle resetting the users account data that isn't so dangerous.
One thought I had was to delete the user account and then recreate it. Then do the restore. I think that this would be less risky. I am open to any suggestions.
Check the parameters first.
Then use && after cd so that it won't execute rm if the cd fails.
if [ -n "$username" ] && [ -d "/home/$username" ]
then
sudo sh -c "cd '/home/$username' && rm -rf * .[^.]*"
fi
I added .[^.]* in the rm command so it will delete dot-files as well. [^.] is needed to prevent it from deleting . (the user's directory) and .. (the /home directory).

Can't remove node module file in mac

When i try run this command
rm -rf node_modules
I get this error:
rm: node_modules/core-js/core/function.js: Illegal byte sequence
how can i fix it ?
Looks like theres some encoding issues with this file. You can do:
LC_ALL=C rm -rf node_modules
Or you can try setting these two vars before you run your rm command.
export LC_CTYPE=C
export LANG=C
You can also add this in your .bashrc file but it will cause issues in other places.

Downloading and automatically installing a tgz file

#!/bin/bash
mkdir /tmp
curl -O http://www.mucommander.com/download/nightly/mucommander-current.app.tar.gz /tmp/mucommander.tgz
tar -xvzf /tmp/mucommander.tgz */mucommander.app/*
cp -r /tmp/mucommander.app /Applications
rm -r /tmp
I'm trying to create a shell script to download and extract muCommander to my applications directory on a Mac.
I tried cd into the tmp dir, but then the script stops when I do that.
I can extract all using the -C argument, but the current tgz path is muCommander-0_9_0/mucommander.app, which could change on later builds, so I'm trying to keep it generic.
Can anyone give me pointers where I'm going wrong?
Thanks in advance.
Strip the first path component when you untar the archive, from tar(1):
--strip-components count
(x mode only) Remove the specified number of leading path ele-
ments. Pathnames with fewer elements will be silently skipped.
Note that the pathname is edited after checking inclusion/exclu-
sion patterns but before security checks.
Update
Here is a working bash example of how to, fairly generically, copy the contents of the tgz file to /Applications.
shopt -s nocaseglob
TMPDIR=/tmp
APP=mucommander
TMPAPPDIR=$TMPDIR/$APP
mkdir -p $TMPAPPDIR
curl -o $TMPDIR/$APP.tgz http://www.mucommander.com/download/nightly/mucommander-current.app.tar.gz
tar --strip-components=1 -xvzf $APP.tgz -C $TMPAPPDIR
mv $TMPAPPDIR/${APP}* /Applications
# rm -rf $TMPAPPDIR $TMPDIR/$APP
The rm command is commented out for now, verify that it does no harm before you use it.
The following will update your muCommander.
#for the safety, remove old temporary extraction from the /tmp
rm -rf /tmp/muCommander.app
#kill the running mucommander - you dont want replace the runnung app
ps -ef | grep ' /Applications/muCommander.app/' | grep -v grep | awk '{print $2}' | xargs kill
#download, extract, remove old, move new, open
#each command run only when the previous ended with success
curl http://www.mucommander.com/download/nightly/mucommander-current.app.tar.gz |\
tar -xzf - -C /tmp --strip-components=1 '*/muCommander.app' && \
rm -rf /Applications/muCommander.app && \
mv /tmp/muCommander.app /Applications && \
open /Applications/muCommander.app
Beware, after the '\' must following new line, and not any spaces...

How to uninstall Jenkins?

This is probably very simple, but I can't find any hint anywhere. So how one is supposed to do that, in general and specifically on Mac?
These instructions apply if you installed using the official Jenkins Mac installer from http://jenkins-ci.org/
Execute uninstall script from terminal:
'/Library/Application Support/Jenkins/Uninstall.command'
or use Finder to navigate into that folder and double-click on Uninstall.command.
Finally delete last configuration bits which might have been forgotten:
sudo rm -rf /var/root/.jenkins ~/.jenkins
If the uninstallation script cannot be found (older Jenkins version), use following commands:
sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
sudo rm /Library/LaunchDaemons/org.jenkins-ci.plist
sudo rm -rf /Applications/Jenkins "/Library/Application Support/Jenkins" /Library/Documentation/Jenkins
and if you want to get rid of all the jobs and builds:
sudo rm -rf /Users/Shared/Jenkins
and to delete the jenkins user and group (if you chose to use them):
sudo dscl . -delete /Users/jenkins
sudo dscl . -delete /Groups/jenkins
These commands are also invoked by the uninstall script in newer Jenkins versions, and should be executed too:
sudo rm -f /etc/newsyslog.d/jenkins.conf
pkgutil --pkgs | grep 'org\.jenkins-ci\.' | xargs -n 1 sudo pkgutil --forget
You are right, it is simple. Run (admin password required):
'/Library/Application Support/Jenkins/Uninstall.command'
It may be necessary to do this with admin privileges using sudo.
Keep in mind, that in Terminal you need to add backslash before space, so the proper copy/paste will be
/Library/Application\ Support/Jenkins/Uninstall.command
p.s. sorry for the late answer :)
run this on Terminal:
sh "/Library/Application Support/Jenkins/Uninstall.command"
There is no uninstaller. Therefore, you need to:
Delete the directory containing Jenkins (or, if you're deploying the war -- remove the war from your container).
Remove ~/.jenkins.
Remove you startup scripts.
My Jenkins version: 1.5.39
Execute steps:
Step 1. Go to folder /Library/Application Support/Jenkins
Step 2. Run Uninstall.command jenkins-runner.sh file.
Step 3. Check result.
It work for me.
Run the following commands to completely uninstall Jenkins from MacOS Sierra. You don't need to change anything, just run these commands.
sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
sudo rm /Library/LaunchDaemons/org.jenkins-ci.plist
sudo rm -rf /Applications/Jenkins '/Library/Application Support/Jenkins' /Library/Documentation/Jenkins
sudo rm -rf /Users/Shared/Jenkins
sudo rm -rf /var/log/jenkins
sudo rm -f /etc/newsyslog.d/jenkins.conf
sudo dscl . -delete /Users/jenkins
sudo dscl . -delete /Groups/jenkins
pkgutil --pkgs
grep 'org\.jenkins-ci\.'
xargs -n 1 sudo pkgutil --forget
Salam
Shah
On Mac; these two below commands completely remove Jenkins from your machine. just open your Terminal and execute them:
'/Library/Application Support/Jenkins/Uninstall.command' and
sudo rm -rf /var/root/.jenkins ~/.jenkins
Thanks

how to create AppleScript app to run a set of terminal commands

How would I go about creating an AppleScript command that when I just run the script (or double click it in Finder?), it would run a set of terminal commands? The set of commands completely remove MySQL, and it has become a pain to constantly write them out. The commands are:
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm /etc/my.cnf
There is also another command sudo nano /etc/hostconfig that opens a file and I need to delete a line from the file, but that seems like it'd be too hard to code, so I guess I can do that by hand. But it would be a huge help to do this automatically with a single script.
Would it just be a bunch of these commands?
do shell script (...)
Thanks,
Hristo
Yes, you would use do shell script.
However, for the commands where you execute as super user (sudo) you would instead use with administrator privileges. So, for sudo rm /usr/local/mysql you'd do:
do shell script "rm /usr/local/mysql" with administrator privileges
If your list of commands is long, then it might just be easier to put all your commands into a single shell script file, and then execute that shell script using do shell script:
do shell script "/path/to/shell/script" with administrator privileges
You don't actually need to use AppleScript for this - just put all the shell commands in a text file and give it a .command suffix and make sure it's executable (e.g. chmod +x my_script.command) - this will make it double-clickable in the Finder.
I find that .scpt files work best but this is just a preference thing.
Open "Script Editor" and add the following command:
sudo rm -rf /usr/local/mysql; sudo rm -rf /usr/local/mysql*; sudo rm -rf /Library/StartupItems/MySQLCOM; sudo rm -rf /Library/PreferencePanes/My*; sudo rm -rf /Library/Receipts/mysql*; sudo rm -rf /Library/Receipts/MySQL*; sudo rm /etc/my.cnf; say job completed successfully" with administrator privileges

Resources