`stat` 출력 형식: 날짜 필드 자르기

`stat` 출력 형식: 날짜 필드 자르기

stat한 가지 형식(적어도 Linux의 bash에서 얻은 형식) 에서는 형식 수정자를 사용할 수 있습니다. 예를 들어 %010s크기 필드를 10자 이상으로 설정하고 왼쪽을 0으로 채웁니다(그런데 이 작업은 Recorded 어딘가에서 작동합니까). ?)

필드 길이를 제한하는 동일한 트릭이 있습니까? %xyz 형식에서 초의 소수 부분을 제거하고 싶습니다. 아니면 출력을 사후 처리하기 위해 sed/awk를 사용해야 합니까?

답변1

GNU 도구를 사용하여,

date -r file +'%F %T %z'

이는 주어진 파일의 마지막으로 수정된 타임스탬프를 가져오고(1초 미만의 해상도 없이) 이를 사용하여 date생성된 파일과 동일한 형식으로 다시 포맷합니다 stat -c %y file.

예:

$ stat -c '%y' file
2021-03-17 08:53:39.540802643 +0100
$ date -r file +'%F %T %z'
2021-03-17 08:53:39 +0100

형식 지정 printf에 -like 형식을 직접 사용할 수 있지만 중간 문자열을 수정할 수는 없습니다.%y

$ stat -c '%.19y' file
2021-03-17 08:53:39

이렇게 하면 19자 이후의 문자열이 잘려 1초 미만의 데이터가 제거되지만 시간대 정보도 무시됩니다.

답변2

좋아, printf와 같은 길이/매트를 지정할 수 있으므로( %010s예를 들어), 실제로 필드의 최대 길이를 문서화하는 원래 printf 문서로 돌아가겠습니다. 그리고 짠... stat형식에 따라 작동합니다! (날짜 부분만 유지하기 위해 여기서는 잘렸습니다):

stat -c '%.10y %10s %n' /boot/*
2018-05-17    1501659 /boot/abi-4.13.0-43-generic
2018-05-30    1501528 /boot/abi-4.13.0-45-generic
2018-05-17     213220 /boot/config-4.13.0-43-generic
2018-05-30     213220 /boot/config-4.13.0-45-generic
1970-01-01       4096 /boot/efi
2018-06-15       1024 /boot/grub
2018-05-22   52211016 /boot/initrd.img-4.13.0-43-generic
2018-06-22   52210415 /boot/initrd.img-4.13.0-45-generic
2017-04-08      12288 /boot/lost+found
2016-01-28     182704 /boot/memtest86+.bin
2016-01-28     184380 /boot/memtest86+.elf
2016-01-28     184840 /boot/memtest86+_multiboot.bin
2018-05-17        255 /boot/retpoline-4.13.0-43-generic
2018-05-30        255 /boot/retpoline-4.13.0-45-generic
2018-05-17    3884045 /boot/System.map-4.13.0-43-generic
2018-05-30    3883942 /boot/System.map-4.13.0-45-generic
2018-05-17    7713296 /boot/vmlinuz-4.13.0-43-generic
2018-05-22    7715224 /boot/vmlinuz-4.13.0-43-generic.efi.signed
2018-05-30    7712560 /boot/vmlinuz-4.13.0-45-generic
2018-06-14    7714488 /boot/vmlinuz-4.13.0-45-generic.efi.signed

답변3

이는 통계적 타임스탬프에서 부품을 추출한 다음 사용자 정의 날짜 출력을 생성하는 편리한 방법입니다.

#!/bin/bash

given_file=$1   #  supply file to get backed up with timestamp

answer_interstital=$(stat -c '%.16y'  $given_file)  #   2021-08-23 15:09   stat given file extract out last changed timestamp using the -c flag
                                                    #   the   '%.16y'  specifies we want 16 character wide format of timestamp

first_portion=$( echo $answer_interstital  | cut -c6-7,9-10)     #  0823
second_portion=$(echo $answer_interstital  | cut -c12-13,15-16)  #  1509

cool_lastchanged_timestamp="${first_portion}_${second_portion}"  #  print both of above portions separated with an underbar

backup_filename=${given_file}~~${cool_lastchanged_timestamp}   #  create backup file using formatted timestamp

cp -p $given_file  $backup_filename # backup file from    some_file   to    some_file~~0823_1509   

답변4

@nohillside가 제공한 원래 질문에 대한 의견을 확장하면 날짜 정보의 형식을 수정하는 가장 유연한 방법은 stat해당 시대 이후의 시간을 사용하고 명령을 사용하여 형식을 지정하는 것입니다 date.

예를 들어:

date --date="@"`stat -c '%Y' file` "+%F %r"

결과:

2023-02-17 01:17:24 PM

매개변수 에서는 "+FORMAT"date 명령에 사용 가능한 모든 수정자를 사용할 수 있으며, 이는 를 사용하여 확인할 수 있습니다 man date.

관련 정보