I am trying to run a specific program by looping it through a bunch of subdirectories using the find function, yet nothing seems to happen. Here is my code:
for FILE in $(find ~/test -maxdepth 1 -type f -name '*.vcf'); do
dotnet ~/Nirvana/Nirvana.dll \
-c ~/Nirvana/Data/Cache/GRCh37/Both \
--sd ~/Nirvana/Data/SupplementaryAnnotation/GRCh37 \
-r ~/Nirvana/Data/References/Homo_sapiens.GRCh37.Nirvana.dat \
-i $FILE \
-o ~/test/annotTest/$FILE.vcf.gz
done
Debugging with set -x seems to point to the following line:
find /home/test -maxdepth 1 -type f -name '*.vcf'
set x
I'm pretty sure the program's parameters are correct, since they work when I use it on just one file, but looping it just does nothing.
Is there some error in my code that I'm not seeing?
Thanks!
CodePudding user response:
The -maxdepth 1
is telling find
to search only in ~/test, not its subdirectories.
find
's idea of depth can be a little confusing: since you gave it ~/test
as the starting point for the search, it considers that to be at depth 0, items directly inside it to be at depth 1, and items inside those (i.e. in subdirectories of ~/test
) to be at depth 2. So if you want it to find things in subdirectories, use -maxdepth 2
.
(Just to make things more confusing: if you use something like find ~/test/* ...
, then the shell expands the ~/test/*
part into a list of files & directories in ~/test
and passes those as arguments to find
. Therefore, since each of those was given to find
as a separate starting point, each of those would be considered depth 0 in its own separate search. In this case, things in the subdirectories would be at depth 1.)
Actually, since you want to search at just a single level, I'd just use a shell wildcard expression:
for file in ~/test/*/*.vcf; do
(Note that lower- or mixed-case variable names are generally good practice, to avoid conflicts with the many all-caps names that have special meanings.)