Sorting based on filename

Hello ,
I have to write a bash script. I will explain the logic based on a scenario.

Scenario :
Suppose I have few files in a Folder X :

  FILE_201508.list
  FILE_201510.list
  FILE_201507.list
  abc_201510.csv
  xyz_201508.csv
  abc_201507.csv
  def_201507.csv  

1) Now , first it should it only search for files starting with FILE_* in descending order and take the file with lowest number i.e FILE_201507.list .
2) (a)Then , corresponding to that file number , it should take other files which has same number in their prefix i.e (abc_201507.txt, def_201507.txt) and change their file name and copying it to a different folder Y.
OR
(b) It should open that .list file which is having few *.csv file names that are in same X folder . it should select those files and then rename them and copy it to a different folder Y .

OK...
and what have you done so far?

This is in no way the most efficient of methods, but this should get you started. Since ls always sorts the fname:

fname=`ls FILE* | head -1`

gets you the earliest of the FILE filenames. If you have some aberrant version of ls , stick a sort in there:

fname=`ls FILE* | sort | head -1`

Now that you have the particular filename you want, you simply have to extract the number, and then do another ls .

This is the real hoakey part, I am sure that monkeying with the $VAR designators in bash could produce the same result:

num=`echo $f | sed -e 's/FILE_//' -e 's/\.list//'`

filelist=`ls *${num}*`

gives you the list of matching files.

I'm sure you can do the rest. Remember to remove the already-scanned FILE_*.list from the current directory before you loop...

Sounds like homework, and i would have redirected to: http://www.unix.com/homework-and-coursework-questions/
But since there is already an answer...

for F in FILE*
do 	echo "$F"
	str="${F/*_}"
	for add in [a-z]*_${str/\.*}*
	do 	echo "* $add"
	done
done

Cheers