want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names.

Ex: i have 4 different files with name -
CTS_NONE_10476031_MRL_PFT20081215a.txt
CTS_NONE_10633009_MRL_PFT20091020a.txt
CTS_NONE_10345673_MRL_PFT20081215a.txt
CTS_NONE_10872456_MRL_PFT20091020a.txt

and the 1st file should go to the dir 'OLD_10476031', 2nd file into dir 'OLD_10633009' , 3rd into dir OLD_10345673 and so on.

tried differnt ways in ksh script but none are working correctly

I have a HP-UX machine and using KSH.

Thanks in advance for help.

In Ksh:

for file in *.txt
do
dirname=`echo $file | cut -d '_' -f3`
mv $file $dirname/
done

cheers,
Devaraj Takhellambam

Just added OLD_ to the dir name

 
for file in *.txt
do
dirname=`echo $file | cut -d '_' -f3`
path1=OLD_${dirname}
mv $file $path1/
done

Small addition Deva.

for file in *.txt
do
dirname=`echo $file | cut -d '_' -f3`
ndirname="OLD_"$dirname
mv $file $ndirname/
done

Looks perfect. thanx guys :slight_smile: