How to take the maximum digits file linked with the folder using perl?

    my $location = $output_dir;
    print $location;
    open LOGFILE, $location;
    my $first_line = 1;
    #print $first_line;
    my $max_id;
    while (<LOGFILE>) {
        if (/rev_(\d)+/) {
            if ($first_line) {
                $first_line = 0;
                $max_id = $1;

            } else {
                $max_id = $1 if ($1 > $max_id);
                print $max_id;
            }
        }
    }

    close LOGFILE;

Obtained output:

 |-- USB_FS
    |   |-- Database
    |   |   |-- rev1
    |   |   |   |-- slater_ams_USB_FS_Database_rev1_.config
    |   |   |   `-- slater_ams_USB_FS_Database_rev1_.html
    |   |   |-- rev2
    |   |   |   |-- slater_ams_USB_FS_Database_rev2_.config
    |   |   |   `-- slater_ams_USB_FS_Database_rev2_.html
    |   |   |-- rev3
    |   |   |   |-- slater_ams_USB_FS_Database_rev3_.config
    |   |   |   `-- slater_ams_USB_FS_Database_rev3_.html
    |   |   |-- slater_ams_USB_FS_Database_.config
    |   |   `-- slater_ams_USB_FS_Database_.html

Expected output:

    |-- USB_FS
    |   |-- Database
    |   |   |-- rev3
    |   |   |   |-- slater_ams_USB_FS_Database_rev3_.config
    |   |   |   `-- slater_ams_USB_FS_Database_rev3_.html
    |   |   |-- slater_ams_USB_FS_Database_.config
    |   |   `-- slater_ams_USB_FS_Database_.html

After your main loop possibly just do another loop to go from 1 - $max_id and tar up then rm -rf each lower numbered directory?

Maybe I'm missing something but it seems like you're 99% there.

Make sure you make a complete backup of your directory before testing anything that deletes files. Make sure the tar files contain the files you think they do. Make sure the right directories are deleted.

Can you show me some examples?