본문 바로가기
Development/리눅스

리눅스 date 명령어를 이용한 시간 확인 및 설정하기 (hwclock 포함)

by 버들도령 2022. 10. 24.
728x90

리눅스 date 명령어를 이용한 
시간 확인 및 설정하기 (hwclock 포함)

리눅스 서버를 사용하다보면
의도치 않게 시간이 느려지거나 빨라지는 경우가 있습니다.
이런 경우에 시간을 새로 설정해 주어야 하는데요.

아래의 명령어들을 통해서 시간을 설정하는 방법에 대해서 알아봅니다.

현재 시간 확인하기
# date 
2020. 06. 29. (월) 13:50:13 KST

 

시:분:초 시간 설정하기
# date -s 11:22:15
2020. 06. 29. (월) 11:22:15 KST

년-월-일 시24:분:초 시간 설정하기 
# date -s '2021-07-22 12:09:10'
Thu Jul 22 12:09:10 KST 2021

년-월-일 시24:분:초 시간 설정하기
# date -s '2020-07-29 21:05:00'        
Wed Jul 29 21:05:00 KST 2020

MMDDHHMMYYYY.SS 형식으로 시간 설정하기
# date 052620452020.01
Tue May 26 20:45:01 KST 2020

 

표준 시간 표시하기. 옵션은 -u 또는 --utc 또는 --universal 을 사용할 수 있다.
# date -u
2020. 07. 28. (화) 06:42:14 UTC

# date --utc
2020. 07. 28. (화) 06:42:02 UTC

# date --universal
2020. 07. 28. (화) 06:42:09 UTC

 

2일전 시간 확인하기
# date -d '-2 day'
2020. 06. 27. (토) 13:55:04 KST


2시간 전의 시간 확인하기
# date -d '-2 hour'
2020. 06. 29. (월) 11:55:14 KST


2년 전의 시간 확인하기
# date -d '-2 year'
2018. 06. 29. (금) 13:55:28 KST


어제 14시의 시간 확인하기
# date -d 'yesterday 14:00'
2020. 06. 28. (일) 14:00:00 KST


어제 14시의 시간 확인하고, 포맷 지정하기
# date -d 'yesterday 14:00' '+%Y-%m-%d'
2020-06-28


어제 14시의 시간 확인하고, 시분초까지 포맷 지정하기
# date -d 'yesterday 14:00' '+%Y-%m-%d %H:%M:%S'
2020-06-28 14:00:00


1일 전의 시간 확인하고, 시분초까지 포맷 지정하기
# date -d '1 day ago' '+%Y-%m-%d %H:%M:%S'                
2020-06-28 13:57:25


1일전의 시간 확인하고, 시분초까지 포맷 지정하기
# date -d '1 hour ago' '+%Y-%m-%d %H:%M:%S'   
2020-06-29 12:57:38


# date -d '1 month ago' '+%Y-%m-%d %H:%M:%S'    
2020-05-29 13:57:47

1년 이후, 1달 전의 시간 확인하기
# date -d '1 year 1 month ago' '+%Y-%m-%d %H:%M:%S'
2021-05-29 13:58:08


1년 1달 전의 시간 확인하기
# date -d '1 year ago  1 month ago' '+%Y-%m-%d %H:%M:%S' 
2019-05-29 13:58:23


1년 후의 1달 전 시간 확인하기
# date -d '1 year -1 month' '+%Y-%m-%d %H:%M:%S'         
2021-05-29 13:58:39


1년 1개월전의 시간 확인하기
# date -d '-1 year -1 month' '+%Y-%m-%d %H:%M:%S'             
2019-05-29 13:58:53


특정일을 기준으로 5일 이후 시간 확인
# date -d '2020-06-29 + 5 day' '+%Y-%m-%d %H:%M:%S'                
2020-07-04 00:00:00


특정일 기준으로 5달 이후 시간 확인
# date -d '2020-06-29 + 5 month' '+%Y-%m-%d %H:%M:%S'   
2020-11-29 00:00:00


특정일 기준으로 5달 이전 시간 확인
# date -d '2020-06-29 - 5 month' '+%Y-%m-%d %H:%M:%S' 
2020-01-29 00:00:00


특정일 기준으로 5일 이전 시간 확인
# date -d '2020-06-29 - 5 day' '+%Y-%m-%d %H:%M:%S'     
2020-06-24 00:00:00

# date
2020. 07. 28. (화) 15:53:58 KST

현재 시간기준으로 다음 월요일을 확인
# date -d "next mon" 
2020. 08. 03. (월) 00:00:00 KST

인용문구는 홀따옴표와 쌍따옴표 모두 사용 가능합니다.
# date -d 'next mon' 
2020. 08. 03. (월) 00:00:00 KST

