On the 22nd of November 2019, the following code will create a string in $pattern
that will be 20191121|20191120|20191119
. It does this using one call to GNU date
to get the dates, and then concatenates the results with |
as a delimiter. Note that the default date
utility on Solaris can not be used like this, which is why we use gdate
(GNU date
's name on Solaris, by default).
readarray -t dates < <(
cat <<END_DATE_INPUT | gdate -f - +'%Y%m%d'
1 day ago
2 days ago
3 days ago
END_DATE_INPUT
)
pattern=$( IFS='|'; printf '%s' "${dates[*]}" )
This can be used as an extended shell globbing pattern in bash
to delete the wanted directories based on their names, and also to show their sizes (with du
):
shopt -s extglob
du -s -h /opt/png/wsm/data/workdir/batch/*@($pattern)*/
#rm -r -f /opt/png/wsm/data/workdir/batch/*@($pattern)*/
If you have many thousands of these directories, use a loop:
shopt -s extglob
for dirpath in /opt/png/wsm/data/workdir/batch/*@($pattern)*/
do
du -s -h "$dirpath"
#rm -r -f "$dirpath"
done
You may well want to test this with rm
replaced by echo
before running it "live".
If you have access to the zsh
shell (which you do have on Solaris), and you're happy with working by the last-modified timestamp on the directories (i.e. the time at which something was most recently added or deleted in the directory):
du -s -h /opt/png/wsm/data/workdir/batch/*(/m-3m+0)
(and similarly for rm
).
The glob qualifier (/m-3m+0)
makes the preceding pattern only match directories that were modified within the last three days, but more than a day ago. Note that this does not use the filenames of the directories.
bash
4.4.19). – Kusalananda Nov 23 '19 at 14:39