Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
6 added 85 characters in body
source | link

With GNU find, you could obviously pick out your numeric directories and then loop over their contents:

find . -maxdepth 1 -type d -regex '.*/[0-9]+$' -exec sh '
    for dirpath do
        for pathname in "$dirpath"/*; do
            [ ! -f "$pathname" ] && continue
            # use "$pathname" here
        done
    done' sh +

but I don't generally suggest using regular expressions on filenames or directory names since they are mainly for matching text in text files. GNU find obviously supports matching pathnames with regular expressions, because GNU software tries to be as convenient as possible.

With GNU find, you could obviously pick out your numeric directories and then loop over their contents:

find . -maxdepth 1 -type d -regex '.*/[0-9]+$' -exec sh '
    for dirpath do
        for pathname in "$dirpath"/*; do
            [ ! -f "$pathname" ] && continue
            # use "$pathname" here
        done
    done' sh +

but I don't generally suggest using regular expressions on filenames or directory names since they are mainly for matching text in text files. GNU find obviously supports matching pathnames with regular expressions, because GNU software tries to be as convenient as possible.

5 added 85 characters in body
source | link

except that the find variation would skip symbolic links to regular files and include hidden names (set the dotglob shell option in bash to include these).

The pattern +([0-9)/ would expand to names of directories that only have digits in their names. This would also work in zsh if the KSH_GLOB option was set instead of extglob.

except that the find variation would skip symbolic links to regular files.

The pattern +([0-9)/ would expand to names of directories that only have digits in their names.

except that the find variation would skip symbolic links to regular files and include hidden names (set the dotglob shell option in bash to include these).

The pattern +([0-9)/ would expand to names of directories that only have digits in their names. This would also work in zsh if the KSH_GLOB option was set instead of extglob.

4 added 481 characters in body
source | link
ls */

This would list the contents of all the non-hidden subdirectories in the current directory. Since you only seem to have subdirectories with numeric names, this will show the contents of these.

The shell globbing pattern * would expand to all non-hidden names in the current directory. Adding a / at the end of the pattern forces the pattern to expand to only directories (since non-directories can't have / in their names).

With the zsh shell, the following filename globbing pattern would expand to only regular files in each subdirectory

*/*(.)

To do something with those names that matches, you would loop over the expansion of that glob pattern:

for pathname in */*(.); do
    # use "$pathname" here
done

In bash or sh, you would call a small script from find instead:

find . -mindepth 2 -maxdepth 2 -type f -exec sh '
    for pathname do
        # use "$pathname" here
    done' sh {} +

But that's really just a fancy way of writing

for pathname in */*; do
    [ ! -f "$pathname" ] && continue
    # use "$pathname" here
done

except that the find variation would skip symbolic links to regular files.

Again, this would do what you wanted (look in directories with numeric names) because you appear to only have subdirectories with numeric names.

Assuming that there may be other subdirectories too,

for pathname in */*; do
    [ ! -f "$pathname" ] && continue
    case $(dirname "$pathname") in
        *[!0-9]*) continue
    esac
    # use "$pathname" here
done

or, with bash,

for pathname in */*; do
    if [ ! -f "$pathname" ] ||
       [[ $(dirname "$pathname") == *[!0-9]* ]]
    then
        continue
    fi
    # use "$pathname" here
done

These loops would loop over all the names in the subdirectories, but would skip any name not referring to a regular file (or a symbolic link to one), and would also skip any file in a subdirectory whose name contains anything that is not a digit.

With the extglob shell option in bash, you could make that a bit shorter:

shopt -s exglob
for pathname in +([0-9])/*
    [ ! -f "$pathname" ] && continue
    # use "$pathname" here
done

The pattern +([0-9)/ would expand to names of directories that only have digits in their names.

ls */

This would list the contents of all the non-hidden subdirectories in the current directory. Since you only seem to have subdirectories with numeric names, this will show the contents of these.

The shell globbing pattern * would expand to all non-hidden names in the current directory. Adding a / at the end of the pattern forces the pattern to expand to only directories (since non-directories can't have / in their names).

With the zsh shell, the following filename globbing pattern would expand to only regular files in each subdirectory

*/*(.)

To do something with those names that matches, you would loop over the expansion of that glob pattern:

for pathname in */*(.); do
    # use "$pathname" here
done

In bash or sh, you would call a small script from find instead:

find . -mindepth 2 -maxdepth 2 -type f -exec sh '
    for pathname do
        # use "$pathname" here
    done' sh {} +

But that's really just a fancy way of writing

for pathname in */*; do
    [ ! -f "$pathname" ] && continue
    # use "$pathname" here
done

except that the find variation would skip symbolic links to regular files.

Again, this would do what you wanted (look in directories with numeric names) because you appear to only have subdirectories with numeric names.

ls */

This would list the contents of all the non-hidden subdirectories in the current directory. Since you only seem to have subdirectories with numeric names, this will show the contents of these.

The shell globbing pattern * would expand to all non-hidden names in the current directory. Adding a / at the end of the pattern forces the pattern to expand to only directories (since non-directories can't have / in their names).

With the zsh shell, the following filename globbing pattern would expand to only regular files in each subdirectory

*/*(.)

To do something with those names that matches, you would loop over the expansion of that glob pattern:

for pathname in */*(.); do
    # use "$pathname" here
done

In bash or sh, you would call a small script from find instead:

find . -mindepth 2 -maxdepth 2 -type f -exec sh '
    for pathname do
        # use "$pathname" here
    done' sh {} +

But that's really just a fancy way of writing

for pathname in */*; do
    [ ! -f "$pathname" ] && continue
    # use "$pathname" here
done

except that the find variation would skip symbolic links to regular files.

Again, this would do what you wanted (look in directories with numeric names) because you appear to only have subdirectories with numeric names.

Assuming that there may be other subdirectories too,

for pathname in */*; do
    [ ! -f "$pathname" ] && continue
    case $(dirname "$pathname") in
        *[!0-9]*) continue
    esac
    # use "$pathname" here
done

or, with bash,

for pathname in */*; do
    if [ ! -f "$pathname" ] ||
       [[ $(dirname "$pathname") == *[!0-9]* ]]
    then
        continue
    fi
    # use "$pathname" here
done

These loops would loop over all the names in the subdirectories, but would skip any name not referring to a regular file (or a symbolic link to one), and would also skip any file in a subdirectory whose name contains anything that is not a digit.

With the extglob shell option in bash, you could make that a bit shorter:

shopt -s exglob
for pathname in +([0-9])/*
    [ ! -f "$pathname" ] && continue
    # use "$pathname" here
done

The pattern +([0-9)/ would expand to names of directories that only have digits in their names.

3 added 481 characters in body
source | link
2 added 481 characters in body
source | link
1
source | link