현재 시간기준으로 다음 월요일을 확인
# date --date="next mon" 
2020. 08. 03. (월) 00:00:00 KST

# date --date='next mon'  
2020. 08. 03. (월) 00:00:00 KST

 

현재 시간을 기준으로 다음 월요일은? 옵션으로는 mon 또는 monday
# date -d "next mon"            
2020. 08. 03. (월) 00:00:00 KST

현재 시간을 기준으로 다음 화요일은? 옵션으로는 tue 또는 tues 또는 tuesday
# date -d "next tue"            
2020. 08. 04. (화) 00:00:00 KST

현재 시간을 기준으로 다음 수요일은? 옵션으로는 wed 또는 wednes 또는 wednesday
# date -d "next wed"      
2020. 07. 29. (수) 00:00:00 KST

현재 시간을 기준으로 다음 목요일은? 옵션으로는 tue 또는 tues 또는 tuesday
# date -d "next thu"     
2020. 07. 30. (목) 00:00:00 KST

현재 시간을 기준으로 다음 금요일은? 옵션으로는 fri 또는 friday
# date -d "next fri"     
2020. 07. 31. (금) 00:00:00 KST

현재 시간을 기준으로 다음 토요일은?옵션으로는 sat 또는 saturday
# date -d "next sat"    
2020. 08. 01. (토) 00:00:00 KST

현재 시간을 기준으로 다음 일요일은? 옵션으로는 sun 또는 sunday
# date -d "next sun"    
2020. 08. 02. (일) 00:00:00 KST

 

아래와 같이 파일에 날짜를 입력하시고, 저장.
# cat sampleDatefile 
Jul 28 2002
Aug 05 2008

아래와 같이 명령을 입력하면, 데이터 파일에 저장했던 날짜를 읽어서 표시해줍니다.
# date --file=sampleDatefile 
2002. 07. 28. (일) 00:00:00 KST
2008. 08. 05. (화) 00:00:00 KST

 

아래 쉘스크립트는
변수안에 변수를 선언하여
일정한 개월수를 받아서 특정 날짜를 출력하는 것입니다.
추후에 유용하게 사용할떄가 있을겁니다.

#!/bin/bash

echo "Start~~~"
DIFF_MONTH=60
dateDiff=` date -d "${DIFF_MONTH} month" '+%y%m%d' `
echo $dateDiff
echo "Complete..."

 

명령어 시행 기준일 : 2020. 07. 28. (화) 15:10:27 KST
명령어 설명 실행 결과
date +%a locale's abbreviated weekday name (e.g., Sun)
date +%A locale's full weekday name (e.g., Sunday) 화요일
date +%b locale's abbreviated month name (e.g., Jan)  7월
date +%B locale's full month name (e.g., January) 7월
date +%c locale's date and time (e.g., Thu Mar  3 23:05:25 2005) 2020년 07월 28일 (화) 오후 03시 10분 27초
date +%C century; like %Y, except omit last two digits (e.g., 20)   20
date +%d day of month (e.g, 01) 28
date +%D date; same as %m/%d/%y   07/28/20
date +%e day of month, space padded; same as %_d 28
date +%F full date; same as %Y-%m-%d  2020-07-28
date +%g last two digits of year of ISO week number (see %G) 20
date +%G year of ISO week number (see %V); normally useful only with %V 2020
date +%h same as %b    7월
date +%H hour (00..23)  15
date +%I hour (01..12)   3
date +%j day of year (001..366)   210
date +%k hour ( 0..23)   15
date +%l hour ( 1..12) 3
date +%m month (01..12)  7
date +%M minute (00..59)  10
date +%n a newline  개행처리
date +%N nanoseconds (000000000..999999999) 425957510
date +%p locale's equivalent of either AM or PM; blank if not known   오후
date +%P like %p, but lower case  오후
date +%r locale's 12-hour clock time (e.g., 11:11:04 PM)  오후 03시 10분 27초
date +%R 24-hour hour and minute; same as %H:%M 15:10
date +%s seconds since 1970-01-01 00:00:00 UTC   1595916627
date +%S second (00..60)  27
date +%t a tab 
date +%T time; same as %H:%M:%S 15:10:27
date +%u day of week (1..7); 1 is Monday  2
date +%U week number of year, with Sunday as first day of week (00..53) 30
date +%V ISO week number, with Monday as first day of week (01..53)  31
date +%w day of week (0..6); 0 is Sunday  2
date +%W week number of year, with Monday as first day of week (00..53)   30
date +%x locale's date representation (e.g., 12/31/99) 2020년 07월 28일
date +%X locale's time representation (e.g., 23:13:48) 15시 10분 27초
date +%y last two digits of year (00..99) 20
date +%Y year  2020
date +%z +hhmm numeric timezone (e.g., -0400) 900
date +%:z +hh:mm numeric timezone (e.g., -04:00) +09:00
date +%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)   +09:00:00
date +%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) 9
date +%Z alphabetic time zone abbreviation (e.g., EDT)   KST

 

