I was writing a simple script to delete some log files, all the log filenames contain the date of creation in the form YYYYMMDD and I needed to delete the files from previous month keeping the current month. The script will run on the last day of every month.
I thought the task will be very easy as I could use the GNU date command to build the date string, something like:
# The year
Use the date relative option for getting the previous month, something like:
date --date="-1 month" +%m
Then concatenate both strings and build a file pattern using wildcards like ? and *.
The system date was 2019-05-31 ahd I was expecting the 04 string as the output, instead 05 was printed.
I tried several variants and all printed the same, 05:
$ date --date="-1 month" +%m
$ date --date="last month" +%m
I DuckDuckGoed the Internet and found this answer:
This is from the coreutils manual online:
$ date --date="$(date +%Y%m15) -1 month" +%m
Friday, May 31, 2019
GNU date and relatives dates
Posted by J. Ernesto Aneiros at 3:46 PM
Labels: DATE, GNU, relative date
Subscribe to: Post Comments (Atom)
Post a Comment