I am trying to run my bash script from another folder
I want it to be executed from which folder i want
What should I do ?
Im trying to used find in the beginning but it doesnt work !
find /path/to/Files -type d -exec Notes-Khaled-Mustafa.sh {} \;
./Notes-Khaled-Mustafa.sh {} simply path the current folder as a parameter to your script.
Which is not what you want. You want to execute your script with, as its current execution folder, the one you list through the find command.
That is done by calling a subshell and executing a cd in it.
In your bash session, try instead:
find . -type d -execdir /path/to/Note-khaled-mustafa.sh \;
So not "./Note-khaled-mustafa.sh", but "/absolute/full/path/to/Note-khaled-mustafa.sh"
This uses -execdir to change directory to each matched path. If your version of find doesn't have -execdir, use instead:
find . -type d -exec sh -c 'cd "$1" && /path/to/Note-khaled-mustafa.sh' sh {} \;
If you really just want to run the script and you do know the name, try
/path/to/Notes-Khaled-Mustafa.sh
if this does not work, change the permissions:
chmod +x /path/to/Notes-Khaled-Mustafa.sh
/path/to/Notes-Khaled-Mustafa.sh
Related
I'm trying to source a file that I can get from the output of find using these commands:
find ./ -iname activate.fish -exec source {} \;
and
find ./ -iname activate.fish -exec builtin source {} \;
But both these commands give the error of the form find: ‘source’: No such file or directory or find: ‘builtin’: No such file or directory. Seems like exec of find is not able to recognize fish's builtins ?
What I basically want to achieve is a single command that will search for Python's virtualenv activate scripts in the current directory and execute them.
So doing something like -exec fish -c 'source {}; \ would not help. I've tried it as well and it doesn't error out but does not make the changes either.
Any ideas what can be done for this ?
Thanks!
Perhaps you need:
for file in (find ./ -iname activate.fish)
source $file
end
# or
find ./ -iname activate.fish | while read file
source $file
end
Command substitution executes the command, splits on newlines, and returns that list.
As mentioned in comments, seems like -exec does not run in or affect the current shell environment. So find -exec is not gonna work for my use case.
Instead, this will work:
source (find ./ -iname activate.fish)
This is my first time using Automator and it seems like a pretty nifty tool. I am running into an issue however at the very end of the automation. The goal of my automator workflow is to specify a path, then create a directory Highschool1, for example, within that path.
From here, I want it to search for any files within the specified path that include "Highschool1" in the name, and move them into the new Highschool1 directory. Here is how my bash script works within terminal:
mkdir "/Users/tommy/Desktop/TestShow1/WG/Highschool1"
This creates the directory as intended. Then:
find /Users/tommy/Desktop/TestShow1/WG -name 'Highschool1' -prune -o -type f -name '*Highschool1*' -exec mv -- {} /Users/tommy/Desktop/TestShow1/WG/Highschool1 \;
This finds the files I want while excluding the new Highschool1 directory, and then moves the found files into that Highschool1 directory. It is all working as intended at the base.
It's when I try to apply this script within my automation using positional parameters that it stops working.
-I stuff a variable called "HighschoolName" with the input "Highschool1"
-Then I stuff a variable called "pathA" with the input, which is the path I chose: "/Users/tommy/Desktop/TestShow1/WG"
-Then I call back my HighschoolName variable and begin with the positional parameters.
This is the final script I used:
mkdir "$1/$2"
find /$1 -name '$2' -prune -o -type f -name '*$2*' -exec mv -- {} /$1/$2 \;
This creates the directory Highschool1 where I want it, but fails to move any files into it. It gives me no error message either. It simply acts as if the script was run successfully. Does anyone have any idea what the problem could be?
Read about quoting
In:
find /$1 -name '$2' -prune -o -type f -name '*$2*' -exec mv -- {} /$1/$2 \;
'$2' will not interpolate the variables, you need to use "$2" (same for '*$2*')
I'm trying to move a script from the directory I'm in to another directory after I have performed a find in the current directory. Although I don't get an error nothing happens. I don't know why. Can you help?
find . -name ScriptsFlowchart.xml -execdir mv {} Users/me/Desktop/SequencingScripts/{} \;
Try using -exec instead of -execdir and drop {} in target definition.
find . -name ScriptsFlowchart.xml -exec mv {} Users/me/Desktop/SequencingScripts/ \;
{} will retrieve the file path relative to current dir. So you must run the mv command from the current dir as well (using -exec).
From find manual page:
-execdir command ;
Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.
If that's in a bash script, try reading $? right after running the command to check if there were any errors (if equals 0, runs successfully).
I cannot delete folder created by php mkdir
for I in `echo $*`
do
find $I -type f -name "sess_*" -exec rm -f {} \;
find $I -type f -name "*.bak" -exec rm -f {} \;
find $I -type f -name "Thumbs.db" -exec rm -f {} \;
find $I -type f -name "error.log" -exec sh -c 'echo -n > "{}"' -f {} \;
find $I -type f -path "*/cache/*" -name "*.*" -exec rm -f {} \;
find $I -path "*/uploads/*" -exec rm -rdf {} \;
done
I want to delete under /uploads/ all files and folders please help me thanks...
You should consider changing your find command to use the -o pragma to join your conditions together as the final exec is basically the same. This will avoid recursing the file system repeatedly.
The other answers address your concern about php mkdir. I'll just add that it has nothing to do with the fact it was created with php mkdir rather than any other code or command. It is due to the ownership and permissions.
I think this is most likely because php is running in apache or another http server under a different user than you are invoking the bash script. Or perhaps the files uploaded in uploads/ are owned by the http server's user and not the user invoking it.
Make sure that you run the bash script under the same user as your http server.
To find out which user owns which file do:
ls -l
If you run you bash script as root, you should be able to delete it anyway, but that is not recommended.
Update
To run it as root for nautilus script use the following as your nautilus script:
gksudo runmydeletescript
Then put all the other code into another file with the same path as whatever you have put for runmydeletescript and run chmod +x on it. This is extremely dangerous!
You should probably add -depth to the command to delete sub-directories of upload before the directory itself.
I worry about the -path but I'm not familiar with it.
Also consider using + instead of \; to reduce the number of commands executed.
I want to run two commands but the second command depends on the first.
Is there a way to do something like this.
find . -name '*.txt' -exec 'y=$(echo 1); echo $y' {} \;
...
And actually, I want to do this.
Run the find command, change to that directory that the file is in and then run the command on the file in the current directory.
find . -name '*.txt' -exec 'cd basedir && /mycmd/' {} \;
How do I do that?
find actually has a primary that switches to each file's directory and executes a command from there:
find . -name '*.txt' -execdir /mycmd {} \;
Find's -exec option expects an executable with arguments, not a command, but you can use bash -c cmd to run an arbitrary shell command like this:
find . -name '*.txt' -exec bash -c 'cd $(dirname {}) && pwd && /mycmd $(basename {})' \;
I have added pwd to confirm that mycmd executes in the right directory. You can remove it. dirname gives you the directory of each file and basename gives you the filename. If you omit basename your command will receive (as {}) pathname to each file relative to the directory where you run find which is different from mycmd's current directory due to cd, so mycmd will likely fail to find the file. If you want your command to receive absolute pathname, you can try this:
find $PWD -name '*.txt' -exec bash -c 'cd $(dirname {}) && pwd && /mycmd {}' \;