Shell obtains the current system time

Source: Internet
Author: User
Tags what date what date and time

In shell scripts, you often need to obtain the system time to process an operation. Today, the system learns how to obtain the system time. The record is as follows:

Linux system time can directly call system variables in shell, for example:
Get the current period: 'date + % Y % m % d', 'date + % F', or $ (date + % Y % m % d)

The command output result is as follows:

 
 
  1. [root@centi-C sh]# date +%Y%m%d 
  2. 20120727 
  3. [root@centi-C sh]# date +%F 
  4. 2012-07-27 
  5. [root@centi-C sh]# date +%y%m%d 
  6. 120727 

Get the period of yesterday: 'date-d Yesterday + % Y % m % d' can also be written as 'date-D-1day + % Y % m % d'

 
 
  1. [root@centi-C sh]# date -d yesterday +%Y%m%d 
  2. 20120726 
  3. [root@centi-C sh]# date -d yesterday +%F 
  4. 2012-07-26 
  5. [root@centi-C sh]# date -d -1day +%y%m%d 
  6. 120726 
  7. [root@centi-C sh]# date -d -1day +%Y%m%d 
  8. 20120726 

Get date of the day before yesterday: 'date-D-2day + % Y % m % d'
And so on, for example, get the date 10 days ago: 'date-D-10day + % Y % m % d'
Or 'date-d "n days ago" + % Y % m % d' n days ago'
Tomorrow: 'date-d tomorrow + % Y % m % d'
Note that there are spaces in the middle.
 

As for what date and time formats you need, you need to apply the relevant time domain parameters.
The related time domains are as follows:
% H hour (00 .. 23)
% I hour (01 .. 12)
% K hour (0 .. 23)
% L hour (1 .. 12)
% M (00 .. 59)
% P displays am or PM
% R time (HH: mm: SS am or PM), 12 hours
% S number of seconds that have elapsed since 00:00:00, January 1, January 1, 1970
% S seconds (00 .. 59)
% T time (in 24-hour format) (HH: mm: SS)
% X display time format (% H: % m: % s)
% Z Time Zone Date Field
% A abbreviation of the day of the week (Sun .. SAT)
% A full name of the day of the week (Sunday... Saturday)
% B (Jan .. dec)
% B FULL NAME (January... December)
% C date and time (Mon Nov 8 14:12:46 CST 1999)
% D the day of the month (01 .. 31)
% D Date (mm/DD/yy)
The options % H and % B are the same.
% J the day of the year (001 .. 366)
% M month (01 .. 12)
% W the day of a week (0 represents Sunday)
% W the week of the year (00 .. 53, Monday is the First Day)
% X display Date Format (mm/DD/yy)
% Y the last two digits of the year (1999 is 99)
% Y (for example, 1970,1996)
Note: Only Super Users have the permission to use the date command to set the time. Generally, users can only use the date command to display the time.

Add an exercise script. function:

Back up and compress all the content in the/etc directory on the first day of each month, store it in the/root/bak directory, and file name is in the form of YYMMDD_etc, YY is year, mm is month, DD is day. The shell program fileback is stored in the/usr/bin directory.

 
 
  1. #/bin/bash 
  2. #filebak 
  3. #file executable: chmod 755 filebak 
  4. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin 
  5. export PATH 
  6. bakdir="/root/bak/" 
  7. filename="`date +%y%m%d`_etc.tar.gz" 
  8. if [ ! -x "$bakdir" ];then 
  9. mkdir $bakdir 
  10. fi 
  11. cd $bakdir 
  12. tar cvfz $filename /etc

Or use the crontab-e command to add a scheduled task:
0 1 ***/bin/sh/usr/bin/fileback

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.