#!/bin/sh for i in 01 02 03 04 do pushd "$i" ls *.txt | while read f do echo "${f:5}" cp "$f" "r/${f:5}" done popd done