Linux

[Linux] stat - 파일/디렉터리 상태 확인

비번변경 2023. 5. 10. 15:55

stat

파일이나 디렉터리의 상태 정보를 확인할 수 있는 명령어다. 디렉터리 내 파일에 대한 요약 정보를 위주로 출력하는 ls 명령어보다 상세한 정보를 확인할 수 있다.

 

 

사용 방법

명령어 뒤에 정보를 확인할 파일을 지정한다. 확인할 파일은 여러 개를 공백으로 나열하거나 *으로 지정할 수도 있다.

옵션 없이 사용하면 파일명, 크기, 접근 권한 등의 정보를 확인할 수 있다.

stat <FILE>

 

출력 정보

  • Inode : inode 번호
  • Links : 링크된 파일 수
  • Uid, Gid : 소유자 및 그룹의 이름과 id
  • Access : 출력이 8진수 숫자/또는 문자열 형식인 경우 권한 정보
  • Access : 출력이 시각 형식인 경우 접근 시각(atime) 정보
  • Modify : 수정 시각(mtime) 정보
  • Change : 변경 시각(ctime) 정보 

 

 

요약 출력

-t, --terse 옵션을 사용하면 요약된 정보를 확인한다.

stat -t <FILE>

 

 

파일 시스템 정보 출력

-f 옵션을 사용하면 파일 시스템 상태 정보를 확인한다.

stat -f <FILE>

 

 

원하는 정보 출력

-c, --printf 옵션을 사용하면 원하는 정보만 출력할 수 있다.

-c 옵션을 사용할 때는 출력 마지막에 개행 문자가 들어간다. --printf 옵션을 사용할 때는 개행 문자가 들어가지 않을 수 있어, 형식 지정 시 \\n을 포함시켜주어야 할 수 있다.

 

1. 8진수로 권한 표시

권한 정보를 8진수로 표시할 떄는 %a 형식을, 문자열로 표시할 때는 %A 형식을 사용한다.

stat -c %a <FILE>

 

2. atime, mtime, ctime 정보 표시

파일에 대한 접근 시각 또는 수정 시각 등을 표시한다.

stat --printf "atime=%x \\nmtile=%y \\nctime=%z \\n" <FILE>

형식을 대문자로 지정하면 timestamp로 표시된다.

 

+ 형식 지정자

그 외 다른 형식 지정자는 다음과 같다. --f 옵션과 함께 사용하는 파일 시스템에 대한 형식 지정은 제외했다.

  %a   access rights in octal (note '#' and '0' printf flags)
  %A   access rights in human readable form
  %b   number of blocks allocated (see %B)
  %B   the size in bytes of each block reported by %b
  %C   SELinux security context string
  %d   device number in decimal
  %D   device number in hex
  %f   raw mode in hex
  %F   file type
  %g   group ID of owner
  %G   group name of owner
  %h   number of hard links
  %i   inode number
  %m   mount point
  %n   file name
  %N   quoted file name with dereference if symbolic link
  %o   optimal I/O transfer size hint
  %s   total size, in bytes
  %t   major device type in hex, for character/block device special files
  %T   minor device type in hex, for character/block device special files
  %u   user ID of owner
  %U   user name of owner
  %w   time of file birth, human-readable; - if unknown
  %W   time of file birth, seconds since Epoch; 0 if unknown
  %x   time of last access, human-readable
  %X   time of last access, seconds since Epoch
  %y   time of last data modification, human-readable
  %Y   time of last data modification, seconds since Epoch
  %z   time of last status change, human-readable
  %Z   time of last status change, seconds since Epoch

 

 

참고 문서

https://unix.stackexchange.com/questions/523515/how-to-test-file-permissions-using-shell-script

https://www.lesstif.com/lpt/linux-stat-105644077.html

https://hbase.tistory.com/296

 

728x90