• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Search

date

Calculate Date to Epoch Time

June 24, 2022

Here’s a script to convert date to epoch time on Mac OS.

#!/bin/bash
if [[ -z $1 ]]; then
  echo "Format: date2epoch.sh 2022/01/01-04:58:06"
else
  echo "$1"; date -j -f date -j -f "%Y/%m/%d-%T" "$1" +"%s"
fi

#!/bin/bash if [[ -z $1 ]]; then echo "Format: date2epoch.sh 2022/01/01-04:58:06" else echo "$1"; date -j -f date -j -f "%Y/%m/%d-%T" "$1" +"%s" fi

Result

$ ./date2epoch.sh 2022/08/31-22:00:00
2022/08/31-22:00:00
1661997600

$ ./date2epoch.sh 2022/08/31-22:00:00 2022/08/31-22:00:00 1661997600

Filed Under: Linux Tagged With: conversion, date, epoch

Set Linux Time Zone

January 27, 2020

Here’s another way of setting Linux time zone.

Check server time.

# check server time
date

# check server time date

Change to local time.

# set to local time
rm /etc/localtime
ln -s /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

# set to local time rm /etc/localtime ln -s /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

Check server time again.

# check server time again
date

# check server time again date

Filed Under: Linux Tagged With: date, localtime, set, time, zone

Calculate Epoch Time to Date

June 27, 2019

Here’s how to calculate epoch time using the Linux date command.

# Linux
date --date @1561266781
date -d @1561266781
# MacOS
date -r 1561266781

# Linux date --date @1561266781 date -d @1561266781 # MacOS date -r 1561266781

Output is : Sun Jun 23 05:13:01 UTC 2019

Show current epoch time.

date +%s

date +%s

Output: 1561642643

Filed Under: Cloud Tagged With: calculate, date, epoch, time

Add date to filenames

June 23, 2019

Here’s how to add date stamps to filenames. Uses epoch time.

tar cpzf mybackup-`date +%s`.gz /home/user

tar cpzf mybackup-`date +%s`.gz /home/user

Filed Under: Linux Tagged With: date, filenames, tar

A month ago

March 19, 2019

How to get last month’s value. Useful with Bash scripts.

LASTMONTH = $(date +%Y%m -d '1 month ago')
echo $LASTMONTH
# Output is ...
201902

LASTMONTH = $(date +%Y%m -d '1 month ago') echo $LASTMONTH # Output is ... 201902

Filed Under: Linux Tagged With: bash, date, month, scripts

  • Go to page 1
  • Go to page 2
  • Go to Next Page »
  • Home
  • About
  • Search

Copyright © 2023