Deleting content of folder with shell script - linux

Im having problems trying to empty a folder in my script.
This is working in my command line:
rm -r Folder1/Folder2/*
But if in my script I do this:
DIR="Folder1/Folder2/"
rm -r "$DIR*"
It says "rm: Folder1/Folder2/*: No such file or directory", where is the problem?
Im running the script in the same folder as I tried the command.

Glob expansion doesn't happen inside quotes.
Try:
rm -r -- "$DIR"*
(Just make really sure you don't put a space after the quotes.)

rm -r $DIR*
That should work, without quotes

Related

rm doesn't Work in script bash

I have a sh file with 1.000.000 of instructions "rm -f", but is not working. This runs on a ubuntu server 16.04
I tried this.
#!/bin/bash
/bin/rm -f /media/pictures2015/pictures/FTP/20150806/5939757.tif
/bin/rm -f /media/pictures2015/pictures/FTP/20150806/5939758.tif
/bin/rm -f /media/pictures2015/pictures/FTP/20150805/5939759.tif
And this
#!/bin/bash
rm -f /media/pictures2015/pictures/FTP/20150806/5939757.tif
rm -f /media/pictures2015/pictures/FTP/20150806/5939758.tif
rm -f /media/pictures2015/pictures/FTP/20150805/5939759.tif
The sh file has all the permissions, but don't wokr. With the "split" command change the sh file of 1.000.000 lines to two sh files of 500.000 lines.
The output dont show any error !!!
Thank You
the error was caused because the list of the images exported from phpmyadmin and at the end of the line I am with the "CR-LF" format, so investigate how to remove it and find the dos2unix program, install it in ubuntu, run and it worked.
The final solution I found on this link cyberciti
Thanks !!!!

Move all files except specified folder

Im trying to move all the contents of the current directory to a new folder in the current directory using a script
mv !\(.svn\|$line\|.\|..\) $line
error is
mv: cannot stat '!(.svn|RSSIFXServicesCommon|.|..)': No such file or directory
I echoed the command to output and if i copy and execute the command myself, it works.
I tried enabling extglob
With extended regex (shopt -s extglob), there's no need to quote your regex:
mkdir -p /tmp/t/4
touch /tmp/t/{1,2,3}
shopt -s extglob
cd /tmp/t
mv !(4|.|..) 4
This also works if I put this in a shell script.

Script cannot find file

I am trying to run a script named myscript.command on a Mac/Linux machine.
#!/bin/sh
echo 'Starting'
chmod 777 ./myfile
The problem is that when I get to the chmod part I get this output:
chmod ./myfile: No such file or directory
But both myscript.command and the myfile are in the same folder.
EDIT
It seems that when I launch the script the script's location is not being preserved. How can I preserve the location?
The script is being launched via double click in the UI.
$0
In order to change the current working directory to the script's directory, put the following command right after the shebang line:
cd "$(dirname "$0")"
The $0 variable expands to the script name (path to the script), and dirname returns path to the script's directory.
Detecting the current working directory
You can use pwd command to get the current working directory. If you are actually running Bash (I'm not sure, since the shebang in your code points to /bin/sh), you can use the built-in $PWD variable:
PWD
The current working directory as set by the cd builtin.
Storing the script's path into variable
Alternatively, save the directory path into a variable, and use it in the script, e.g.:
dir="$(cd $(dirname "$0"); pwd)"
chmod 770 "$dir/somefile"
Double quotes
Note the use of double quotes. Double quotes prevent reinterpretation of special characters. It is also the way to pass strings containing spaces as a single word:
dir="some directory name"
cd "$dir"
Without double quotes the words are interpreted as separate arguments.
You might start off something like this, too..
#!/bin/sh
echo 'Starting'
if [ -f ./myfile ]; then
chmod 777 ./myfile
else
echo "File does not exist:"
ls -l
fi

Executing shell script in ubuntu

Maybe I am missing something here but I have the following small bash script to delete some old files that get created when using flex and yacc its pretty simple but when I run the script it echos the result to the terminal but does not delete the file I'm probably missing something stupid could you guys point me in the right direction.
#!/bin/bash
echo "rm -f y.tab.h"; (just using one file for now)
I tried changing it to
echo rm -f y.tab.h;
but still no luck
I tried executing it with bash delete.sh and sh delete.sh and even using chmod +x on the file and executing it with ./
Remove echo statement
Just use
rm -f y.tab.h

bash - how to pipe result from the which command to cd

How could I pipe the result from a which command to cd?
This is what I am trying to do:
which oracle | cd
cd < which oracle
But none of them works.
Is there a way to achieve this (rather than copy/paste of course)?
Edit : on second thought, this command would fail, because the destination file is NOT a folder/directory.
So I am thinking and working out a better way to get rid of the trailing "/oracle" part now (sed or awk, or even Perl) :)
Edit :
Okay that's what I've got in the end:
cd `which oracle | sed 's/\/oracle//g'`
You use pipe in cases where the command expects parameters from the standard input. ( More on this ).
With cd command that is not the case. The directory is the command argument. In such case, you can use command substitution. Use backticks or $(...) to evaluate the command, store it into variable..
path=`which oracle`
echo $path # just for debug
cd $path
although it can be done in a much simpler way:
cd `which oracle`
or if your path has special characters
cd "`which oracle`"
or
cd $(which oracle)
which is equivalent to backtick notation, but is recommended (backticks can be confused with apostrophes)
.. but it looks like you want:
cd $(dirname $(which oracle))
(which shows you that you can use nesting easily)
$(...) (as well as backticks) work also in double-quoted strings, which helps when the result may eventually contain spaces..
cd "$(dirname "$(which oracle)")"
(Note that both outputs require a set of double quotes.)
With dirname to get the directory:
cd $(which oracle | xargs dirname)
EDIT: beware of paths containing spaces, see #anishpatel comment below
cd `which oracle`
Note those are backticks (generally the key to the left of 1 on a US keyboard)
OK, here a solution that uses correct quoting:
cd "$(dirname "$(which oracle)")"
Avoid backticks, they are less readable, and always quote process substitutions.
You don't need a pipe, you can do what you want using Bash parameter expansion!
Further tip: use "type -P" instead of the external "which" command if you are using Bash.
# test
touch /ls
chmod +x /ls
cmd='ls'
PATH=/:$PATH
if cmdpath="$(type -P "$cmd")" && cmdpath="${cmdpath%/*}" ; then
cd "${cmdpath:-/}" || { echo "Could not cd to: ${cmdpath:-/}"; exit 1; }
else
echo "No such program in PATH search directories: ${cmd}"
exit 1
fi
besides good answer above, one thing needs to mention is that cd is a shell builtin, which run in the same process other than new process like ls which is a command.
https://unix.stackexchange.com/questions/50022/why-cant-i-redirect-a-path-name-output-from-one-command-to-cd
http://en.wikipedia.org/wiki/Shell_builtin
In response to your edited question, you can strip off the name of the command using dirname:
cd $(dirname `which oracle`)

Resources