본문 바로가기

OS/LINUX

[Linux] 텍스트 파일 활용 명령어 (vi, tail, head, cat) 부제) 로그 조회 및 관리하기

[Linux] 텍스트 파일 활용 명령어 (vi, tail, head, cat)

부제) 로그 조회 및 관리하기 






GUI 형태가 아닌 텍스트, 쉘로 구성된 서버를 사용할 때에는 명령어를 모르면 불편한 점이 많습니다. 텍스트 파일을 열어본다던지,

특정부분만 보고싶다던지등의 다양한 상황이 발생할텐데요. 

이번에 작성된 포스팅은 개발한 프로그램의 로그를 확인 및 텍스트 파일 수정 등의 다양한  상황에서 알아두면 유용한 명령어들을 정리해보았습니다.







1. vim 명령어 ( == vi , 텍스트 편집기)

vim 은 vi 의 상위 호환되는 텍스트 편집기입니다. 모든 종류의 일반 텍스트를 편집하는데 사용할 수 있으며, 프로그램 편집에 유용한 명령어 입니다.

vi 명령어에 보다 다양한 개선사항이 존재합니다. 더 자세한 사항을 알고 싶으시면 #man vim 으로 사용법을 터득하는 것을 추천드립니다.

기본 사용법은 vi 나 vim 이나 동일하기 때문에, 어떤 명령어를 사용하실지는 선택사항인 것 같습니다.

 

vi [파일명] 


1) 파일이 존재할 경우,

파일을 조회, 수정할 수 있는 텍스트 편집기가 실행됩니다.


2) 존재하지 않을 경우,

새로운 텍스트 파일을 생성할 수 있는 텍스트 편집기가 실행되며, 저장시 새로운 텍스트 파일이 생성되고, :q 등으로 저장이 이루어지지 않은 경우에는

취소되고, 새로운 텍스트파일이 생성되지 않습니다.




2. cat 명령어

파일을 연결하고, 해당 파일의 내용을 출력해주는 명령어 입니다. 즉, 전체 출력 명령어라고 보시면 될 것 같습니다.

cat [option] [file]

 -A, --show-all equivalent to -vET

 -b, --number-nonblank number nonempty output lines

 -e     equivalent to -vE

 -E, --show-ends display $ at end of each line

 -n, --number number all output lines

 -s, --squeeze-blank  suppress repeated empty output lines

 -t     equivalent to -vT

 -T, --show-tabs display TAB characters as ^I

 -u     (ignored)

 -v, --show-nonprinting

 use ^ and M- notation, except for LFD and TAB

 





3. tail 명령어

각 파일의 마지막 10행을 표준 출력하는 명령어 입니다. 실시간 업데이트 되는 로그를 확인할 때 유용하게 사용할 수 있는 명령어입니다.

tail [option] [file]

  -c, --bytes=K

              output  the  last K bytes; alternatively, use -c +K to output bytes starting with

              the Kth of each file

       -f, --follow[={name|descriptor}]

              output appended data as the file grows; -f, --follow, and --follow=descriptor are

              equivalent

       -F     same as --follow=name --retry

       -n, --lines=K

              output  the  last  K  lines, instead of the last 10; or use -n +K to output lines

              starting with the Kth

       --max-unchanged-stats=N

              with --follow=name, reopen a FILE which has not changed size after N (default  5)

              iterations  to  see if it has been unlinked or renamed (this is the usual case of

              rotated log files).  With inotify, this option is rarely useful.

       --pid=PID

              with -f, terminate after process ID, PID dies

       -q, --quiet, --silent

              never output headers giving file names

       --retry

              keep trying to open a file even when it is or becomes inaccessible;  useful  when

              following by name, i.e., with --follow=name

       -s, --sleep-interval=N

              with -f, sleep for approximately N seconds (default 1.0) between iterations.

              With inotify and --pid=P, check process P at least once every N seconds.

       -v, --verbose always output headers giving file names

       --help display this help and exit

       --version output version information and exit

 

위와 같이 tail -f 명령어를 이용하면, 새로이 업데이트 되는, 즉 파일의 마지막에 추가로 붙는 로그들을 읽어들일 수 있습니다.

그렇게되면, 새로이 업데이트 되는 로그들이 출력되면서, 실시간 로그들을 확인 할 수 있겠지요.




3. head 명령어

tail 과는 반대 개념의 명령어입니다. 파일의 앞부분을 출력하는 명령어입니다.

head [option] [file]

  -c, --bytes=[-]K

              print  the  first  K  bytes of each file; with the leading ‘-’, print all but the

              last K bytes of each file


       -n, --lines=[-]K

              print the first K lines instead of the first 10; with the leading ‘-’, print  all

              but the last K lines of each file


       -q, --quiet, --silent

              never print headers giving file names


       -v, --verbose

              always print headers giving file names


       --help display this help and exit


       --version

              output version information and exit

 



'OS > LINUX' 카테고리의 다른 글

[ LINUX ] CentOS LVM 파티션 축소 및 확장  (0) 2018.07.25
[ LINUX ] vsftpd 설치 ( FTP 서버 설치 )  (0) 2018.07.25
[Linux ] Centos JAVA 설치  (0) 2018.07.23
Linux _ 방화벽 포트 열기  (0) 2017.05.12
Linux _ tar.xz 압축풀기  (1) 2017.05.10