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:
- [root@centi-C sh]# date +%Y%m%d
- 20120727
- [root@centi-C sh]# date +%F
- 2012-07-27
- [root@centi-C sh]# date +%y%m%d
- 120727
Get the period of yesterday: 'date-d Yesterday + % Y % m % d' can also be written as 'date-D-1day + % Y % m % d'
- [root@centi-C sh]# date -d yesterday +%Y%m%d
- 20120726
- [root@centi-C sh]# date -d yesterday +%F
- 2012-07-26
- [root@centi-C sh]# date -d -1day +%y%m%d
- 120726
- [root@centi-C sh]# date -d -1day +%Y%m%d
- 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.
- #/bin/bash
- #filebak
- #file executable: chmod 755 filebak
- PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
- export PATH
- bakdir="/root/bak/"
- filename="`date +%y%m%d`_etc.tar.gz"
- if [ ! -x "$bakdir" ];then
- mkdir $bakdir
- fi
- cd $bakdir
- tar cvfz $filename /etc
Or use the crontab-e command to add a scheduled task:
0 1 ***/bin/sh/usr/bin/fileback