I need a script that will search for a file in a applications directory and delete it. If it's not there it will continue with the install.
What I'm needing deleted:
/Applications/Cydia.app/Sections/Messages(D3#TH's-Repo).png
If that's not found I want it to continue on the install. If it finds that file I want it to delete it before continuing the installation.
This is what I've got:
#!/bin/bash
file="/Applications/Cydia.app/Sections/Messages(D3#TH's-Repo).png"
if [ -f "$file" ]
then
echo "$file delteling old icon"
rm -rf /Applications/Cydia.app/Sections/Messages(D3#TH's-Repo).png
else
echo "$file old icon deleted already moving on"
fi
try this
#!/bin/bash
if [ -e <your_file> ]; then
rm -f <your_file>
fi
this should do.
Parentheses are used to start a subshell in bash, so you'll need to put your filename in double-quotes (as you did in the file test).
Change the line:
rm -rf /Applications/Cydia.app/Sections/Messages(D3#TH's-Repo).png
To:
rm -rf "${file}"
And this will remove the file (assuming no permissions problems).
Related
I have this shell script that I'm using to clean up some temp files.
The script is stored in: /root/cronjobs.
When I run the script from this location ./cleanUploader.sh, it deletes all the files in the current folder along with itself.
Here's the script:
#!/bin/bash
# cleanUploader.sh
# Batch file to remove various temp directories and files left over from the Uploader
clear
echo
INHOUSEFILES=/var/www/html/inhouseweb/officedb/uploader/files
shopt -s extglob
if [ -d $INHOUSEFILES ]; then
echo "Removing directory $INHOUSEFILES"
rm -rf $INHOUSEFILES/* !(".gitignore")
else
echo "directory $INHOUSEFILES not found"
fi
echo
shopt -u extglob
echo
echo "Done"
What am I doing wrong?
rm -rf $INHOUSEFILES/* !(".gitignore")
This deletes all files in $INHOUSEFILES/*, and then it also deletes everything in the current directory except .gitignore. That's what !(".gitignore") does when it's a separate argument.
If your intention is to delete everything in $INHOUSEFILES/ except .gitignore then combine the two arguments:
rm -rf $INHOUSEFILES/!(".gitignore")
It's also a good idea to quote variable expansions. (And conversely you don't need them around a literal string like .gitignore.)
rm -rf "$INHOUSEFILES"/!(.gitignore)
I'm trying to uninstall a program by deleting all of the files the installer installed. This is the script I have tried, but it returns a "Too many arguments" error on line 6 (highlighted with **) when I try and run it.
This is to be deployed out to multiple machine through Apple Remote Desktop.
I would like to put it in a package to run, but as an executable script will also do the job. Am I going about this wrong? This is not the entire script but it follows the same pattern.
#!/bin/bash
## This will uninstall ETC Nomad v2.3.3.9.0.10.mpkg
## From Contents of ETCnomad Eos Mac 2.3.3.9.0.10.pkg
**if [ -d /Applications/Eos Family Welcome Screen.app ]; then**
/bin/rm -rf /Applications/Eos Family Welcome Screen.app
fi
if [ -f /tmp/Element_Hotkeys.pdf ]; then
/bin/rm -rf /tmp/Element_Hotkeys.pdf
fi
if [ -f /tmp/Eos_Hotkeys.pdf ]; then
/bin/rm -rf /tmp/Eos_Hotkeys.pdf
fi
if [ -f /tmp/FixtureReleaseNotes.pdf ]; then
/bin/rm -rf /tmp/FixtureReleaseNotes.pdf
fi
if [ -f usr/local/etc/DCIDTable ]; then
/bin/rm -rf usr/local/etc/DCIDTable
fi
exit 0
Answer
Use ' around path/filenames that contain spaces or else the shell will try to interpret the parts as different from the filename and get confused, hence the error message.
More comments
As jubobs points out, there's no use in testing whether the file exists before deleting it. Furthermore, you already use the -f option which ignores nonexistent files so the test becomes irrelevant.
Remove absolute paths from your commands to the keep your script portable. The shell's PATH environment variable is used to search for commands in the right places.
No need to remove files from /tmp/ because the OS does that for you.
Be careful when you tinker with system folders like /usr/ because every system upgrades overwrite them, and often times it's hard to tell all dependencies.
You can simplify your script:
#!/bin/bash
## This will uninstall ETC Nomad v2.3.3.9.0.10.mpkg
## From Contents of ETCnomad Eos Mac 2.3.3.9.0.10.pkg
rm -rf '/Applications/Eos Family Welcome Screen.app'
# rm -rf /tmp/Element_Hotkeys.pdf
# rm -rf /tmp/Eos_Hotkeys.pdf
# rm -rf /tmp/FixtureReleaseNotes.pdf
rm -rf /usr/local/etc/DCIDTable
exit 0
Running OSX 10.11.2, I need to rm -rf the file in the location indicated in the symbolic links below for atom, npm and node as well as the links. I am currently log in as a user but terminal is in su mode.
I tried few commands for no avail. I tried to go to those locations but do not know how to.
Thank you
To start, rm itself does not have a suitable option to remove the files that the links point to. That would make it cumbersome to do this in a single command. A script helps:
#!/bin/sh
for name in "$#"
do
if [ -L "$name" ]
then
target=$(stat -f '%Y' "$name")
$0 "$target"
fi
[ -e "$name" ] && rm -rf "$name"
done
The script uses the OSX stat command to obtain the link target, and recurs to itself, to remove the target (which could be another link), and after removing the target, removes the link (or non-link, as the case may be).
In a comment, OP clarified that the link itself should not be removed. That can be done by changing the test:
#!/bin/sh
for name in "$#"
do
if [ -L "$name" ]
then
target=$(stat -f '%Y' "$name")
$0 "$target"
fi
[ -e "$name" ] && [ ! -L "$name" ] && rm -rf "$name"
done
I am working on some stuff where I am storing data in a file.
But each time I run the script it gets appended to the previous file.
I want help on how I can remove the file if it already exists.
Don't bother checking if the file exists, just try to remove it.
rm -f /p/a/t/h
# or
rm /p/a/t/h 2> /dev/null
Note that the second command will fail (return a non-zero exit status) if the file did not exist, but the first will succeed owing to the -f (short for --force) option. Depending on the situation, this may be an important detail.
But more likely, if you are appending to the file it is because your script is using >> to redirect something into the file. Just replace >> with >. It's hard to say since you've provided no code.
Note that you can do something like test -f /p/a/t/h && rm /p/a/t/h, but doing so is completely pointless. It is quite possible that the test will return true but the /p/a/t/h will fail to exist before you try to remove it, or worse the test will fail and the /p/a/t/h will be created before you execute the next command which expects it to not exist. Attempting this is a classic race condition. Don't do it.
Another one line command I used is:
[ -e file ] && rm file
You can use this:
#!/bin/bash
file="file_you_want_to_delete"
if [ -f "$file" ] ; then
rm "$file"
fi
If you want to ignore the step to check if file exists or not, then you can use a fairly easy command, which will delete the file if exists and does not throw an error if it is non-existing.
rm -f xyz.csv
A one liner shell script to remove a file if it already exist (based on Jindra Helcl's answer):
[ -f file ] && rm file
or with a variable:
#!/bin/bash
file="/path/to/file.ext"
[ -f $file ] && rm $file
Something like this would work
#!/bin/sh
if [ -fe FILE ]
then
rm FILE
fi
-f checks if it's a regular file
-e checks if the file exist
Introduction to if for more information
EDIT : -e used with -f is redundant, fo using -f alone should work too
if [ $( ls <file> ) ]; then rm <file>; fi
Also, if you redirect your output with > instead of >> it will overwrite the previous file
So in my case I wanted to remove a FIFO file before I create it again, so this worked for me:
#!/bin/bash
file="/tmp/test"
rm -rf $file | true
mkfifo $file
| true will continue the script even if file is not found.
I have a shell (ksh) script. I want to determine whether a certain directory is present in /tmp, and if it is present then I have to delete it. My script is:
test
#!/usr/bin/ksh
# what should I write here?
if [[ -f /tmp/dir.lock ]]; then
echo "Removing Lock"
rm -rf /tmp/dir.lock
fi
How can I proceed? I'm not getting the wanted result: the directory is not removed when I execute the script and I'm not getting Removing Lock output on my screen.
I checked manually and the lock file is present in the location.
The lock file is created with set MUTEX_LOCK "/tmp/dir.lock" by a TCL program.
In addition to -f versus -d note that [[ ]] is not POSIX, while [ ] is. Any string or path you use more than once should be in a variable to avoid typing errors, especially when you use rm -rf which deletes with extreme prejudice. The most portable solution would be
DIR=/tmp/dir.lock
if [ -d "$DIR" ]; then
printf '%s\n' "Removing Lock ($DIR)"
rm -rf "$DIR"
fi
For directory check, you should use -d:
if [[ -d /tmp/dir.lock ]]; then
echo "Removing Lock"
rm -rf /tmp/dir.lock
fi