Finding 4 current files having specific File Name pattern

Hi All,
I am trying to find 4 latest files inside one folder having following File Name pattern and store them into 4 different variables and then use for processing in my shell script. File name is fixed length.
1) Each file starts with = ABCJmdmfbsjop letters + 7 Digit Number _mm-dd-yyyy-hh-mm-ss-3 Digit Number.data
where ABCJmdmfbsjop :- is alphabet. Each file starts with this ABCJmdmfbsjop alphabet.
7 Digit Number :- Numeric 7 digits.
__:- Underscore character
mm-dd-yyyy-hh-mm-ss :- This will be today's month-date-Year-Hour-Minutes-seconds
-
3 digit number This cauld be any number.
.data:- this will be extension of the file.

For example:- ABCJmdmfbsjop0001552_11-02-2012-13-01-10-962.data

Any help regarding this will be appreciated.
Thanks,
-Lance

If your file name is fixed length you can simply use cut:-

fname="ABCJmdmfbsjop0001552_11-02-2012-13-01-10-962.data"
echo $fname | cut -c 1-13
echo $fname | cut -c 14-20
echo $fname | cut -c 22-40
echo $fname | cut -c 42-44
echo $fname | cut -c 46-50

Hi Bipinath,
Thanks for your help on this.
But the provided solution is not working. Here we are trying to find 4 different file names which are inside one folder and store them into 4 different variable meeting following file name criteria:-
Each file starts with = ABCJmdmfbsjop letters + 7 Digit Number _mm-dd-yyyy-hh-mm-ss-3 Digit Number.data
where ABCJmdmfbsjop :- is alphabet. Each file starts with this ABCJmdmfbsjop alphabet.
7 Digit Number :- Numeric 7 digits.
__:- Underscore character
mm-dd-yyyy-hh-mm-ss :- This will be today's month-date-Year-Hour-Minutes-seconds
-
3 digit number This cauld be any number.
.data:- this will be extension of the file.

For example:- ABCJmdmfbsjop0001552_11-02-2012-13-01-10-962.data

TS=`date +"%m-%d-%Y"`

find . -name "ABCJmdmfbsjop*${TS}*"

---------- Post updated at 11:13 ---------- Previous update was at 11:08 ----------

find . -name "ABCJmdmfbsjop???????_${TS}*.data"
1 Like

Thanks bipinajith. You solution worked and listed required files.
Now How i can copy these 4 files name listed by find . -name "ABCJmdmfbsjop???????_${TS}*.data" coammnd into 4 variables like Var1, var2, var2 & var4.
Thanks a lot for your help regarding this.

You can use a for loop and use the same cut statement which I provided before for variable assignments:-

for fname in `find . -name "ABCJmdmfbsjop???????_${TS}*.data" | sed 's/\.\///g'`
do
   # use those cut statements here
done

I hope it help.

1 Like

That's why it is good to get use to use YYYYMMDD_HHMMSS notation so that they get ordered by name naturally

1 Like