How to find all .sh files and make them executable using bash in linux? - linux

also when I launch I want to pass path to folder when located these .sh files
I started with this
#!/bin/bash
find /home/user_name -name "*.sh"
And after script has to write in logo list with executable files

The safest and way both in terms of security and in terms of weird file names (spaces, weird characters, and so forth) is to use find directly:
find /home/user -name "*.sh" -execdir chmod u+x {} +
You can check the comments and the manual of find why this is safe, but in short, it makes sure your file is properly quoted in the chmod command. execdir (rather then -exec) is an extra security feature making sure the command is executed in the directory the file was found in avoiding race conditions (elaborated in the manual).

another way :
find . -name "*.sh" -exec chmod ux+y {} \;
you can first check your command by using
find . -name "*.sh" -print

If you want to make all files executable for the current user, you can use the command as follows (assuming that you have permission for all files in target home folder) :
find /home/user_name -name "*.sh" -print0 | xargs -0 chmod u+x

By #kabanus command
#!/bin/bash
# chmod u+x $(find $1 -name "*.sh")
# ls -1 $1/*.sh
find $1 -name "*.sh" -print -exec chmod u+x {} +
And use as
$ ./script.sh /your_directory
/your_directory - first argument ($1) in the script.

Related

cannot delete directory using shell command

I wanted to delete all directories with name in same pattern RC_200, here is what I did:
find -name "RC_200" -type d -delete
But it's complaining about this:
find: cannot delete '.RC200': Directory not empty
You should try:
find . -name RC200 -type d -exec rm -r {} \;
You can see here, what the command does
More, you can try what #anubhava recommended in comment (Note the + at the end); this one is equivalent to a xargs solution:
find . -name RC200 -type d -print0|xargs -0 rm -r
xargs executes the command passed as parameter, with the arguments passed to stdin. This is using rm -r to delete the directory and all its children
May be you should run administrator privilege.
if your os Ubuntu add "sudo" before your Command or what ever your os look at administrator key.
OR
remove file protection if it has.

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?
Here is what I have so far:
find -type d -name "*.ABC" -exec {} /Desktop/NewFile \;
I get this as output:
find: './GAE/.ABC: PERMISSION DENIED
Please Help, Thanks!
Your error here above has nothing to do with file read permission. You're trying to execute the directories you find! Avoid running commands as root or sudo unless: (1) you really need it and (2) you really know what you're doing. Quite often people asking for root or sudo privileges are exactly the ones should not have it.
That said... there are several ways to copy a directory tree under *nix. This is just one possible approach:
$ find <start> -type d -name \*.ABC -exec cp -av {} <target> \;
Where:
<start> is a directory name. It's used to tell find where to start its search (for example /usr/local or $HOME)
<target> is another directory name to define the final destination of your copied directories
UPDATE
In case you want to search for multiple paths...
$ find <start> -type d \( -name \*.ABC -o -name \*.DEF \) -exec cp -av {} <target> \;
This should work:
find ./source_dir -name \*.png -print0 | xargs -0 cp -t path/to/destination
For more info, you can look up here.

Run an Executable Program File in Multiple Subdirectories Using Shell

I have a main directory with 361 subdirectories. Within the each subdirectory, there is a parameter file and one executable program file. The executable file is coded to look for the parameter file in the directory where the executable is located. (The same executable file is in all subdirectories. The parameter files all have the same file name in all subdirectories)
Instead of executing the program file individually, is there a cshell command for terminal to run them all at once?
UPDATED
If your Linux is so old it doesn't have -execdir, you could try this:
find $(pwd) -name YourProgram -exec dirname {} \; | while read d; do cd "$d" && pwd; done
If that correctly prints the names of the directories where your program needs to be run, just remove the pwd and replace with whatever you want done in tha directory - presumably something like this:
find $(pwd) -name YourProgram -exec dirname {} \; | while read d; do cd "$d" && ./YourPrgram; done
ORIGINAL ANSWER
Like this maybe:
find . -type f -name YourProgramName -execdir ./YourProgramName YourParameterFile \;
But backup first and check it looks right before using.
The -execdir causes find to change to the directory it has found before running the commands there.
If your command is more complicated, you can do this:
find . -type f -name YourProgramName -execdir sh -c "command1; command2; command3" \;
Check it does what you want like this:
find . -type f -name YourProgramName -execdir pwd \;
Maybe this will help. Suppose you have in each folder a file named params_file and an executable named exec_file, then:
for dir in `find . -maxdepth 1 -mindepth 1 -type d` ; do
cd $dir
cat params_file | xargs ./exec_file
cd ..
done

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