my @files_list = glob "$inpath/filename*.txt"; foreach my $var (files_list){ my @array[i]=$var; i=i+1; } my @sorted_list = reverse sort @myarray; #@myarray contains the files foreach my $file (@sorted_list){ my $single_file = glob "$path/$file*.txt"; some stuff on files...................... }