rm: cannot remove '–rf': No such file or directory - bash

I have a Bash script that automates creating some SVN folders. In the course of doing so, it creates a temporary directory. When I try to delete that temp directory with the rm -rf command, I get the following error...
rm: cannot remove '–rf': No such file or directory
It seems to think that "-rf" is a file name. The command works fine on the command line.
Here is my script...
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Usage: $0 reponame1 reponame2 ..."
else
for var in "$#"
do
REPONAME=$var
mkdir -p ~/temp-$REPONAME/branches
mkdir ~/temp-$REPONAME/tags
mkdir ~/temp-$REPONAME/trunk
svnadmin create $REPONAME
svn import ~/temp-$REPONAME svn+ssh://username#192.168.123.234/home/username/svnrepos/$REPONAME -m "Initial structure"
rm –rf ~/temp-$REPONAME/
done
fi
And here is the output
$ ./mkrepo.sh mysvnrepo
username#192.168.123.234's password:
username#192.168.123.234's password:
Adding /home/username/temp-mysvnrepo/branches
Adding /home/username/temp-mysvnrepo/tags
Adding /home/username/temp-mysvnrepo/trunk
Committing transaction...
Committed revision 1.
rm: cannot remove '–rf': No such file or directory
rm: cannot remove '/home/username/temp-mysvnrepo/': Is a directory

You managed to type a unicode "EN DASH"(U+2013) which is not recognised by rm as a normal hyphen "-"(U+002D) so rm thinks it is the beginning of a file name and not of your parameters. They do look alike, but they are not the same for a program. To fix it, just erase it and type it again making sure you take the normal hyphen/minus key.

The '-' in your script in rm –rf is not the one it expects.
The correct one is rm -rf.
I hope you can spot the difference.
rm –rf
rm -rf

Related

bash shell generates a link that was not specified

I wrote a simple bash script (in /homedir) to run an executable and then move the outputs to /workdir. I also made a soft link of /workdir named work to /homedir for me to switch easily between folders.
All steps are working well, except that an unspecified soft link named 'grids' is created in /workdir to itself. I can't delete it otherwise all outputs are gone as well.
How can this happen?
#!/bin/bash
cd ..
expname=`basename "$PWD"`
echo 'experiment name: '$expname
homedir=/home/b/b380963/icon_foehn/$expname/grids/
workdir=/work/bb1096/b380963/icon_foehn/$expname/grids/
if [ ! -d ${workdir} ]; then
mkdir -p ${workdir}
fi
cd $homedir
ln -s ${workdir} work
cd /home/b/b380963/nwp/dwd_icon_tools_v2/icontools/
./icongridgen --nml $homedir/gridgen_MCH_july.nml
mv ICON_1E_* $workdir/
mv base_grid* $workdir/
It's quite easy to see in your code:
workdir=/work/bb1096/b380963/icon_foehn/$expname/grids/
...
ln -s ${workdir} work
The command ln -s is the command, creating the symlink.
If you don't like the creation of that symlink, you might put that line in comment (don't delete it: in case you're not satisfied, it's easier to uncomment it).
You can solve your issue, using this command:
ln -sTf ...
This removes the existing destination files beforehand.

