관리 메뉴

태섭씨 블로그

리눅스 유용한 command 본문

IT/linux

리눅스 유용한 command

태섭씨 2015. 7. 19. 23:39

file : 파일 type 확인. 텍스트 파일의 경우 인코딩 방식 등도 확인이 가능

아래 예시는 

 - 아스키 텍스트 파일 (EOL이 CR , CRLF , LF로 각각 끝나는 것을 확인이 가능하다. cross platform(unix<->win 등)이 필요한 경우 file 명령어로 확인이 가능.

 - unicode text 파일

 - SGML document text 

 - directory

등의 예시를 보여주고 있다.

$ ls -l

total 80

-rw-r--r--   1 1001065  staff   91  7 19 22:27 mac_os9_file

drwxr-xr-x  14 1001065  staff  476  5 27 20:25 main

-rw-r--r--   1 1001065  staff  23067  7 15 17:17 pom.xml

drwxr-xr-x   4 1001065  staff  136  5 13 15:56 test

-rw-r--r--   1 1001065  staff   18  7 19 23:08 unicodefile

-rw-r--r--   1 1001065  staff   84  7 19 22:26 unixfile

-rw-r--r--   1 1001065  staff   91  7 19 22:24 winfile

$ file *

mac_os9_file: ASCII text, with CR line terminators

main:         directory

pom.xml:      exported SGML document text

test:         directory

unicodefile:  UTF-8 Unicode text

unixfile:     ASCII text

winfile:      ASCII text, with CRLF line terminators

$ 


아래 예시는 war(zip) file, xml text, java class 파일을 보여준다.

$ ls -l idpWebApp.war classes/ehcache.xml classes/log4j.dtd classes/UserProfile.class

-rw-r--r--  1 1001065  staff      8727  7 16 15:57 classes/UserProfile.class

-rw-r--r--  1 1001065  staff       734  7 16 15:57 classes/ehcache.xml

-rw-r--r--  1 1001065  staff      4984  7 16 15:57 classes/log4j.dtd

-rw-r--r--  1 1001065  staff  33487091  6 15 14:45 idpWebApp.war

$ file idpWebApp.war classes/ehcache.xml classes/log4j.dtd classes/UserProfile.class

idpWebApp.war:             Zip archive data, at least v1.0 to extract

classes/ehcache.xml:       XML  document text

classes/log4j.dtd:         XML  document text

classes/UserProfile.class: compiled Java class data, version 50.0 (Java 1.6)

$



tr : translate characters. stdin을 받아서 글자치환/삭제 등을 수행 후  stdout 으로 출력. 

정규식등도 적용이 되기 때문에, 유용하게 사용가능. (사실 awk 나 sed 로도 가능하다)

내 경우 보통 소문자<-> 대문자 치환 등에 사용한다.  (man page 예시에도 있다)

아래 첫 번째 예시는 소문자->대문자 치환, 두 번째 예시는 alphabetical char만 출력하는 예시. (-c 는 complement , -d 는 삭제. 즉 -cd 면 string1 에 해당하는 것만 남긴다는 의미)

$ echo "hello world" | tr '[:lower:]' '[:upper:]'

HELLO WORLD

$ cat tr_test_file

hello world !

this is number 1 2 3

$ tr -cd '[:alpha:]' < tr_test_file

helloworldthisisnumber$ 


odoctal, decimal, hex, ASCII dump. 

파일을 hexdump 뜰 때 유용할 듯 하다 (실은 처음 알았다 이런 명령어..).

아스키 파일을 hexdump 뜬 예시가 아래 있다. 

-txC : -t 는 output format을 지정하는 옵션인데 이 중 x 는 hex 를 의미하고 C 는 char 단위를 의미. hex 외에 oct, signed decimal, unsigned decimal 등이 가능하고 , C 대신에 S (short) , I (int) , L (long) 등이 가능하다. 

-tc : 디폴트 캐릭터 셋. new line 은 \n 으로 출력되며 그 외 특수 문자는 man page 참고.

-ta : 아래 예시에는 없다. ASCII 로 출력되며 control char 는 특정 이름으로 출력된다. 예를 들어 new line 은 "SP" , null 은 "NUL" 등. man page 참고 

$ cat unixfile

Hello world

Hello world

Hello world

Hello world

Hello world

Hello world

Hello world

$ od -txC -tc unixfile

0000000    48  65  6c  6c  6f  20  77  6f  72  6c  64  0a  48  65  6c  6c

           H   e   l   l   o       w   o   r   l   d  \n   H   e   l   l

0000020    6f  20  77  6f  72  6c  64  0a  48  65  6c  6c  6f  20  77  6f

           o       w   o   r   l   d  \n   H   e   l   l   o       w   o

0000040    72  6c  64  0a  48  65  6c  6c  6f  20  77  6f  72  6c  64  0a

           r   l   d  \n   H   e   l   l   o       w   o   r   l   d  \n

0000060    48  65  6c  6c  6f  20  77  6f  72  6c  64  0a  48  65  6c  6c

           H   e   l   l   o       w   o   r   l   d  \n   H   e   l   l

0000100    6f  20  77  6f  72  6c  64  0a  48  65  6c  6c  6f  20  77  6f

           o       w   o   r   l   d  \n   H   e   l   l   o       w   o

0000120    72  6c  64  0a

           r   l   d  \n

0000124


xxdmake a hexdump or do the reverse.

이것도 hexdump 를 생성해준다. (given file or stdin) 또한 반대도 가능. 

내 경우는 주로 vi 에서 hex dump를 보고 싶을 때 사용한다. 단 이 경우 hexdump 한 내용 자체가 text가 되어 버리기 때문에, 저장하면 안된다. 

vi 에서 hexdump한 내용을 볼 때 :%!xxd 를 입력하면 된다. 

원래대로 돌리려면 :%!xxd -r을 입력하면 된다.


<hexdump 전>

Hello world

Hello world

Hello world

Hello world

Hello world

Hello world

Hello world

<vi 에서 hexdump 후>

0000000: 4865 6c6c 6f20 776f 726c 640d 0a48 656c  Hello world..Hel

0000010: 6c6f 2077 6f72 6c64 0d0a 4865 6c6c 6f20  lo world..Hello

0000020: 776f 726c 640d 0a48 656c 6c6f 2077 6f72  world..Hello wor

0000030: 6c64 0d0a 4865 6c6c 6f20 776f 726c 640d  ld..Hello world.

0000040: 0a48 656c 6c6f 2077 6f72 6c64 0d0a 4865  .Hello world..He

0000050: 6c6c 6f20 776f 726c 640d 0a              llo world.. 


 





'IT > linux' 카테고리의 다른 글

리눅스 #! (hash-bang) , env 명령어  (0) 2015.02.11
Comments