Delete files older than 1week(dates need to be calculate based on file name)

Objective: We have multiple files in a folder and we want to delete all files except for last 1 week files.

Note: We are copying these files from original location to this temporary location. So time shown for these files are time when we copied to this location. Not that when file was created.

Files are:

-rw-rw-r-- 1 root root   3730573 Jun 23 14:06 tps-20170516192956200-10248.tps
-rw-rw-r-- 1 root root  34803468 Jun 23 14:06 tps-20170516195930440-2586.tps
-rw-rw-r-- 1 root root  33504240 Jun 23 14:06 tps-20170517231247323-2586.tps
-rw-rw-r-- 1 root root  33247339 Jun 23 14:07 tps-20170519030152674-2586.tps
-rw-rw-r-- 1 root root  33283294 Jun 23 14:07 tps-20170519030525811-2586.tps
-rw-rw-r-- 1 root root  33211241 Jun 23 14:07 tps-20170520120924116-26089.tps
-rw-rw-r-- 1 root root  35135540 Jun 23 14:07 tps-20170521203131446-45475.tps
-rw-rw-r-- 1 root root  27263002 Jun 23 14:07 tps-20170522171753527-45475.tps
-rw-rw-r-- 1 root root  34145738 Jun 23 14:07 tps-20170522180142517-45475.tps
-rw-rw-r-- 1 root root  33248824 Jun 23 14:08 tps-20170523210831497-45475.tps
-rw-rw-r-- 1 root root  33497660 Jun 23 14:08 tps-20170523223946192-45475.tps
-rw-rw-r-- 1 root root  33140593 Jun 23 14:08 tps-20170524004838503-45475.tps
-rw-rw-r-- 1 root root  33700101 Jun 23 14:08 tps-20170524183206292-45475.tps
-rw-rw-r-- 1 root root  33285239 Jun 23 14:08 tps-20170525004909816-45475.tps
-rw-rw-r-- 1 root root  18466717 Jun 23 14:09 tps-20170525171625377-45475.tps

The date of the file can be found in file name itself.

tps-20170525171625377-45475.tps = 2017-05-25

Query: In above example we have files from 16-05-2017 to 25-05-2017. How can keep files only for last seven days i.e. 19th to 25th may and remove rest of them.

Thanks
Ankit

For a zero'th approximation, try

for FN in *.tps; do DAT=${FN#*-}; DAT=${DAT:0:8}; touch -d${DAT} $FN; done
touch -d$((DAT - 7)) REF
find . -newer REF
./tps-20170523210831497-45475.tps
./tps-20170519030152674-2586.tps
./tps-20170520120924116-26089.tps
./tps-20170521203131446-45475.tps
./tps-20170525004909816-45475.tps
./tps-20170524183206292-45475.tps
./tps-20170523223946192-45475.tps
./tps-20170522171753527-45475.tps
./tps-20170525171625377-45475.tps
./tps-20170524004838503-45475.tps
./tps-20170522180142517-45475.tps
./tps-20170519030525811-2586.tps

Please not that this is just a quick and dirty idea on how to proceed; it assumes the file sorted last is the newest one and the one to start calculating backwards from, it is not month end nor year end safe, and it requires shell arithmetic as provided by e.g. bash (recent).

Would it be a problem to extract the file creation time from the file name and use touch to set the time correctly? You can then use find far easier.

Is the date format year, month, day, hour, minute, second, millisecond? You can grab that bit and make a decision on that if you calculate the date before you want to erase the files, something like this:-

#!/bin/bash

# Calculate the cut-off value
earliest_date_allowed=$(date +%Y%m%d%H%M%S -d"1 week ago")
echo "\$earliest_date_allowed=\"$earliest_date_allowed\" or $(date -d"1 week ago")"
while read file
do
   # Extract the date part
   filedate="${file#*-}"            # removes up to first hyphen of filename
   filedate="${filedate%???-*}"     # Removes milliseconds to the end
   if [ $filedate -lt $earliest_date_allowed ]
   then
      echo "I would purge $file"
   fi
done < <(ls -1)                     # Run ls as input for the loop

It doesn't work with milliseconds though and relies on GNU-date. Are these a problem?

Does this get you close?
Robin