file 명령어 (AIX 5.3·6.1)
https://archive.horanglab.com/posts/aix-cmd-file
핵심 정리
- 파일마다 여러 검사를 해서 종류(디렉터리, 텍스트, 실행 파일, 보관 파일 등)를 알아내 표준 출력에 씁니다.
- 매직 번호는 C 로캘이면
/etc/magic, 다른 로캘이면/usr/lib/nls/msg/로캘/magic.cat으로 판별합니다. - 텍스트로 보이는 파일은 앞의 1,024바이트를 봅니다. 크기가 0인 일반 파일은
empty로 표시합니다. - 심볼릭 링크는 기본으로 따라가고,
-h를 주면 링크로 표시합니다.-f 목록파일로 파일 목록을 읽습니다. -m은 다른 매직 파일을,-M은 기본 검사 없이 그 파일의 검사만 씁니다.-c는 매직 파일의 형식 오류를 검사합니다.- 읽을 수 없는 파일이나 종류를 알 수 없는 파일은 종료 상태에 영향을 주지 않습니다(
cannot open,data로 표시).
| 항목 | 내용 |
|---|---|
| 버전 | AIX 5.3, AIX 6.1 (두 버전 내용 같음) |
| 원문 | IBM Commands Reference — 5.3 Volume 2, 6.1 Volume 2 |
| 파일 | /usr/bin/file, /etc/magic |
| 관련 명령어 | find, ld |
목적
파일의 종류를 알아냅니다.
영어 원문
Determines the file type.
구문
파일 종류를 알아낼 때:
file [ -m MagicFile ] [ -d ] [ -h ] [ -i ] [ -M MagicFile ] [ -f FileList ] [ File ... ]
매직 파일의 형식 오류를 검사할 때:
file -c [ -m MagicFile ]
설명
file 명령은 File 매개변수나 FileList 변수로 지정한 파일을 읽어, 파일마다 여러 검사를 하고 종류를 분류해 표준 출력에 씁니다. 파일은 일반 파일, 디렉터리, FIFO(이름 있는 파이프), 블록 특수 파일, 문자 특수 파일, 심볼릭 링크, 소켓 중 하나입니다.
- 크기가 0인 일반 파일은 빈 파일(empty)로 알아냅니다.
- 심볼릭 링크는 기본으로 링크가 가리키는 파일을 따라갑니다.
파일이 ASCII 형식으로 보이면 앞의 1,024바이트를 살펴 종류를 정합니다. ASCII 형식으로 보이지 않으면 바이너리 데이터 파일인지, 확장 문자가 든 텍스트 파일인지 더 가려 봅니다.
File이 실행 파일이나 오브젝트 모듈이고 버전 번호가 0보다 크면 버전 표시를 함께 보여 줍니다. a.out 파일은 ld 명령을 참고합니다.
언어 환경이 C 프로그래밍 언어면 /etc/magic 파일을 써서 매직 번호(종류를 나타내는 숫자나 문자열 상수)가 있는 파일을 알아냅니다. 언어 환경이 C가 아니면 /usr/lib/nls/msg/언어환경/magic.cat 파일을 씁니다.
파일이 없거나, 읽을 수 없거나, 파일 상태를 알 수 없어도 종료 상태에 영향을 주는 오류로 보지 않습니다. 출력에는 처리했지만 종류를 알 수 없었다고 나타냅니다.
-i 플래그를 쓰면 피연산자마다 다음 형식으로 표시합니다.
"%s: %s\n", file, type
type 값은 정해져 있지 않지만, POSIX 로캘에서 파일이 다음 표의 종류로 판별되면 type에 해당 문자열이 들어갑니다(그 밖의 문자열이 더 붙을 수 있음). 문자열 안의 공백은 정확히 빈칸 하나입니다.
| 파일 종류 | type에 들어가는 문자열 |
|---|---|
| 디렉터리 | directory |
| FIFO | fifo |
| 소켓 | socket |
| 블록 특수 파일 | block special |
| 문자 특수 파일 | character special |
| 실행 바이너리 | executable |
| 빈 일반 파일 | empty |
| 심볼릭 링크 | symbolic link to |
ar 보관 라이브러리 |
archive |
| 확장 cpio 형식 | cpio archive |
| 확장 tar 형식 | tar archive |
| 셸 스크립트 | commands text |
| C 언어 소스 | c program text |
| FORTRAN 소스 | fortran program text |
심볼릭 링크로 판별되면 다음 형식을 씁니다.
"%s: %s %s\n", file, type, contents of link
file 피연산자로 지정한 파일이 없거나 읽을 수 없으면 type에 cannot open이, 종류를 알 수 없으면 data가 들어갑니다. 둘 다 종료 상태에 영향을 주는 오류로 보지 않습니다.
영어 원문
The file command reads the files specified by the File parameter or the FileList variable, performs a series of tests on each file, and attempts to classify them by type. The command then writes the file types to standard output. The file can be regular file, directory, FIFO(named pipe), block special, character special, symbolic link or sockets type. • If it is a regular file and of zero length, it is identified as an empty file.
- If the file is a symbolic link, by default, the link is followed by file the symbolic link refers to.
If a file appears to be in ASCII format, the file command examines the first 1024 bytes and determines the file type. If a file does not appear to be in ASCII format, the file command further attempts to distinguish a binary data file from a text file that contains extended characters.
If the File parameter specifies an executable or object module file and the version number is greater than 0, the file command displays the version stamp. The ld command explains the use of a.out files.
If the language environment is the C programming language, the file command uses the /etc/magic file to identify files that have some sort of a magic number; that is, any file containing a numeric or string constant that indicates type.
However, if the language environment is some language other than the C programming language, the file command uses the /usr/lib/nls/msg/<language_env.>/magic.cat file to identify files with a magic number.
If the file does not exist, cannot be read or its file status could not be determined then, it is not considered as an error that affects the exit status. The output indicates that the file was processed but the type could not be determined.
When the -i flag is used, the following format shall be used to identify each operand, file specified: "%s: %s\n", file, type
The values for type are unspecified except that in the POSIX locale, if file is identified as one of the types listed in the following table, type shall contain (but is not limited to) the corresponding string. Each space shown in the strings shall be exactly one space.
Table 5. File Utility Output Strings If file is a: type shall contain the string: Directoy directory FIFO fifo Socket socket Block special block special Character special character special Executable binary executable Empty regular file empty Symbolic link symbolic link to ar archive library archive Extended cpio format cpio archive Extended tar format tar archive Shell script commands text C-language source c program text
Table 5. File Utility Output Strings (continued) If file is a: type shall contain the string: FORTRAN source fortran program text
If file is identified as a symbolic link, the following alternative output format shall be used: "%s: %s %s\n", file, type, contents of link"
If the file named by the file operand does not exist or cannot be read, the string cannot open shall be included as part of the type field, but this shall not be considered an error that affects the exit status. If the type of the file named by the file operand cannot be determined, the string data shall be included as part of the type field, but this shall not be considered an error that affects the exit status.
플래그
| 플래그 | 설명 |
|---|---|
-c |
지정한 매직 파일(기본은 /etc/magic)의 형식 오류를 검사합니다. 보통은 하지 않는 검사이며, 이 플래그에서는 파일 종류를 알아내지 않습니다. |
-d |
시스템 기본 검사를 파일에 적용합니다. |
-f FileList |
지정한 파일 목록을 읽습니다. 목록은 한 줄에 파일 하나이며, 앞뒤에 공백이 없어야 합니다. |
-h |
심볼릭 링크를 만나면 심볼릭 링크로 표시합니다. -h가 없어도, 없는 파일을 가리키는 심볼릭 링크는 -h를 준 것처럼 심볼릭 링크로 표시합니다. |
-i |
일반 파일이면 종류를 더 가리지 않고, 위 설명의 형식으로 표시합니다. |
-m MagicFile |
매직 파일의 이름을 지정합니다(기본은 /etc/magic). |
-M MagicFile |
파일 종류를 알아낼 검사가 든 파일을 지정합니다. 시스템 기본 검사는 하지 않습니다. |
영어 원문
-c— Checks the specified magic file (the /etc/magic file, by default) for format errors. This validation is not normally done. File typing is not done under this flag.-d— Applies any default system tests to the file.-fFileList — Reads the specified file list. The file must list one file per line and must not contain leading or trailing spaces.-h— When a symbolic link is encountered, identifies the file as a symbolic link. If the-hflag is not specified and file is a symbolic link that refers to a nonexistent file, file shall identify the file as a symbolic link, as if the-hflag had been specified.-i— If a file is a regular file, does not attempt to classify the type of the file further, but identifies the file as specified in “Description” on page 517.-mMagicFile — Specifies the file name of the magic file (the /etc/magic file, by default).-MMagicFile — Specifies the name of a file containing tests that shall be applied to a file in order to classify it. No default system tests shall be applied.
종료 상태
| 값 | 뜻 |
|---|---|
| 0 | 잘 끝났습니다. |
| 0보다 큼 | 오류가 났습니다. |
영어 원문
This command returns the following exit values:
- 0 — Successful completion.
>0— An error occurred.
예제
- 파일에 든 정보의 종류를 표시합니다.
myfile의 종류(디렉터리, 데이터, ASCII 텍스트, C 프로그램 소스, 보관 파일 등)가 나옵니다.
file myfile
- 목록에 적힌 파일마다 종류를 표시합니다.
filenames목록의 파일마다 종류가 나옵니다. 파일 이름은 한 줄에 하나씩 적어야 합니다.
file -f filenames
참고: file 명령의 메시지를 바꾸고 싶으면 -m 옵션으로 별도의 매직 파일을 씁니다. 읽기 전용인 /etc/magic 파일은 고치지 않는 것이 좋습니다.
영어 원문
- To display the type of information a file contains, enter:
file myfile
This displays the file type of myfile (such as directory, data, ASCII text, C-program source, and archive).
- To display the type of each file named in a list of file names, enter:
file -f filenames
This displays the type of each file named in the filenames list. Each file name must appear alone on a line.
Note: To get customized messages from the file command, use a separate magic file with the -m option. It is not advisable to edit the read-only /etc/magic file.
파일
| 파일 | 설명 |
|---|---|
/usr/bin/file |
file 명령이 들어 있습니다. |
/etc/magic |
파일 종류 데이터베이스가 들어 있습니다. |
관련 명령어: find, ld
영어 원문
- /usr/bin/file — Contains the file command.
- /etc/magic — Contains the file type database.
Related reference:
- find Command
Related information:
- ld command
- Files command
- File and directory access modes
리눅스(GNU)와 다른 점
| 항목 | AIX 5.3·6.1 | GNU/리눅스 file |
|---|---|---|
| 매직 파일 | /etc/magic(로캘별 magic.cat) |
/usr/share/misc/magic.mgc 등 |
-i |
일반 파일을 더 가리지 않음 | MIME 형식으로 출력 |
-h |
심볼릭 링크로 표시 | 같음(-L이 따라가기) |
리눅스의 file -i(MIME 형식)와 AIX의 -i는 뜻이 완전히 다릅니다. AIX 5.3·6.1 문서에는 -b(파일 이름 없이), -z(압축 파일 안 보기), --mime-type 같은 플래그가 없습니다.