Distinct filenames pattern

Hi All,

I am working on designing the archival process for my system, where I will have to find distinct file names ( when excluded time_stamp extention ) from given directory and for each file type keep the latest and move all other older to different location ( lets say dir Back ). Below are example of files I will have :

-rw-r--r-- 1 user_id grp_name 10445 Oct 17 10:04 abc.dat.20141017_100517
-rw-r--r-- 1 user_id grp_name 10445 Oct 17 10:57 abc.dat.20141017_105829
-rw-r--r-- 1 user_id grp_name 10445 Oct 17 11:13 mno.dat.20141017_111422
-rw-r--r-- 1 user_id grp_name 10445 Oct 17 16:01 mno.dat.20141017_160144

==============:
I would expect to get distinct list and for each, keep latest :
So for above example I have 2 distinct file_type ( excluding time_stamped extention ) ==> abc.dat / mno.dat

then once i have list, I need to move older and keep latest.
for ex. for abc.dat :
I will keep abc.dat.20141017_105829 and move abc.dat.20141017_100517
to backup dir.

Is there any easy implementation for this one??

Thanks in advance.

Please use code tags as required by forum rules!

Why don't you adapt / extend the solution to your former problem? That'd be an easy implementation!

If this code works for you and if you keep it (and execute it) in the directory where all the distinct files are initially stored, you can run it again and again without modification. There should not be two completely identical files in the source and destination dir (backup dir), though, else the file in the destination dir will be overwritten.

#!/bin/bash

# create file list with distinct files
for file in *.dat*; do
 echo "${file%%.*}"
done | sort | uniq -d > distinct.txt

# exit if there are no distinct files
if ! [ -s "distinct.txt" ]; then
 echo "No distinct files."
 rm -f distinct.txt
 exit
fi

# check if backup dir exists, else create it in the current dir
[ -d "backup" ] || mkdir backup

# read from previously created file list
while read dfile; do
x=1
# list available files for the particular distinct file, newest first
# skip very newest file, move the rest into backup dir
 ls -t "$dfile"* |\
  while read line; do
   [ $x -eq 1 ] && x=0 && printf "%s\n" "skipping \"$line\" ..." && continue
   echo mv "$line" "backup"
  done
  x=1
done < distinct.txt

# delete file list
rm -f distinct.txt

If the output of this script is correct, remove the echo command to make the mv commands work.