opendir(); readdir(); so what you could do is: use strict; use warnings; use File::Copy; opendir (DIR,"./path") || die "$!"; # this opens the directory foreach my $i(readdir DIR){ next if (/\./g || grep{$_=~/$i/g}22..50); move("./path/$i", "/tmp/$i"); # if (-e "./path/$i"); } closedir DIR;