I am trying to write a script in bash that will determine the oldest directory in a file structure.
In this example, I have four directories with the following titles that mimic the date and time they were created. The files are 2022-02-21:20:30:23
, 2022-02-21:22:30:23
, 2022-02-28:22:30:23
, and 2023-02-28:22:30:23
. In this case, the dates are arbitrary and were just developed to test the script.
I want the script to identify the directories that exist within this directory, and based on the name of the directory, identify which one is the oldest directory.
My initial attempt at doing this is shown below, and yes, I know the strings are not properly declared as dates, but I do not know how to do this and could use some suggestions.
oldest="1899-01-01:01:01:01"
dirs=`ls -d */`
for i in $dirs ; do
if [[ oldest < $i ]] ; then
oldest=$i
fi
done
echo $oldest