Skull Skull - 2 months ago 10
Bash Question

Shell script to find folder

I have few randoms date folders on location /Users/skull/Desktop
and i want to select/find the folder of today's date and if not there then select less nearest to todays date. Should not take high date folder
eg: Today date 2016-09-08 as on location we don’t have 2016-09-08 folder so it should take less nearest date which is 2016-09-06 or if 2016-09-06 not there then 2016-08-30

Folder names:
2016-07-015

2016-07-27

2016-08-30

2016-09-05

2016-09-06

2016-09-10

2016-09-15

2016-09-20

location=/Users/Skull/Desktop
DAY=$(date +"%Y-%m-%d"| awk '{print $1$2$3}')
#2016-09-08
DATA_FOLDER=


kindly hep me on this guys with the script.

Answer

Output the names in reverse order. Then use an awk script that prints the first one that's less than or equal to the current date.

DATA_FOLDER=$(ls -R "$location" | awk -v day=$DAY '$0 <= day {print; exit}')