Home > OS >  Can you specify the depth after a wildcard expression in find?
Can you specify the depth after a wildcard expression in find?

Time:11-06

I have a small script that works perfectly well, except that it tries and fails to read some files that I don't want it reading anyway. It has no operational impact except it takes more time to read those extra files.

find "$FIND_START" -type f -wholename "*/DAT/*.zip"  | while read -r SOURCE_FILE
do
    echo "Generating data from $SOURCE_FILE"
    java -jar \
        generator-0.2.4-jar-with-dependencies.jar \
        $SOURCE_FILE $VARIATION $OUTPUT_DIR $NUM_GENERATE $SEED \
        > stdout.txt 2> stderr.txt

done

The depth at which the DAT/ folder is located is not guaranteed, or I think I could somehow use maxdepth argument.

However, I know that all the .zip files are contained directly in the DAT/ directory, not any sub-directories.

Is there a way to specify the depth after the wild card? If it matters, I'm on git bash for windows.

CodePudding user response:

Use -regex instead of -wholename (which, BTW, is equivalent to the more portable -path).

find "$FIND_START" -type f -regex '.*/DAT/[^/] \.zip'

[^/] matches a sequence of characters that doesn't include /, so it won't match across subdirectories of DAT.

  • Related