Reputation: 1133
I have this folders:
2014-09-01-00:00:01
2014-09-01-01:00:01
2014-09-01-02:00:01
2014-09-01-03:00:01
2014-09-01-04:00:01
(There are many more folders)
I write this names to an array. (folders=("2014-09-01-00:00:01" "2014-09-01-01:00:01"...)
)
How can I get the folder with the newest date? (Not based on the creation/modified date)
Upvotes: 1
Views: 406
Reputation: 246827
bash filename pattern expansion gives you alphabetically sorted results, so:
folders=(*/)
echo "${folders[-1]}"
Upvotes: 0
Reputation: 2338
you can use below command
ll -rt | tail -n 1
Actually it will give you the latest file or directory in your parent directory.
But as you added in your question that your parent directory contains only directories. You can simply use above command.
Upvotes: 0
Reputation: 289745
ls
is the first thing to think about, but parsing ls is evil. Hence, I would use find
for this:
find /your/path -mindepth 1 -maxdepth 1 -type d | sort -rn
It looks for all the directories in /your/path
(not sub-directories) and sorts them numerically.
The first one will be the newer. Adding | head -1
we get just that one.
Upvotes: 2
Reputation: 64318
Given the convenient date/time format you're using, you can simply sort lexicographically, using sort
:
ls -d PARENT_DIR/*/ | sort | tail -1
Upvotes: 0