Compare directory dates

hi, I need to know if a specific directory exists in a folder named after the date of yesterday (02/06/2015)

The problem is simple but not how to do it. :confused:

i= date -d "yesterday" '%Y-%m-%d'  <- the format of directory is 2015-06-02

if in /var/logroot/index exist directory whit yesterday name...
echo "yes"
else
echo "no"
fi

---------- Post updated at 10:10 AM ---------- Previous update was at 09:40 AM ----------

I have the answer!

i= ls /var/logroot/ | grep $(date -d "yesterday" '+%Y-%m-%d')

if -d $i; then
echo "yes"
else
echo "no"
fi

I guess you have

...
if [ -d "$i" ]; then
...

if -d /var/logroot/$(date -d "yesterday" '+%Y-%m-%d')
.....

No. Neither:

if -d  /var/logroot/$(date -d "yesterday" '+%Y-%m-%d')

nor:

if -d $i; then

are valid commands on UNIX and Linux systems unless you have created your own -d utility. And, creating a utility with a name starting with a hyphen isn't a good idea. (There are too many contexts where a command name starting with a hyphen won't be recognized as a command name.)

You can use the code MadeInGermany suggested:

if [ -d "$i" ]; then

or the synonym:

if test -d "$i"; then

to determine whether the string contained in the expansion of $i is a directory.

And, the code:

i= ls /var/logroot/ | grep $(date -d "yesterday" '+%Y-%m-%d')

isn't correct either, unless you are trying to run the ls utility with the variable i set to an empty string in its environment. Even using:

i="$(ls /var/logroot/ | grep $(date -d "yesterday" '+%Y-%m-%d'))"
if [ -d "$i" ]; then

won't do what you want unless you can guarantee that there will never be more than one file in that directory that contains yesterday's date as part of its name.

A safer (and faster) way to do something like this would be:

for i in *$(date -d yesterday +%Y-%m-%d)*
do	if [ -d "$i" ]
	then	printf 'Yes, Processing directory "%s":\n' "$i"
		# Do whatever else you want to do with matching directories here...
	else	printf 'No, Skipping "%s"\n' "$i"
	fi
done

But, note also that dd/mm/YYYY (the date format you used as an example) and YYYY-mm-dd (the date format created by your invocation of the date utility) are not the same. And, if you expect a filename in one format to match the string produced by date , you had better be sure that they are using the same format.

Don Cragun, tons of thanks for show me the MadeInGermany way. Amazing explanation.

Although with my method i have the solution i think your german way its more simple, and yep, never have 2 subdirectorys (with date name) in the same directory, its a automatic rutine to create dirs.

again, thanks!!