Linux에서는 파일 내용을 보기 위한 여러 명령어와 프로그램이 제공됩니다. 파일 작업은 거의 모든 컴퓨터 사용자, 초보자, 정기 사용자, 고급 사용자, 개발자, 관리자 등이 수행하는 어려운 작업 중 하나입니다. 파일을 효과적으로 처리하는 것은 예술입니다.

오늘 이 글에서는 head, tail, cat라고 불리는 가장 인기 있는 명령어에 대해 논의할 것입니다. 대부분의 사람들은 이미 이러한 명령어를 인식하고 있지만, 필요할 때 실제로 사용하는 사람은 매우 적습니다.
1. head 명령어
head 명령어는 주어진 파일 이름의 처음 열 개의 줄을 읽습니다. head 명령어의 기본 구문은 다음과 같습니다:
head [options] [file(s)]
예를 들어, 다음 명령어는 ‘ /etc/passwd ‘라는 파일의 처음 열 개의 줄을 표시합니다.
# head /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh lp:x:7:7:lp:/var/spool/lpd:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh
만약 둘 이상의 파일이 주어지면, head는 각 파일의 처음 열 개의 줄을 개별적으로 표시할 것입니다. 예를 들어, 다음 명령어는 각 파일의 열 개의 줄을 표시합니다.
# head /etc/passwd /etc/shadow ==> /etc/passwd <== root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin ==> /etc/shadow <== root:$6$85e1:15740:0:99999:7::: bin:*:15513:0:99999:7::: daemon:*:15513:0:99999:7::: adm:*:15513:0:99999:7::: lp:*:15513:0:99999:7::: sync:*:15513:0:99999:7::: shutdown:*:15513:0:99999:7::: halt:*:15513:0:99999:7::: mail:*:15513:0:99999:7::: uucp:*:15513:0:99999:7:::
기본 열 개의 줄 이상의 라인을 검색하는 경우에는 ‘ -n ‘ 옵션을 사용하여 검색할 라인 수를 지정하는 정수와 함께 사용합니다. 예를 들어, 다음 명령어는 ‘ 5 ‘라인을 ‘ /var/log/yum.log ‘ 파일에서 표시합니다.
# head -n5 /var/log/yum.log Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686 Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686 Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686 Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch
실제로 ‘ -n ‘ 옵션을 사용할 필요는 없습니다. 빼기 기호를 사용하여 동일한 결과를 얻기 위해 공백 없이 정수를 지정하면 됩니다.
# head -5 /var/log/yum.log Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686 Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686 Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686 Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch
head 명령은 ‘‐c’ 옵션 다음에 표시할 바이트 수를 따라서 지정된 바이트 수만큼 표시할 수도 있습니다. 예를 들어, 다음 명령어는 주어진 파일의 첫 45바이트를 표시할 것입니다.
# head -c45 /var/log/yum.log Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el
2. tail 명령
tail 명령은 텍스트 파일의 마지막 열 개의 줄을 표시할 수 있게 해줍니다. head 명령과 마찬가지로 tail 명령은 ‘n’ 줄 수와 ‘n’ 문자 수를 지원합니다.
tail 명령의 기본 구문은 다음과 같습니다:
# tail [options] [filenames]
예를 들어, 다음 명령어는 ‘access.log’라는 파일의 마지막 열 개의 줄을 출력할 것입니다.
# tail access.log 1390288226.042 0 172.16.18.71 TCP_DENIED/407 1771 GET http://download.newnext.me/spark.bin? - NONE/- text/html 1390288226.198 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.210 1182 172.16.20.44 TCP_MISS/200 70872 GET http://mahavat.gov.in/Mahavat/index.jsp pg DIRECT/61.16.223.197 text/html 1390288226.284 70 172.16.20.44 TCP_MISS/304 269 GET http://mahavat.gov.in/Mahavat/i/i-19.gif pg DIRECT/61.16.223.197 - 1390288226.362 570 172.16.176.139 TCP_MISS/200 694 GET http://p4-gayr4vyqxh7oa-3ekrqzjikvrczq44-if-v6exp3-v4.metric.gstatic.com/v6exp3/redir.html pg 1390288226.402 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.437 145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html 1390288226.445 0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html 1390288226.605 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.808 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
만약 여러 파일을 제공하면, tail은 각 파일의 마지막 열 개의 줄을 출력할 것입니다.
# tail access.log error.log ==> access.log <== 1390288226.042 0 172.16.18.71 TCP_DENIED/407 1771 GET http://download.newnext.me/spark.bin? - NONE/- text/html 1390288226.198 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.210 1182 172.16.20.44 TCP_MISS/200 70872 GET http://mahavat.gov.in/Mahavat/index.jsp pg DIRECT/61.16.223.197 text/html 1390288226.284 70 172.16.20.44 TCP_MISS/304 269 GET http://mahavat.gov.in/Mahavat/i/i-19.gif pg DIRECT/61.16.223.197 - 1390288226.362 570 172.16.176.139 TCP_MISS/200 694 GET http://p4-gayr4vyqxh7oa-3ekrqzjikvrczq44-if-v6exp3-v4.metric.gstatic.com/v6exp3/redir.html pg 1390288226.402 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.437 145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html 1390288226.445 0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html 1390288226.605 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.808 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html ==> error_log <== [Sun Mar 30 03:16:03 2014] [notice] Digest: generating secret for digest authentication ... [Sun Mar 30 03:16:03 2014] [notice] Digest: done [Sun Mar 30 03:16:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.0-fips configured -- resuming normal operations
비슷하게, 다음과 같이 ‘‐n’ 옵션을 사용하여 마지막 몇 줄을 출력할 수도 있습니다.
# tail -5 access.log 1390288226.402 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.437 145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html 1390288226.445 0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html 1390288226.605 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.808 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
‘‐c’ 인수를 사용하여 문자 수를 출력할 수도 있습니다.
# tail -c5 access.log ymantec.com:443 - NONE/- text/html
3. cat 명령
‘cat’ 명령은 가장 많이 사용되는 범용 도구입니다. 이 도구는 표준 입력을 표준 출력으로 복사합니다. 텍스트 파일이 현재 화면에 맞지 않는 경우 스크롤을 지원합니다.
cat 명령어의 기본 구문은 다음과 같습니다:
# cat [options] [filenames] [-] [filenames]
cat의 가장 빈번한 사용은 파일의 내용을 읽는 것입니다. 파일을 읽기 위해 필요한 것은 cat 뒤에 공백과 파일 이름을 입력하는 것뿐입니다.
# cat /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh lp:x:7:7:lp:/var/spool/lpd:/bin/sh …
cat 명령은 파일들을 연결하는 데도 사용됩니다.
# echo 'Hi Tecmint-Team' > 1 # echo 'Keep connected' > 2 # echo 'Share your thought' > 3 # echo 'connect us [email protected]' > 4
# cat 1 2 3 4 > 5
# cat 5 Hi Tecmint-Team Keep connected Share your thought connect us [email protected]
이는 파일을 생성하는 데에도 사용될 수 있습니다. ‘cat’ 명령어를 실행한 후 출력 리디렉션 연산자와 생성할 파일 이름을 입력하여 이루어집니다.
# cat > tecmint.txt Tecmint is the only website fully dedicated to Linux.
우리는 ‘cat’ 명령에 대한 사용자 정의 종료자를 가질 수 있습니다. 여기에 구현되어 있습니다.
# cat > test.txt << end I am Avishek Here i am writing this post Hope your are enjoying end
# cat test.txt I am Avishek Here i am writing this post Hope your are enjoying
‘cat’ 명령어의 힘을 과소평가하지 마십시오. 파일을 복사하는 데 유용할 수 있습니다.
# cat avi.txt I am a Programmer by birth and Admin by profession
# cat avi.txt > avi1.txt
# cat avi1.txt I am a Programmer by birth and Admin by profession
이제 ‘cat’의 반대는 무엇인가요? 그래, 그것은 ‘tac‘입니다. ‘tac’는 리눅스 하에서의 명령어입니다. ‘tac’에 대해 어떤 것이라도 언급하는 대신 예제를 보여주는 것이 좋습니다.
모든 월의 이름이 한 단어씩 한 줄에 나타나도록 텍스트 파일을 생성하십시오.
# cat month January February March April May June July August September October November December
# tac month December November October September August July June May April March February January
‘cat’ 명령어 사용 사례에 대한 더 많은 예제는 13 cat 명령어 사용 사례
지금까지입니다. 또 다른 흥미로운 기사로 다시 찾아볼게요. 그때까지 기다려 주시고 Tecmint에 연결 유지하세요. 소중한 의견을 댓글 섹션에 남겨주시기 바랍니다.
Source:
https://www.tecmint.com/view-contents-of-file-in-linux/