Home > Mobile >  Bash: find highest numerical value folder
Bash: find highest numerical value folder

Time:09-08

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 '\0' |head -n 1 but the regex syntax will not filter out exclusive numerical folders.

Best Regards

CodePudding user response:

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-char RS):

awk can handle scientific notation numerical strings as well.

find . -type d -name '*[0-9]*' -print0 |
awk -v RS='\0' -F '\\.?/' '
$NF 0 == $NF && $NF > max {max = $NF} END {print max}'

4

Or if all of your sub-directories at level 1 only, you may use this printf | awk solution:

printf '%s\0' *[0-9]*/ |
awk -v RS='\0' -F '/' '
$1 0 == $1 && $1 > max {max = $1} END {print max}'

4

CodePudding user response:

I like extended globbing for things like this.

$: ls -ld *
drwxr-xr-x 1 paul 1049089 0 Sep  7 09:35 1
drwxr-xr-x 1 paul 1049089 0 Sep  7 09:35 2
drwxr-xr-x 1 paul 1049089 0 Sep  7 09:35 3
drwxr-xr-x 1 paul 1049089 0 Sep  7 09:35 4
drwxr-xr-x 1 paul 1049089 0 Sep  7 09:35 5_e


$: shopt -s extglob   # turn on extended globbing
$: lst=(  ([0-9])/ )  # get non-hidden directories named with only digits
$: echo ${lst[-1]%/}  # show the highest
4

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.

$: ls -ld *
drwxr-xr-x 1 paul 1049089 0 Sep  7 09:35 1
drwxr-xr-x 1 paul 1049089 0 Sep  7 09:35 2
drwxr-xr-x 1 paul 1049089 0 Sep  7 10:13 2e-3
drwxr-xr-x 1 paul 1049089 0 Sep  7 09:35 3
drwxr-xr-x 1 paul 1049089 0 Sep  7 10:13 3e-3
drwxr-xr-x 1 paul 1049089 0 Sep  7 09:35 4
drwxr-xr-x 1 paul 1049089 0 Sep  7 10:13 4e-3
drwxr-xr-x 1 paul 1049089 0 Sep  7 09:35 5_e
drwxr-xr-x 1 paul 1049089 0 Sep  7 10:13 5e-3_a
drwxr-xr-x 1 paul 1049089 0 Sep  7 10:13 e-3


$: printf  '%s\0'  ([0-9])*(e-[0-9])/ |
>   awk -v RS='\0' -F '/' '$1 0 > max {max = $1} END {print max}'
4
  • Related