I have a simple for-loop that iterates over a glob-pattern and enters an if
condition, for when the directory path is not equal to */venv
then print the file. How can I get this working in the if
statement?
For example:
for file in ML*/*; do if [ "$file" != */venv ]; then echo $file ; fi; done
Is what I have tried, but it prints out:
[: too many arguments
[: too many arguments
[: too many arguments
[: too many arguments
[: too many arguments
[: too many arguments
[: too many arguments
[: too many arguments
[: too many arguments
[: too many arguments
...
...
The following implementation works for */venv
:
for file in ML*/*; do if [[ "$file" != */venv]]; then find $file ; fi; done
And if I wanted to exclude multiple paths:
for file in ML*/*; do if [[ "$file" != */venv && "$file" != */ENV_DIR ]]; then find $file ; fi; done
However, this assumes only the inner directory. If I wanted to exclude all sub-directories that may have venv
or ENV_DIR
so */*/venv, */*/*/venv
etc ..., how do I take this into consideration?
I have attempted the above with the following:
for file in ML*/*;
do for innerFile in $file*/*;
do if [[ "$innerFile" != */venv && "$innerFile" != */ENV_DIR && "$file" != */venv && "$file" != */ENV_DIR ]];
then find $innerFile ; fi; done; done
However, the nested loop only return inner-subdirectories, therefore $file
is skipped and we only get $file*/*
paths, but for further nested files like ML/plot/something/distant/venv
this won't be captured and I'd have to nest even further. What is the most effective way to capture this nested exclusion?
The below solution does not work and still will include venv
, or other directories that I want excluded. I found that the following works:
find ML*/* -not -path "*/venv/**" -not -path "*/ENV_DIR/**" -print0 | while read -d $'\0' file; do if [[ "$file" != */venv && "$file" != */ENV_DIR ]]; then echo $file; fi; done
This will only return the subdirectories and those files which do not contain venv
or ENV_DIR
, therefore, completing my previous proposal.
However, when I change echo $file
to find $file
the excluded directory path appears for ENV_DIR
and I am not sure why because the IF
condition should of removed it.