Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of common commands for shell scripts to obtain time in Linux environment

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "the summary of common commands for obtaining time in shell scripts in Linux environment". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Related concepts

The system time of Linux can be directly called the system variable date in shell, such as:

Get today's date: `1day +% Y% m% d` or `date +% F` or $(date +% y%m%d) get yesterday's period: `date-d yesterday +% Y% m% d` can also be written as `date-d-1day +% Y% m% d` to get the day before yesterday: `date-d-2day +% Y% m% d` to get the date 10 days ago: `date-d-10day +% Y% m% d` or n days ago: `date-d "n days Ago "+% y% m% d` get tomorrow's date: `date-d tomorrow +% y% m% d` get last month's date: date-d last-month +% Y% m get next month's date: date-d next-month +% Y% m

As for what kind of date and time format you need, you need to apply the relevant time domain parameters to achieve it:

The relevant time domains are as follows:

% H hour (00.23)% I hour (01.12)% k hour (0.23)% l hour (1.12)% M minute (00.59)% p shows AM or PM% r time (hh:mm:ss AM or PM) 12 hours% s from 00:00:00 on January 1, 1970 to the present number of seconds% s seconds (00.59)% T time (24-hour system) (hh:mm:ss)% X display time format (% H:%M:%S)% Z time zone date domain% an abbreviation (Sun..Sat)% A full name of day of the week (Sunday..Saturday)% b month (Jan..Dec)% full name of B month (January..December)% c date and time (Mon Nov 8 14:12:46 CST 1999)% d Day of a month (01.. 31)% D date (mm/dd/yy)% h and% b option same% j Day of a year (001.. 366)% m month (01.. 12)% w Day of a week (0 represents Sunday) )% W the week ordinal of the year (00.53) Monday is the first day)% x display date format (mm/dd/yy)% last two digits of y year (1999 is 99)% Y year (e.g., 1970, 1996, etc.)% F output date is in the format 2019-10-20

Note: only superusers have permission to use the date command to set the time, and ordinary users can only use the date command to display the time.

two。 Get time script-DateTime.sh

#! / bin/bash echo "= =" echo "show linux original format date and time:" echo DateTime: $(date) echo "= =" echo "show date time like format: YYYY-MM-DD HH:MM:SS" NOW_DATE_TIME=$ (date "+% Y-%m-%d% H:%M:%S") echo $NOW_DATE_TIME echo "=" echo "show date time like format: YYYYMMDD-HHMMSS" NOW_TIME=$ (date "+% Y% m% dmurf% H% M%) S ") echo $NOW_TIME echo" = = "echo" show last year: "LAST_YEAR=$ (date" +% Y-%m-%d% H:%M:%S "--date="-1 years ") echo $LAST_YEAR echo" = "echo" show next year: "NEXT_YEAR=$ (date" +% Y-%m-%d% H:%M:%S "--date=" 1 years ") echo $NEXT_YEAR echo" = = "echo" show last month: "LAST_ MONTH=$ (date "+% Y-%m-%d% H:%M:%S"-- date= "- 1 months") echo $LAST_MONTH echo "= =" echo "show next month:" NEXT_MONTH=$ (date "+% Y-%m-%d% H:%M:%S"-- date= "1 months") echo $NEXT_MONTH echo "= =" echo "show last day:" LAST_DAY=$ (date "+% Y-%m-%d% H:%M:%S" "--date="-1 days ") echo $LAST_DAY echo" = = "echo" show next day: "NEXT_DAY=$ (date" +% Y-%m-%d% H:%M:%S "--date=" 1 days ") echo $NEXT_DAY echo" = "echo" show last hour: "LAST_HOUR=$ (date" +% Y-%m-%d% H:%M:%S "- date="-1 hours ") echo $LAST_HOUR echo" = = "echo" "show next hour:" NEXT_HOUR=$ (date "+% Y-%m-%d% H:%M:%S"-- date= "1 hours") echo $NEXT_HOUR echo "= =" echo "show last minute:" LAST_MINUTE=$ (date "+% Y-%m-%d% H:%M:%S"-- date= "- 1 minutes") echo $LAST_MINUTE echo "=" echo "show next minute:" NEXT_MINUTE=$ (date "+% Y-%m-%d) % H:%M:%S "--date=" 1 minutes ") echo $NEXT_MINUTE echo" = = "echo" show last second: "LAST_SECOND=$ (date" +% Y-%m-%d% H:%M:%S "--date="-1 seconds ") echo $LAST_SECOND echo" = "echo" show next second: "NEXT_SECOND=$ (date" +% Y-%m-%d% H:%M:%S "- date=" 1 seconds ") echo $NEXT_SECOND echo" = = "

Running result:

3. Practice script

Back up and compress all the contents of the / etc directory on the first day of each month and store them in the / root/bak directory with the following file name: yymmdd_etc,yy for the year, mm for the month, and dd for the day. The Shell program fileback is stored in the / usr/bin directory.

# / bin/bash # filebak bakdir= "/ root/bak/" filename= "`date +% y% m% d` _ etc.tar.gz" if [!-x "$bakdir"]; then mkdir $bakdir fi cd $bakdir tar cvfz $filename / etc # add scheduled tasks using the crontab-e command: 01 * / bin/sh / usr/bin/fileback "Summary of common commands for obtaining time in Linux environment shell scripts". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report