How can I sort the below files based on the date and time inside the folder and delete the older ones and keep the most recent ones in Linux?

I just want to keep the latest one regardless of the actual date. It does not matter if the most recent file is even a year old, I just want to keep that.

aa_zz_yuyubc_207_dc_ghgh.brr.01_30_2019 3_15_04 am.csv
aa_zz_chuucd.brr.04_30_2020 7_15_12 pm.csv
aa_zz_ddetr_354_m.brr.5_25_2021 03_28_00 am.csv
aa_zz_xyf.brr.6_05_2021 5_20_55 PM.csv

Expected result: only aa_zz_xyf.brr.6_05_2021 5_20_55 PM.csv remains:

Dates are in <month>_<day>_<year> <hour>_<minute>_<second> am/pm (12 hour format).


  • 2
    Please edit your question and give us more details. Are you really using Unix? Which one? Do you maybe mean Linux instead? How can we know the age of the file? Should we try to parse the file's name, or should we take the age from the file system?
    – terdon
    Nov 21 at 14:43
  • 3
    Does this answer your question? Find pattern: sort and delete older files
    – alecxs
    Nov 21 at 19:52
  • @alecxs this question looks like a simpler version of the other one, so they're not strictly speaking duplicates. Nov 22 at 9:15

With zsh, you could do:

zmodload zsh/datetime
echo rm -f -- *.brr.*.csv(nOe['
    LC_ALL=C strftime -rs REPLY "%m_%d_%Y %I_%M_%S %p" ${${REPLY:r}##*.brr.}

(remove echo (for dry-run) if that looks alright)

We use zsh's Oe glob qualifier to sort in reverse (capital O), numerically (with n) based on the value of $REPLY as returned by the code in [...].

That code uses strftime -r (strftime in reverse which in effect is strptime) to convert the time stamp in the file name to the epoch time in seconds. Then, all we have to do is select only the 2nd to last ([2,-1]) to pass the rm, leaving only the one with the most recent timestamp.

  • 1
    @alecxs, the OP mentions nothing about any prefix that would have not to be ignored here. But if you wanted to only consider file names that start with aa_zz, it would just be a matter of changing *.brr.*.csv to aa_zz*.brr.*.csv Nov 22 at 10:28

Just use the xtime functions in find command

access time creation time modification time


find toto/ -name "*csv" -mtime +90 -exec rm {} \;

removes files only in toto/ how have a mtime > 90 days.

if you are sure you wanna keep only the last one whatever the date is corresponding or not to your needs (file name vs real dates of creation/modification/Access) of the file)

you might use something like to isolate the file to keep

 ls | sort -t_ -k5 -k4 -k3 | tail -1

here you need the exact reverse of it

number=$(ls | wc -l) # word cound by line
while read ; do  rm "${REPLY}" ; done < <(ls | sort -t_ -k5 -k4 -k3 | head -$(($number - 1)))

example :

 while read ; do  rm "${REPLY}" ; done < <(ls | sort -t_ -k5 -k4 -k3 | head -$(($number - 1)))
'aa_zz_ddetr_354_m.brr.5_25_2021 03_28_00 am.csv'
  • 1
    If the OP's file names represent their age, this command would remove all of them. In any case, the OP isn't asking how to delete old files but how to delete some and keep the newest.
    – terdon
    Nov 21 at 14:46
  • so delete your post & write a correct one
    – francois P
    Nov 21 at 14:49
  • 1
    Sorry? The only incorrect post here is your answer, is that what you want me to delete? I haven't posted anything.
    – terdon
    Nov 21 at 14:58
  • 2
    OK, you need to calm down now. I am not blaming you, I am simply pointing out that although the question is not clear, the one thing that is clear is that the file aa_zz_xyf.brr.6_05_2021 5_20_55 PM.csv should be kept and your answer would delete it. You are answering "how can I delete files older than X" and the question here is "how can I delete some files but keep the newest". That's all. You can edit the answer or ignore me, up to you.
    – terdon
    Nov 21 at 15:22
  • 1
    you might add -n flag because columns missing leading zeros (even more funny on the linked duplicate there exist files with different delimiter than _ for example 'cc_co_eeffyy_cc.err.04_10-2020 12_00_00 am.csv') and did you notice the time with am/pm suffix?
    – alecxs
    Nov 21 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.