I'm trying to compare a file with date in its name to the current datetime of the system:

Filenames are with the following format:

FileName.yyyymmdd.hhmm.file (e.g. File156tr.20220914.0958.txt)

I have came up with the following script:


FROM_FILE=$(ls -1 /some/directory/File156tr.*.*.txt | grep -Eo '[[:digit:]]{8}')
NOW=$(date +"%Y%m%d")

if [ $DIFFERENCE -ge 1 ]; then
    echo "There are files that are older than today's date"

The problem is that the script works perfectly when there's only 1 file of a type there. But for example, if we have the same file, but with different date in it's name (e.g. File156tr.20220913.1053.txt AND File156tr.20220914.0958.txt in the same directory) the script won't work as the ls command will print both dates that are in the filenames.

In case there are multiple files with the same name, but with different datetime in their names (like mentioned above), how can I print out only the newest one of them (the one that's closer to the current date)?

2 Answers 2


That is what loops are for:


for FILE in /some/directory/File156tr.*.*.txt
  FROM_FILE=$(echo $FILE | grep -Eo '[[:digit:]]{8}')
  NOW=$(date +"%Y%m%d")

  if [ $DIFFERENCE -ge 1 ]; then
      echo "There are files that are older than today's date"
  • Please note that your approach would print the message There are files that are older ... for every file to which this condition applies instead of only once. Perhaps you can expand the functionality of the script to accomodate that.
    – AdminBee
    Sep 15, 2022 at 14:41
  • Indeed, it's better than what I did, but it prints all the files that are older than the current date. The script I'm looking for has to print only the newest of all older than today files.
    – joniop
    Sep 15, 2022 at 14:54
  • @joniop, then you need to define "newest" criteria. It can be something like $DIFFERENCE -ge 1 -a $DIFFERENCE -le 7, or you can do a play with with the date to find the beginning/end of month/week, and calculate two "differences". If you want to sort files, you can do for file in $(ls -1 patern | sort). But that is not part of this topic. Open another one, if you want help with sorting of files.
    – White Owl
    Sep 15, 2022 at 21:00

The script I'm looking for has to print only the newest of all older than today files.

I would suggest that you simply build a list of the filenames that exist, remove names matching the current date, and sort the list. The last entry in the list is the most recent. Or if you prefer, sort the list in reverse order, and the first entry in the list is the most recent.

Assuming you don't have any YYYYMMDD date strings in the /some/directory/ path portion, something as simple as this may work:

#!/usr/bin/env bash

  for f in /some/directory/File156tr.*.*.txt
    [[ -f "$f" ]] && printf '%s\n' "$f"
  done |
  sort -r |
  egrep -v "$(date "+%Y%m%d")" |
  head -1

if [[ -z "$mr" ]]
  printf 'There are no files prior to today.\n'
  printf 'The most recent file prior to today is "%s"\n' "$mr"

This code iterates across all files matching the wildcard, concatenating all the filenames and passing them to sort. sort sorts them into descending order, and removes any filenames that contain a YYYYMMDD string matching today's date. The output of sort is piped to head -1 to return only the single highest-sorting filename, or possibly the empty string if there are no files older than today.

  • Thanks man, works perfectly, I'll add some additional things according to my needs, but that's perfect and all I was looking for.
    – joniop
    Sep 16, 2022 at 9:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .