With zsh
:
(){echo $#} *"$(date +%Y-%m-%d)"*.tgz(DN)
Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz
as a decimal number. Replace (DN)
with (DN[1])
if you want only 0 or 1.
To use as the condition in an if
statement, you can do:
if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then
echo found
else
echo none found
fi
In bash
the equivalent could be:
(shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#")
(replace "$#"
with "$(($#>0))"
to get 0 or 1).
and
if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then
echo found
else
echo none found
fi
With ksh93
:
(FIGNORE='@(.|..)'; set -- ~(N)*"$(date +%Y-%m-%d)"*.tgz; echo "$#")
and
if (FIGNORE='@(.|..)'; set -- ~(N)*"$(date +%Y-%m-%d)"*.tgz; (("$#"))); then...
POSIXly
ls -qA | grep -c "$(date +%Y-%m-%d).*\.tgz$"
for the count.
ls -qA | grep -q "$(date +%Y-%m-%d).*\.tgz$"; echo "$(($? == 0))"
for 0 or 1 and:
And:
if ls -qA | grep -q "$(date +%Y-%m-%d).*\.tgz$"; then...
Though the common wisdom is not to parse the output of ls
, here with -q
, we're making sure there's one file per line and the replacing of non-printable characters with ?
shouldn't affect the grep
ing for our pattern so it should be relatively safe.
You may see differences if the file names contain sequences of bytes that don't form valid characters. One advantage is that you'll get an error message if the current directory is no readable.
-f
enough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?<?php
, and it is advised not to use?>
at the end of a PHP program.