for my $i ('001' .. '004') { my $dir_name = "abc_$i"; chdir $dir_name or die "Unable to change to $dir_name: $!"; ... chdir '..'; }