Shell date-time and timestamp conversions

Source: Internet
Author: User

Gitlab backup files are displayed in timestamps, similar to: 1438624820_gitlab_backup.tar to make it easier toread, you want to convert the file name to a date format:2015-08-04_gitlab_ Backup.tar

So I looked for the method of time conversion in the shell and recorded the following memo.

Some commands for Linux under time conversion:
    • Date +%s can get Unix timestamp;
    • Use the shell to turn the date time and timestamp to each other:

date-d "2015-08-04 00:00:00" +%s output:1438617600

    • And the time stamp is converted to a string to do this:

date-d @1438617600 "+%y-%m-%d" output:2015-08-04

    • If you need to get the days before and after the specified date:
      Seconds= ' date-d "2015-08-04 00:00:00" +%s ' #得到时间戳
      seconds_new= ' expr $seconds + 86400 ' #加上一天的秒数86400
      date_new= ' date-d @ $seconds _new "+%y-%m-%d" ' #获得指定日前加上一天的日前
To modify the script for the Gitlab backup file name:

Shell date-time and timestamp conversions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.