I have a folder with several numeric subfolders:
.
├── 1
├── 2
├── 3
├── 4
└── 5_a
I would like to find the one with the highest value. In this example it would be folder 4.
The folders can also be numbered:
.
├── 1e-3
├── 2e-3
├── 3e-3
├── 4e-3
└── 5e-3_a
How can I achieve this? The code is to be used in a bash script for batch processing.
I have tried: find . -type d -regex '.*/[0-9][^/]*' |sort -rn -t '' |head -n 1
but the regex syntax will not filter out exclusive numerical folders.
Best Regards
2
Answers
I like extended globbing for things like this.
edit
Feeling stupid for posting that without correcting for lexical sorts…
Thanks, Glenn, for calling me on it.
Stealing some of @anubhava’s logic to grab the numeric max, including the parsing of the scientific notation.
You may try this
find | awk
command to find the greatest numerical value of a sub-directory name (gnu-awk will be required due to multi-charRS
):awk
can handle scientific notation numerical strings as well.Or if all of your sub-directories at level 1 only, you may use this
printf | awk
solution: