Issue
Trying to execute script on files located into multiple subdirectories (with different depth). Im using script with arguments, Tried:
for i in "/home/working_dir_with_subdirs"/* ; do
/opt/my_script -e -x "$i" "$i"/* "$i"/..
done
But only files in main directory /home/working_dir_with_subdirs
was loaded by script. Files from subdirectories was not loaded, In terminal I saw that script is trying to execute subdirectory as a input file, not files inside directory.
Found some examples with find
command, but files in subdirectories got different extensions.
Solution
One solution would use find
and xargs
:
find /home/working_dir_with_subdirs/ -type f -print0 |
xargs -0 -n1 /opt/my_script -e -x
This would call /opt/my_script
once for each file located in /home/working_dir_with_subdirs
and its subdirectories.
If your script can accept multiple files as arguments, you could drop the -n1
:
find /home/working_dir_with_subdirs/ -type f -print0 |
xargs -0 /opt/my_script -e -x
This would call your script with multiple files as arguments, rather than once per file.
In both cases, the -print0
on find
and the -0
on xargs
are to correctly handle filenames that contain whitespace.
Answered By - larsks