Why does ''rm "$dir"/* !(.gitignore)'' delete the script itself?

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)

Mac Deleting Multiple Files Bash

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

shell script - creating folder structure

I wrote this little shell script(test.sh) to create a basic folder structure:
#!/bin/bash
# Check if directory already exists,
# if it doesnt, create one.
if [ ! -d "~/.dir1" ]; then
mkdir ".dir1"
else
rm -rf ".dir1"
mkdir ".dir1"
fi
When I run
test.sh
in console, the hidden folder is created.
But:
When I run it again it tells me:
mkdir: .dir1: File exists
But it could exist because I removed it in my shell script before I created a new one!
So why does it display this message?
Thanks and greetings!
Replace
[ ! -d "~/.dir1" ]
by
[ ! -d "${HOME}/.dir1" ]
I would simply use -p.
mkdir -p "$HOME/dir1"
If you pass -p, mkdir wouldn't throw an error if the directory already exists, it would simply silently return in that case.
If you want to make sure folder is empty use this:
rm -rf "$HOME/dir1"
mkdir -p "$HOME/dir1"
and no if! The basic problem with the if is the fact that it is not immune against race conditions. When the script went off from CPU right after the if - and creates "dir1" - your script will fail when it enters the CPU again since it still thinks the directory does not exist.
What you are doing by "~/.dir1" is not right. It's just another string for a directory name literally "~/.dir1" i.e ~ is not being expanded to $HOME.
Use full path or ~/".dir1" or ~/.dir1 instead.
You can use $HOME too: $HOME/.dir1 or "$HOME/.dir1" or "$HOME"/".dir1" all of them will produce same result... but quoting variables is a good practice.
~ isn't expanded when you place it in quotes. You need to leave it unquoted.
if [ ! -d ~/.dir1 ]
Of note, you're checking for ~/.dir1 but you make .dir1. That's only the same directory if the current directory is ~. If it isn't, they're not the same.
Also, mkdir -p will do this for you, creating a directory only if it doesn't exist already. You could simplify your script to:
mkdir -p ~/.dir1
or
rm -rf ~/.dir1
mkdir ~/.dir1

bad character showing up in bash script execution

I have a bash script that is getting an accented character appended to some strings that is causing it to fail, and I can't find where or how these characters are getting in there.
Here is some example output:
mv: cannot move â/tmp/myapp.zipâ to â/opt/myserver/myapp/deploys/myapp.1.2.21.zipâ: No such file or directory
ln: failed to create symbolic link â/opt/myserver/myapp/deploys/myapp_beta.zipâ: No such file or directory
cp: cannot stat â/opt/myserver/myapp/deploys/myapp_beta.zipâ: No such file or directory
the invalid character is the â.
The script is below:
#!/bin/bash
BRANCH=$1
SVN_LOC="https://svn/svn/myserver/"
MYAPP_REPO="myapp.git"
COREJS_REPO="core-js.git"
SPARTAN_REPO="core-spartan.git"
MYAPP_LOCATION="myapp/"
COREJS_LOCATION="corejs/"
SPARTAN_LOCATION="spartan/"
DEPLOY_LOCATION="/tmp/deploy/"
CLEANUP="${DEPLOY_LOCATION}*"
DEPLOY_STORE="/opt/myserver/myapp/deploys/"
DEPLOY_TIME=$(date +%s)
failed ()
{
rm -rf $CLEANUP
exit 1
}
mkdir -p $DEPLOY_LOCATION
echo "Retrieving Code from Git Branch ${BRANCH}"
echo "Retrieving myapp code"
mkdir -p "${DEPLOY_LOCATION}${MYAPP_LOCATION}"
pushd /opt/myserver/myapp/myapp
git archive $BRANCH | tar -x -C "${DEPLOY_LOCATION}${MYAPP_LOCATION}"
if [ $? -ne 0 ]
then
echo "Failed retrieving code from git ${MYAPP_REPO} repo";
failed
fi
popd
echo "Checking version numbers"
VERSION=$(php "${DEPLOY_LOCATION}${MYAPP_LOCATION}version.php" output)
DEPLOY_PACKAGE="${DEPLOY_STORE}myapp.${VERSION}.zip"
if [ -f $DEPLOY_PACKAGE ]
then
echo "A deploy with the same version number (${VERSION}) already exists! Please increment version number or manually deal with existing ${DEPLOY_PACKAGE}";
failed
fi
echo "Retrieving corejs code"
mkdir -p "${DEPLOY_LOCATION}${COREJS_LOCATION}"
pushd /opt/myserver/myapp/core-js
git archive $BRANCH | tar -x -C "${DEPLOY_LOCATION}${COREJS_LOCATION}"
if [ $? -ne 0 ]
then
echo "Failed retrieving code from git ${COREJS_REPO} repo";
failed
fi
popd
echo "Retrieving spartan code"
mkdir -p "${DEPLOY_LOCATION}${SPARTAN_LOCATION}"
pushd /opt/myserver/myapp/spartan
git archive $BRANCH | tar -x -C "${DEPLOY_LOCATION}${SPARTAN_LOCATION}"
if [ $? -ne 0 ]
then
echo "Failed retrieving code from git ${SPARTAN_REPO} repo";
failed
fi
popd
echo "Minifying js and css"
pushd "${DEPLOY_LOCATION}${MYAPP_LOCATION}Server/Deploy/"
php MinifyLyroke.php --deploytime $DEPLOY_TIME
popd
ASSETS_DEPLOY_PACKAGE="${DEPLOY_STORE}myappassets.${VERSION}.zip"
TEMP_ASSETS_ZIP_LOC="/tmp/myappassets.zip"
DEPLOY_ASSETS="${DEPLOY_LOCATION}myapp/Assets/"
ASSETS_DEPLOY_LOCATION="/tmp/assetsdeploy/"
DEPLOYED_ASSETS="${ASSETS_DEPLOY_LOCATION}myappassets_${DEPLOY_TIME}"
mkdir -p $ASSETS_DEPLOY_LOCATION
echo "Packaging assets deploy to ${ASSETS_DEPLOY_PACKAGE}"
mv $DEPLOY_ASSETS $DEPLOYED_ASSETS
pushd $ASSETS_DEPLOY_LOCATION
zip -r ${TEMP_ASSETS_ZIP_LOC} *
popd
mv ${TEMP_ASSETS_ZIP_LOC} ${ASSETS_DEPLOY_PACKAGE}
ln -sfn ${ASSETS_DEPLOY_PACKAGE} "${DEPLOY_STORE}myappassets_beta.zip"
cp "${DEPLOY_STORE}myappassets_beta.zip" "/opt/myserver/myapp/myapp/Server/Deploy/"
rm -rf $DEPLOYED_ASSETS
rm -rf $ASSETS_DEPLOY_LOCATION
echo "Packaging deploy to ${DEPLOY_PACKAGE}"
TEMP_ZIP_LOC="/tmp/myapp.zip"
pushd ${DEPLOY_LOCATION}
zip -r ${TEMP_ZIP_LOC} *
popd
mv "${TEMP_ZIP_LOC}" "${DEPLOY_PACKAGE}"
ln -sfn "${DEPLOY_PACKAGE}" "${DEPLOY_STORE}myapp_beta.zip"
cp "${DEPLOY_STORE}myapp_beta.zip" "/opt/myserver/myapp/myapp/Server/Deploy"
echo "Cleaning up"
rm -rf $CLEANUP
can anyone possibly see the issue or suggest a way I can go about finding where the issue is?
Those â characters are just mangled smart quotes printed from your shell. Your shell is probably outputting UTF-8, but your terminal is reading ISO-8859-1. Note that â is the rendering of a UTF-8 encoded smart quote ‘ in ISO-8859-1, with two nonprintable characters following the â. Most modern terminal emulators come with an option to enable UTF-8; see if you can enable that (it will make your life easier).
The problem is in your script, not the funny characters.
Try opening the script in another text editor like Notepad++ and see if there are any special characters present.
From the command line, type both of these commands. One or more of the files/directories you are expecting to exist, does not exist.
ls /tmp/myapp.zip
ls /opt/myserver/myapp/deploys
The accepted answer explains the problem, thanks #nneonneo. This is what you can do for a quick fix:
A) check your locale settings with:
locale
B) before calling your script or in the top of your bash-script try:
export LANG=en_US.UTF-8
export LC_ALL=C

Resources