If you have a fixed directory structure of two levels:
shopt -s dotglob nullglob
for pathname in /filepath/orig/v1//; do
[[ $pathname == *.txt ]] && continue
printf 'Processing "%s"\n' "$pathname" >&2
cdo info "$pathname" >"$pathname.txt"
done
This first enables the dotglob
and nullglob
shell options. These shell options allows globbing patterns to match hidden names (dotglob
) and will ensure that patterns that are not matched are removed completely (nullglob
; this means the loop would not run a single iteration if /filepath/orig/v1/*/*
does not match any names).
Any name in our loop that already ends with .txt
is skipped, and the rest is processed with cdo info
to generate a .txt
file (note that I don't know what cdo info
actually does). Note that there is no need to touch
the filename first as the file would be created by virtue of redirecting into it.
Related:
If you know you will only process files with names ending in .nc
:
shopt -s dotglob nullglob
for pathname in /filepath/orig/v1//.nc; do
printf 'Processing "%s"\n' "$pathname" >&2
cdo info "$pathname" >"$pathname.txt"
done
If you want to process all files with names ending in .nc
anywhere beneath /filepath/orig/v1
:
find /filepath/orig/v1 -type f -name '*.nc' -exec sh -c '
for pathname do
printf "Processing \"%s\"\n" "$pathname" >&2
cdo info "$pathname" >"$pathname.txt"
done' sh {} +
This calls a short in-line script for batches of found regular files with names ending in .nc
.
You could also use /filepath/orig/v1/*/
as the search path with find
to only search the subdirectories of /filepath/orig/v1
and not /filepath/orig/v1
itself.
for dir in $(find . -type d); do
would already find every directory on all levels of the tree. Do you need to find the directories here, or is it enough to process all the files individually? – ilkkachu Dec 06 '21 at 12:06