Script Rename files

Hello,
I have this problem.
In a directory I have 4 csv files with this format:

PHOENIX_KM_INTERAZIONI_YYYYMMDD.csv
PHOENIX_KM_TRIPLETTE_YYYYMMDD.csv
NEWCAB_KM_INTERAZIONI_YYYYMMDD.csv
NEWCAB_KM_INTERAZIONI_YYYY_MM_DD.csv

YYYYMMDD: format CURRENT date

I wont rename all files in this format:

PHOENIX_KM_INTERAZIONI.csv
PHOENIX_KM_TRIPLETTE.csv
NEWCAB_KM_INTERAZIONI.csv
NEWCAB_KM_INTERAZIONI.csv

The script must rename all files, truncate "_YYYYMMDD" EVERY DAY.

Anyone can help me, please?

Thanks a lot

Andrew

ls | cat | awk '{f=$0;sub("_[^_]+$",".csv");system("mv " f " " $0)}'

I don't usually point out UUoC instances, but that one is quite egregious :wink:

A cheaper version of your code:

for f in *_*.csv; do mv "$f" "${f%_*}.csv"; done

However, I'm not sure how NEWCAB_KM_INTERAZIONI_YYYYMMDD.csv and NEWCAB_KM_INTERAZIONI_YYYY_MM_DD.csv should be handled. If the date portion of the filename is properly stripped from both, the resulting filenames would be identical and the second file processed would clobber the first. Our proposed solutions only strip the final/only date segment, so there is no clobbering. How this should be handled if this result isn't good enough should be clarified by the original poster.

Regards,
Alister

#!/bin/bash
i=1
 ls -1 *.csv > list
while read file
  do
newfile=$(sed -e 's/\(.*\)\(_YY......\).*/\1/' -e $i\ '!d' list).csv
mv $file $newfile
((i++))
done < list
rm -f list
$ ls *KM*[0-9][0-9].csv | while read line; do
new=$(echo "$line" | sed 's/_[0-9]\{4\}_\{0,1\}[0-9][0-9]_\{0,1\}[0-9][0-9]//')
mv -v "$line" "$new"
done
NEWCAB_KM_INTERAZIONI_22221130.csv -> NEWCAB_KM_INTERAZIONI.csv
NEWCAB_KM_INTERAZIONI_2222_11_30.csv -> NEWCAB_KM_INTERAZIONI.csv
PHOENIX_KM_INTERAZIONI_22221130.csv -> PHOENIX_KM_INTERAZIONI.csv
PHOENIX_KM_TRIPLETTE_22221130.csv -> PHOENIX_KM_TRIPLETTE.csv
$ 

---------- Post updated at 21:01 ---------- Previous update was at 20:56 ----------

Okay, now I see why this is no option :rolleyes: