반응형

안녕하세요 가야태자 @talkit 입니다. 

오늘 적으려고 하는 이야기는 리눅스 명령어 tail에 관한 이야기 입니다. 

tail은 사전적의미로 꼬리라는 의미가 있습니다. 

TAIL(1)                                                                        User Commands                                                                        TAIL(1)

NAME
       tail - output the last part of files

SYNOPSIS
       tail [OPTION]... [FILE]...

DESCRIPTION
       Print the last 10 lines of each FILE to standard output.  With more than one FILE, precede each with a header giving the file name.

       With no FILE, or when FILE is -, read standard input.

       Mandatory arguments to long options are mandatory for short options too.

       -c, --bytes=[+]NUM
              output the last NUM bytes; or use -c +NUM to output starting with byte NUM of each file

       -f, --follow[={name|descriptor}]
              output appended data as the file grows;

              an absent option argument means 'descriptor'

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

       -n, --lines=[+]NUM
              output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM

       --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 op‐
              tion 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 if it is inaccessible

       -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

       -z, --zero-terminated
              line delimiter is NUL, not newline

       --help display this help and exit

       --version
              output version information and exit

tail은 위의 매뉴얼에도 나와 있듯이 파일의 마지막 일부를 보여주는 명령어 입니다. 

tail [옵션들] 파일명

위와 같이 사용할 수 있습니다. 

많은 옵션이 있지만, 제가 주로 사용하는 옵션은 -n, -f 입니다. -s도 재미 있는 옵션이겠네요 ^^

먼저 -n은 기본적으로 10줄만 출력해주는 tail의 줄수를 늘릴수 있습니다. 

-f 는 계속 변환의 파일의 경우 모니터링 할 수 있습니다. 

그래서 -f 같은 경우는 로그를 추적할때 많이 쓰입니다. 

실제로 한번 해보도록 하겠습니다. 

tail -f temp.txt

지난 번에 만들어 둔 temp.txt의 마지막 10줄을 한번 보겠습니다. 

tail temp.txt
2022-12-09, 충청남도, 15
2022-12-09, 충청북도, 16
2022-12-10, 경기도, 10
2022-12-10, 강원도, 11
2022-12-10, 경상남도, 12
2022-12-10, 경상북도, 13
2022-12-10, 전라남도, 14
2022-12-10, 전라북도, 15
2022-12-10, 충청남도, 16
2022-12-10, 충청북도, 17

위와 같이 나옵니다. 

그리고, 최근 20줄을 봐 보도록 하겠습니다. 

tail -n 20 temp.txt

위와 같이 명령어를 내리면 됩니다. 

tail -n 20 temp.txt
2022-12-08, 전라남도, 12
2022-12-08, 전라북도, 13
2022-12-08, 충청남도, 14
2022-12-08, 충청북도, 15
2022-12-09, 경기도, 9
2022-12-09, 강원도, 10
2022-12-09, 경상남도, 11
2022-12-09, 경상북도, 12
2022-12-09, 전라남도, 13
2022-12-09, 전라북도, 14
2022-12-09, 충청남도, 15
2022-12-09, 충청북도, 16
2022-12-10, 경기도, 10
2022-12-10, 강원도, 11
2022-12-10, 경상남도, 12
2022-12-10, 경상북도, 13
2022-12-10, 전라남도, 14
2022-12-10, 전라북도, 15
2022-12-10, 충청남도, 16

마지막 20줄을 잘 보여 줍니다. 

그러면 이제 조금 다르게 실제로 로그를 한번 보겠습니다. 

제가 일부러 프로그램을 하나 설치해보고 설치하기 전과 설치후를 모니터링 하는 것을 보여 드리겠습니다. ^^

cd /var/log
tail -f dpkg.log

리눅스 로그 폴더로 이동해서 dpkg.log를 모니터링 합니다. 

tail -f dpkg.log
2022-12-25 14:16:27 configure libcgi-fast-perl:all 1:2.15-1 <none>
2022-12-25 14:16:27 status unpacked libcgi-fast-perl:all 1:2.15-1
2022-12-25 14:16:27 status half-configured libcgi-fast-perl:all 1:2.15-1
2022-12-25 14:16:27 status installed libcgi-fast-perl:all 1:2.15-1
2022-12-25 14:16:27 trigproc man-db:amd64 2.10.2-1 <none>
2022-12-25 14:16:27 status half-configured man-db:amd64 2.10.2-1
2022-12-25 14:16:28 status installed man-db:amd64 2.10.2-1
2022-12-25 14:16:28 trigproc libc-bin:amd64 2.35-0ubuntu3.1 <none>
2022-12-25 14:16:28 status half-configured libc-bin:amd64 2.35-0ubuntu3.1
2022-12-25 14:16:28 status installed libc-bin:amd64 2.35-0ubuntu3.1

현재 결과는 위와 같습니다. 

tail이 끝나면 프롬프트로 나오지만 tail은 해보시면 계속 대기 하고 있습니다. 

저는 mc라고 mid-night command를 설치해보겠습니다. 

sudo apt install mc
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  bzip2 libssh2-1 mailcap mc-data mime-support unzip
Suggested packages:
  bzip2-doc arj catdvi | texlive-binaries dbview djvulibre-bin epub-utils genisoimage gv imagemagick libaspell-dev links | w3m | lynx odt2txt poppler-utils python python-boto
  python-tz unar wimtools xpdf | pdf-viewer zip
The following NEW packages will be installed:
  bzip2 libssh2-1 mailcap mc mc-data mime-support unzip
0 upgraded, 7 newly installed, 0 to remove and 20 not upgraded.
Need to get 2321 kB of archives.
After this operation, 8819 kB of additional disk space will be used.
Do you want to continue? [Y/n]

실제로 저는 위 명령어를 내렸습니다. 

tail -f dpkg.log
2022-12-25 14:16:27 configure libcgi-fast-perl:all 1:2.15-1 <none>
2022-12-25 14:16:27 status unpacked libcgi-fast-perl:all 1:2.15-1
2022-12-25 14:16:27 status half-configured libcgi-fast-perl:all 1:2.15-1
2022-12-25 14:16:27 status installed libcgi-fast-perl:all 1:2.15-1
2022-12-25 14:16:27 trigproc man-db:amd64 2.10.2-1 <none>
2022-12-25 14:16:27 status half-configured man-db:amd64 2.10.2-1
2022-12-25 14:16:28 status installed man-db:amd64 2.10.2-1
2022-12-25 14:16:28 trigproc libc-bin:amd64 2.35-0ubuntu3.1 <none>
2022-12-25 14:16:28 status half-configured libc-bin:amd64 2.35-0ubuntu3.1
2022-12-25 14:16:28 status installed libc-bin:amd64 2.35-0ubuntu3.1
2022-12-26 14:18:15 startup archives unpack
2022-12-26 14:18:15 install bzip2:amd64 <none> 1.0.8-5build1
2022-12-26 14:18:15 status half-installed bzip2:amd64 1.0.8-5build1
2022-12-26 14:18:15 status triggers-pending man-db:amd64 2.10.2-1
2022-12-26 14:18:15 status unpacked bzip2:amd64 1.0.8-5build1
2022-12-26 14:18:15 install libssh2-1:amd64 <none> 1.10.0-3
2022-12-26 14:18:15 status triggers-pending libc-bin:amd64 2.35-0ubuntu3.1
2022-12-26 14:18:15 status half-installed libssh2-1:amd64 1.10.0-3
2022-12-26 14:18:15 status unpacked libssh2-1:amd64 1.10.0-3
2022-12-26 14:18:15 install mailcap:all <none> 3.70+nmu1ubuntu1
2022-12-26 14:18:15 status half-installed mailcap:all 3.70+nmu1ubuntu1
2022-12-26 14:18:16 status unpacked mailcap:all 3.70+nmu1ubuntu1
2022-12-26 14:18:16 install mc-data:all <none> 3:4.8.27-1
2022-12-26 14:18:16 status half-installed mc-data:all 3:4.8.27-1
2022-12-26 14:18:16 status unpacked mc-data:all 3:4.8.27-1
2022-12-26 14:18:16 install mc:amd64 <none> 3:4.8.27-1
2022-12-26 14:18:16 status half-installed mc:amd64 3:4.8.27-1
2022-12-26 14:18:16 status unpacked mc:amd64 3:4.8.27-1
2022-12-26 14:18:16 install mime-support:all <none> 3.66
2022-12-26 14:18:16 status half-installed mime-support:all 3.66
2022-12-26 14:18:16 status unpacked mime-support:all 3.66
2022-12-26 14:18:16 install unzip:amd64 <none> 6.0-26ubuntu3.1
2022-12-26 14:18:16 status half-installed unzip:amd64 6.0-26ubuntu3.1
2022-12-26 14:18:16 status unpacked unzip:amd64 6.0-26ubuntu3.1
2022-12-26 14:18:16 startup packages configure
2022-12-26 14:18:16 configure unzip:amd64 6.0-26ubuntu3.1 <none>
2022-12-26 14:18:16 status unpacked unzip:amd64 6.0-26ubuntu3.1
2022-12-26 14:18:16 status half-configured unzip:amd64 6.0-26ubuntu3.1
2022-12-26 14:18:16 status installed unzip:amd64 6.0-26ubuntu3.1
2022-12-26 14:18:16 configure bzip2:amd64 1.0.8-5build1 <none>
2022-12-26 14:18:16 status unpacked bzip2:amd64 1.0.8-5build1
2022-12-26 14:18:16 status half-configured bzip2:amd64 1.0.8-5build1
2022-12-26 14:18:16 status installed bzip2:amd64 1.0.8-5build1
2022-12-26 14:18:16 configure mc-data:all 3:4.8.27-1 <none>
2022-12-26 14:18:16 status unpacked mc-data:all 3:4.8.27-1
2022-12-26 14:18:16 status half-configured mc-data:all 3:4.8.27-1
2022-12-26 14:18:17 status installed mc-data:all 3:4.8.27-1
2022-12-26 14:18:17 configure libssh2-1:amd64 1.10.0-3 <none>
2022-12-26 14:18:17 status unpacked libssh2-1:amd64 1.10.0-3
2022-12-26 14:18:17 status half-configured libssh2-1:amd64 1.10.0-3
2022-12-26 14:18:17 status installed libssh2-1:amd64 1.10.0-3
2022-12-26 14:18:17 configure mailcap:all 3.70+nmu1ubuntu1 <none>
2022-12-26 14:18:17 status unpacked mailcap:all 3.70+nmu1ubuntu1
2022-12-26 14:18:17 status half-configured mailcap:all 3.70+nmu1ubuntu1
2022-12-26 14:18:17 status installed mailcap:all 3.70+nmu1ubuntu1
2022-12-26 14:18:17 configure mime-support:all 3.66 <none>
2022-12-26 14:18:17 status unpacked mime-support:all 3.66
2022-12-26 14:18:17 status half-configured mime-support:all 3.66
2022-12-26 14:18:17 status installed mime-support:all 3.66
2022-12-26 14:18:17 configure mc:amd64 3:4.8.27-1 <none>
2022-12-26 14:18:17 status unpacked mc:amd64 3:4.8.27-1
2022-12-26 14:18:17 status half-configured mc:amd64 3:4.8.27-1
2022-12-26 14:18:17 status installed mc:amd64 3:4.8.27-1
2022-12-26 14:18:17 trigproc man-db:amd64 2.10.2-1 <none>
2022-12-26 14:18:17 status half-configured man-db:amd64 2.10.2-1
2022-12-26 14:18:17 status installed man-db:amd64 2.10.2-1
2022-12-26 14:18:17 trigproc libc-bin:amd64 2.35-0ubuntu3.1 <none>
2022-12-26 14:18:17 status half-configured libc-bin:amd64 2.35-0ubuntu3.1
2022-12-26 14:18:17 status installed libc-bin:amd64 2.35-0ubuntu3.1

mc 하나 까는데 엄청나게 패키지가 설치되네요 ^^

Ctrl 키와 C를 누르시면 빠져 나옵니다. 

오늘은 여기 까지 하고 다음에는 이 아이의 반대인데 head라는 명령어를 한번 공부해 보겠습니다.

감사합니다. 

반응형

+ Recent posts