본문 바로가기

리눅스

리눅스 파일명에 날짜 포함시키기 방법(date)

728x90

리눅스 파일명에 날짜 포함시키기 방법(date)

1. date 명령어와 함께 파일명 생성하기

filename="myfile_$(date +%Y%m%d).txt"
touch "$filename"

위의 예시에서 $(date +%Y%m%d)는 현재 날짜를 YYYYMMDD 형식으로 반환하는 date 명령어의 결과를 삽입합니다. touch 명령어는 새로운 파일을 생성합니다.

2. strftime 함수 사용하기

filename="myfile_$(date +"%Y%m%d").txt"
touch "$filename"

위의 예시에서 $(date +"%Y%m%d")는 현재 날짜를 YYYYMMDD 형식으로 반환하는 것입니다.

3. cp 명령어를 사용하여 파일 복사 및 이름 변경하기

cp myfile.txt myfile_$(date +%Y%m%d).txt

위의 예시에서 myfile.txt 파일을 myfile_YYYYMMDD.txt로 복사하여 파일명에 날짜를 포함시킵니다.

728x90

date 명령어

date 명령어는 리눅스 및 유닉스 시스템에서 날짜와 시간 정보를 표시하거나 조작하는 데 사용됩니다. 다양한 옵션을 사용하여 원하는 형식으로 날짜 및 시간을 출력할 수 있습니다.

$ date --help
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.

Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING          display time described by STRING, not 'now'
      --debug                annotate the parsed date,
                              and warn about questionable usage to stderr
  -f, --file=DATEFILE        like --date; once for each line of DATEFILE
  -I[FMT], --iso-8601[=FMT]  output date/time in ISO 8601 format.
                               FMT='date' for date only (the default),
                               'hours', 'minutes', 'seconds', or 'ns'
                               for date and time to the indicated precision.
                               Example: 2006-08-14T02:34:56-06:00
  -R, --rfc-email            output date and time in RFC 5322 format.
                               Example: Mon, 14 Aug 2006 02:34:56 -0600
      --rfc-3339=FMT         output date/time in RFC 3339 format.
                               FMT='date', 'seconds', or 'ns'
                               for date and time to the indicated precision.
                               Example: 2006-08-14 02:34:56-06:00
  -r, --reference=FILE       display the last modification time of FILE
  -s, --set=STRING           set time described by STRING
  -u, --utc, --universal     print or set Coordinated Universal Time (UTC)
      --help     display this help and exit
      --version  output version information and exit

date 명령어에서 자주 사용되는 주요 옵션

  • %Y: 4자리 연도를 나타냅니다.
  • %m: 2자리 숫자로 월을 나타냅니다(01-12).
  • %d: 2자리 숫자로 일을 나타냅니다(01-31).
  • %H: 24시간 형식으로 시간을 나타냅니다(00-23).
  • %M: 분을 나타냅니다(00-59).
  • %S: 초를 나타냅니다(00-59).
  • %A: 요일의 전체 이름을 나타냅니다(예: Sunday).
  • %a: 요일의 축약된 이름을 나타냅니다(예: Sun).
  • %B: 월의 전체 이름을 나타냅니다(예: January).
  • %b 또는 %h: 월의 축약된 이름을 나타냅니다(예: Jan).
  • %Z: 시간대를 나타냅니다(예: PDT, EST).
  • %s: 1970년 1월 1일부터 현재까지의 초 단위로 흐른 시간(에포크 타임)을 나타냅니다.
  • %j: 1년 중 현재 날짜의 일 수를 나타냅니다(001-366).
  • %U: 현재 주를 나타냅니다(00-53). 일요일을 주의 시작으로 간주합니다.
  • %w: 현재 요일을 나타냅니다(0-6). 일요일부터 토요일까지 0부터 6까지의 값을 가집니다.
  • %C: 4자리 연도의 세기를 나타냅니다(예: 20).

예를 들어, date +"%Y-%m-%d %H:%M:%S"와 같이 사용하면 "2023-06-10 14:30:45"와 같은 형식으로 현재 날짜와 시간을 출력할 수 있습니다. man date 명령어를 사용하여 더 많은 옵션과 사용 예제를 확인할 수 있습니다.

 

touch test.`date +%Y%m%d%H%M%s`.txt
touch $HOSTNAME.`date +%Y%m%d%H%M%S`.txt

 

728x90