아래는 date 명령어에 대한 옵션과 파라미터에 대한 설명 입니다.

Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

  -d, --date=STRING         display time described by STRING, not `now'
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
  -r, --reference=FILE      display the last modification time of FILE
  -R, --rfc-2822            output date and time in RFC 2822 format.
                            Example: Mon, 07 Aug 2006 12:34:56 -0600
      --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                            TIMESPEC=`date', `seconds', or `ns' for
                            date and time to the indicated precision.
                            Date and time components are separated by
                            a single space: 2006-08-07 12:34:56-06:00
  -s, --set=STRING          set time described by STRING
  -u, --utc, --universal    print or set Coordinated Universal Time
      --help     이 도움말을 표시하고 끝냅니다
      --version  버전 정보를 출력하고 끝냅니다

FORMAT controls the output.  Interpreted sequences are:

  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 20)
  %d   day of month (e.g, 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; same as %Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour ( 0..23)
  %l   hour ( 1..12)
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %N   nanoseconds (000000000..999999999)
  %p   locale's equivalent of either AM or PM; blank if not known
  %P   like %p, but lower case
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M
  %s   seconds since 1970-01-01 00:00:00 UTC
  %S   second (00..60)
  %t   a tab
  %T   time; same as %H:%M:%S
  %u   day of week (1..7); 1 is Monday
  %U   week number of year, with Sunday as first day of week (00..53)
  %V   ISO week number, with Monday as first day of week (01..53)
  %w   day of week (0..6); 0 is Sunday
  %W   week number of year, with Monday as first day of week (00..53)
  %x   locale's date representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (00..99)
  %Y   year
  %z   +hhmm numeric timezone (e.g., -0400)
  %:z  +hh:mm numeric timezone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
  %Z   alphabetic time zone abbreviation (e.g., EDT)

By default, date pads numeric fields with zeroes.
The following optional flags may follow `%':

  -  (hyphen) do not pad the field
  _  (underscore) pad with spaces
  0  (zero) pad with zeros
  ^  use upper case if possible
  #  use opposite case if possible

After any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either
E to use the locale's alternate representations if available, or
O to use the locale's alternate numeric symbols if available.

Report date bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report date translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'date invocation'

 


hwclock 하드웨어 시간 조정하기 (hardware clock)

 

시스템의 하드웨어 시간 확인하기
참고로 현재시각은 '2020-07-29 21:08:28' 이었으니
아래의 하드웨어 시간은 많이 느려져 있네요.
#hwclock
Sat 06 Jun 2020 02:52:08 PM KST  -1.908313 seconds

하드웨어 시간을 시스템의 시간과 일치 시키기
시스템 시간 ▶ 하드웨어 시간으로 설정
'hwclock --systohc' 명령과 같음.
# hwclock -w

# hwclock
Wed 29 Jul 2020 09:08:33 PM KST  -3.104081 seconds


시스템의 시간을 하드웨어 시간과 일치 시키기
하드웨어 시간 ▶ 시스템 시간으로 설정
'hwclock --hctosys' 명령과 같음
# hwclock -s

hwclock의 도움말 확인하기
# hwclock --help
hwclock - query and set the hardware clock (RTC)

Usage: hwclock [function] [options...]

Functions:
  -h | --help         show this help
  -r | --show         read hardware clock and print result
       --set          set the rtc to the time given with --date
  -s | --hctosys      set the system time from the hardware clock
  -w | --systohc      set the hardware clock to the current system time
       --systz        set the system time based on the current timezone
       --adjust       adjust the rtc to account for systematic drift since
                      the clock was last set or adjusted
  -c | --compare      periodically compare the system clock with the CMOS clock
       --getepoch     print out the kernel's hardware clock epoch value
       --setepoch     set the kernel's hardware clock epoch value to the 
                      value given with --epoch
  -v | --version      print out the version of hwclock to stdout

Options: 
  -u | --utc          the hardware clock is kept in UTC
       --localtime    the hardware clock is kept in local time
  -f | --rtc=path     special /dev/... file to use instead of default
       --directisa    access the ISA bus directly instead of /dev/rtc
       --badyear      ignore rtc's year because the bios is broken
       --date         specifies the time to which to set the hardware clock
       --epoch=year   specifies the year which is the beginning of the 
                      hardware clock's epoch value
       --noadjfile    do not access /etc/adjtime. Requires the use of
                      either --utc or --localtime
       --adjfile=path specifies the path to the adjust file (default is
                      /etc/adjtime)
       --test         do everything except actually updating the hardware
                      clock or anything else
  -D | --debug        debug mode
728x90

댓글