[Linux] Date 날짜 설정 및 데이트 관련 명령어

 리눅스 Date 명령어 (출력하기) 

 

리눅스에서 Date 명령을 잘 활용하면 다양한 아이디어를 얻을 수 있습니다. 지금 무슨 분기 인지? 몇 주째 되는지 등등 고려해서 스크립트 등을 만들 수 있습니다. 그럼 간단한 사용법을 알아보겠습니다.

linux date 명령어


 

기본적으로 현재 날짜를 출력하는 방법입니다.

$ date
Tue Sep 20 12:12:22 PM KST 2022
view raw blog1.bash hosted with ❤ by GitHub

 

 

 

다음은 년도만 출력합니다.

$ date '+%Y'
2022
view raw blog2.bash hosted with ❤ by GitHub

 

 

 

연도의 뒷자리 2글자만 표현하려면 소문자 y를 사용합니다. 명령 대부분이 대문자, 소문자에 따라 달라집니다.

$ date '+%y%m%d'
220920
view raw blog3.bash hosted with ❤ by GitHub

 

 

 

시간만 나타내려면 다음과 같이 T 문자를 사용합니다.

$ date '+%T'
12:14:40
view raw blog4.bash hosted with ❤ by GitHub

 

 

 

그 밖에 다양한 옵션들입니다.

%% 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; like %+4Y-%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, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%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
%q quarter of year (1..4)
%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 time zone (e.g., -0400)
%:z +hh:mm numeric time zone (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)
view raw blog5.txt hosted with ❤ by GitHub

 

 

 

위의 명령어중 예를 들어 현재 타임존을 알고 싶다면 다음과 같이 Z를 입력합니다. 그리고 ' ' 구문 안에 여러 리터럴들을 추가할 수 있습니다.

$ date '+%Z'
KST
view raw blog6.bash hosted with ❤ by GitHub

 

 

 

 리눅스 Date 명령어 (수정하기) 

 

이제 날짜을 수정해보겠습니다. 다음과 같이 날짜를 수정합니다.

$ date -s "2022-07-07"
view raw blog7.bash hosted with ❤ by GitHub

 

 

 

이제 전체 날짜와 시간을 수정해보겠습니다.

$ date +%D -s "2022-07-07 14:07:07"
view raw blog8.bash hosted with ❤ by GitHub

 

 

 

마지막으로 시간만 수정해보겠습니다.

$ date +%T -s "07:07:00"
view raw blog9.bash hosted with ❤ by GitHub

Designed by JB FACTORY