Skip to content

Instantly share code, notes, and snippets.

@piaoger
Last active November 13, 2016 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piaoger/87bb91638deab51149d45d790358e135 to your computer and use it in GitHub Desktop.
Save piaoger/87bb91638deab51149d45d790358e135 to your computer and use it in GitHub Desktop.
handling date in linux bash
# I want compress yesterday's log, so I have to learn how to get yesterday with bash
# yesterday=$(date --date='-1 day' +%Y-%m-%d)
# today=$(date +%Y-%m-%d)
# yesterday_log=service_log_${yesterday}.log
# zip ${yesterday_log}.zip ${yesterday_log}
## today
today=$(date +%Y-%m-%d)
## Yesterday
yesterday_one=$(date --date='1 day ago' +%Y-%m-%d)
yesterday_two=$(date --date='-1 days' +%Y-%m-%d)
echo $yesterday_one
echo $yesterday_two
## tomorrow
tomorrow_one=$(date --date='1 day' +%Y-%m-%d)
tomorrow_two=$(date --date='+1 days' +%Y-%m-%d)
echo $tomorrow_one
echo $tomorrow_two
## other (second/minute/hour/week/month/year)
## more exmaples
date -d "-1 month"
date -d "-1 months"
date -d "+1 month"
date -d "+1 months"
date -d "-1 year"
date -d "-1 years"
date -d "+1 year"
date -d "+1 years"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment