Friday, May 31, 2019

GNU date and relatives dates

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
date +%Y

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
05

$ date --date="last month" +%m
05

I DuckDuckGoed the Internet and found this answer:

https://stackoverflow.com/questions/13168463/using-date-command-to-get-previous-current-and-next-month

This is from the coreutils manual online:

https://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html#Relative-items-in-date-strings

$ date --date="$(date +%Y%m15) -1 month" +%m
04

Wednesday, May 8, 2019

PDFs don't open automatically in Chrome

You can normally open PDFs automatically in Chrome by clicking on the file you want to see. If your PDFs are downloading instead of opening automatically in Chrome, Chrome PDF viewer could be turned off.

Open PDFs in Chrome

  1. On your computer, open Chrome.
  2. At the top right, click More More and then Settings.
  3. At the bottom, click Advanced.
  4. Under "Privacy and security," click Site settings.
  5. Near the bottom, click PDF documents.
  6. Turn off Download PDF files instead of automatically opening them in Chrome.
Chrome will now open PDFs automatically when you click them.