#!/usr/bin/perl -w use strict; my $path = '/Users/mydirectory/Desktop/BioinfDev/SequenceAssemblyProject/KOMP/*'; foreach (glob("$path\*")) { print "$1\n" if (-d and /\/(\d+)_\w*$/); }