Recursively find ".sh" file and then execute my .sh file on that path - linux

I have a folder which contains sub sub folders and some folders contains .sh file. I want to execute my .sh file in that path where I see a .sh file. I tried
find . -name '*.sh'
./folder 1/folder 2/run.sh
./folder3/run.sh
./folder 4/run.sh
./folder5/run.sh
This find function is working correctly. Now I have another .sh file which I want to execute on .sh file path. I tried
find . -name '*.sh' /home/cool/Desktop/followup.sh {} ;
where followup.sh is my file. The command is getting executed at current directory . but it is not getting executed on run.sh file path.
I am new to bash scripting. What command or commands would I issue? Note that there are spaces in Folder name.

find <parent-directory where all sub-folders with run.sh are located> -name '*.sh' -execdir /home/cool/Desktop/followup.sh {} \;

Related

Basic Bash Function to find and copy files based on filetype

I use the little bit of code below to find files in a folder with a given filetype and then copy them to a different folder.
find ./ -name '*.chk' -exec cp -prv '{}' "./" ';'
I tried to change this into a function in my .bash_profile so I could use it more quickly, but for some reason, it's not working. By not working, I mean nothing seems to happen when I execute "mymove .txt folder". Bash just goes to a new line, ready to accept more input.
mymove() {
find ./ -name "$1" -exec cp -prv '{}' "$2" ';';
}
Any advice?
You mention that you execute mymove with two parameters, .txt and folder:
$ mymove .txt folder
This will not work as .txt doesn't match any files, change it to:
$ mymove '*.txt' folder
Note the quotes, you do not want to shell to expand *.txt.

How to script to go to the deepest folder directory from parent directory to execute a command?

I am trying to automate converting .vmx to .ova by ovftool, and these .vmx files are generated from ghettoVCB. So I am writing a script to get converting automation working.
My question is how do I write a shell script that goes through each directory from a parent_directory and executes a command in each directory? Or could move everything from the deepest folder to parent_directory? (This solution may take time consuming to move those files from the deepest folder to parent_directory).
The directory structure is as follows:
parent_directory/automation/automation-2016-04-18_19-08-32/automation.vmx
parent_directory/bulls/bulls-2016-04-18_18-28-57/bulls.vmx
Here is another structure layout
parent_directory
automation
automation-2016-04-18_19-08-32
automation.vmx
bulls
bulls-2016-04-18_18-28-57
bulls.vmx
The name of subfolders from parent_directory does not follow patterns. Could be any name.
The folder "automation-2016-04-18_19-08-32" is the name of subfolder + date + time.
Approach 1
move everything from the deepest folder to parent_directory
This command will search subdirectories of the current directory, find all .vmx files, and move them to the current directory:
find . -type f -name '*.vmx' -exec mv {} . \;
Approach 2
write a shell script that goes through each directory from a parent_directory and executes a command in each directory
The following command will search for every subdirectory of the current directory and execute command in it:
find . -type d -execdir command \;
If you want to run command in every directory that contains .vmx files and supply the names of those .vmx files as arguments to command, then run:
find . -type f -name '*.vmx' -execdir command {} +
Alternatively, suppose we want to run the command once for each .vmx file that we find:
find . -type f -name '*.vmx' -execdir command {} \;

launch several scripts located in subdirectories

Here is my problem. I have a directory that contains multiple sub-directories. In each sub-directory, there is at least one script sh.
I want to do a script that execute sequentially all this scripts.
I am pretty new to linux.
Thanks for your help,
find . -name "*.sh" -exec {} \;
This is a shell command which, beginning in the directory it's being run in (specified by .), finds file names that end in .sh and then executes those files (the found file is substituted in the {}). The backslash prevents the semicolon from being expanded by the shell (here, bash).
Try doing it using find and for:
for file in `find . -type f -name "*.sh"`; do sh $file; done
Use can also store it in array and do it:
array=($(find . -type f -name "*.sh"))
for file in ${array[#]};do sh $file; done
From the top directory, run the following command:
for f in `find . -type f -name \*.sh`; do $f; done
The find command will locate all .sh files. The output of the find command (a whitespace separated list of pathnames to the scripts) becomes the input to the for command. The for command processes each input, assigning each entry to the variable f. The "$f" executes each script.

Find -exec and Bash scripts

I'm writing a script in bash.
I invoke it with
find *.zip -type f -exec ./myscript.sh {} \;
At the top of my script I invoke another script like this:
#!/bin/bash
. ticktick.sh
I get the following error
.: ticktick.sh: file not found
If I invoke the script like this
./myscript.sh somefile.zip
it works
If I put the ticktick.sh script in my path in another directory it breaks, so that isn't an option. Is there some special kind of context that scripts called with a find have? I'm obviously new to BASH scripting. Any help would be appreciated
I think there are 2 problems.
1.: if you want to search for all zip files in the current directory, you have to write the following command
find . -type f -name *.zip -exec ...
2.: you execute myscript.sh with ./ before it. So myscript.sh has to be in the current working directory. if your script is in /home/jd/ and you execute it from /home/ your myscript.sh will be not found.
first you have to determine the directory of your files:
install_path=$(dirname $(readlink -f $0))
So your complete find command is:
find . -type f -name *.zip -exec $install_path/myscript.sh {} \;
The myscript.sh file have to be in the same directory as ticktick.sh

How to find -exec cd in linux / unix

I'm searching for a config folder, and trying to change to that directory:
find . -name "config" -exec cd {} \;
There is one match, ./my-applications/config, but after I try this it says:
find: `cd': No such file or directory
What am I doing wrong?
The command cd is a shell built-in, not found in /bin or /usr/bin.
Of course, you can't change directory to a file and your search doesn't limit itself to directories. And the cd command would only affect the executed command, not the parent shell that executes the find command.
Use:
cd $(find . -name config -type d | sed 1q)
Note that if your directory is not found, you'll be back in your home directory when the command completes. (The sed 1q ensures you only pass one directory name to cd; the Korn shell cd takes two values on the command and does something fairly sensible, but Bash ignores the extras.)
In case you have more than one config directory:
select config in $(find . -name config -type d)
do
cd $config
break
done
find runs -exec programs as subprocesses and subprocesses cannot affect their parent process. So, it cannot be done. You may want to try
cd `find . -name "config"`

Resources