This is far from clear. Does it mean "for each of the groups defined by the first four characters, select the file with date stamp less or equal to the date parameter string given"?
So if I am passing a variable of ( 20180118 ) should give me:
abcdefgh_20180102.csv
xyz_20180102.csv
and the variable ( 20180121 ) should give me:
abcdefgh_20180120.csv
xyz_20180121.csv
The timestamps of the files has to be ignored as the filenames can be past dated with current timestamps. So I should only look at the filenames and the last 8 characters of the filename representing the date.
Perhaps i'm missing something in your requirement, but this script will print files in the current directory for the date string passed as argument #1 (defaults to today's date if no parameters passed):
DT=${1:-$(date +%Y%m%d)}
for file in *_${DT}.csv
do
[ -f "$file" ] && echo "$file"
done
I had tried this, but I think the code is reading the string after the first occurrence of "_" as the basis to identify the date. However if few of my files has the naming convention of following, how do we deal with it?