이 기사는 저희 Linux 트릭과 팁 시리즈의 일부입니다. 이 기사에서는 cat 명령어(리눅스에서 가장 자주 사용되는 명령어)와 tac(cat 명령어의 역순 – 파일을 역순으로 출력)의 기본적인 사용법과 실제 예제를 다룰 것입니다.
관련 기사: 리눅스에서 유용한 ‘cat’ 명령어 예제 13가지
리눅스에서 Cat 명령어의 기본적인 사용법
Cat 명령어는 Concatenate의 약자로 *nix 시스템에서 가장 많이 사용되는 명령어 중 하나입니다. 이 명령어의 가장 기본적인 사용법은 파일을 읽고 그 내용을 터미널에 표시하는 것입니다.
# cat file.txt

cat 명령어의 또 다른 사용법은 여러 파일을 읽거나 결합하여 아래 예제에서 설명하는 것처럼 모니터로 출력하는 것입니다.
# cat file1.txt file2.txt file3.txt

이 명령어는 또한 “>”
리눅스 리다이렉션 연산자를 사용하여 여러 파일을 하나의 파일로 연결(결합)하는 데에 사용될 수 있습니다.
# cat file1.txt file2.txt file3.txt > file-all.txt

추가 리다이렉터를 사용하여 새 파일의 내용을 file-all.txt
의 맨 아래에 추가할 수 있습니다.
# cat file4.txt >> file-all.txt

cat 명령어는 파일의 내용을 새 파일로 복사하는 데에 사용될 수 있습니다. 새 파일의 이름은 임의로 지정할 수 있습니다. 예를 들어, 현재 위치의 파일을 /tmp/
디렉토리로 복사합니다.
# cat file1.txt > /tmp/file1.txt

현재 위치에서 파일을 /tmp/
디렉토리로 복사하고 이름을 변경하십시오.
# cat file1.txt > /tmp/newfile.cfg

A less usage of the cat command is to create a new file with the below syntax. When finished editing the file hit CTRL+D
to save and exit the new file.
# cat > new_file.txt

파일의 모든 출력 라인에 번호를 매기기 위해서는 -n
스위치를 사용하십시오.
# cat -n file-all.txt

비어 있지 않은 각 라인의 번호만 표시하려면 -b
스위치를 사용하십시오.
# cat -b file-all.txt

Linux cat 명령어에 대해 더 알고 싶다면, Linux에서 유용한 ‘cat’ 명령어 예제 13가지를 읽어보세요.
Linux에서 Tac 명령어 사용 방법 배우기
반면에, *nix 시스템에서 잘 알려지지 않고 덜 사용되는 명령어인 tac
명령어가 있습니다. Tac은 사실상 cat
명령어의 역순 버전이며 각 파일 라인을 아래에서 위로 출력하는 명령어로 작동합니다.
# tac file-all.txt

명령어의 가장 중요한 옵션 중 하나는 파일 내용을 문자열이나 키워드를 기준으로 분리하는 -s
스위치입니다.
# tac file-all.txt --separator "two"

tac 명령어의 가장 중요한 사용법은, 로그 파일을 디버깅하는 데 큰 도움을 줄 수 있으며 로그 내용의 시간순서를 역으로 뒤집는 데 도움이 됩니다.
$ tac /var/log/auth.log Or to display the last lines $ tail /var/log/auth.log | tac
샘플 출력
tecmint@tecmint ~ $ tac /var/log/auth.log pr 6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session closed for user root Apr 6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session closed for user root ....
tecmint@tecmint ~ $ tail /var/log/auth.log | tac Apr 6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session closed for user root Apr 6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session closed for user root Apr 6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 15:55:02 tecmint CRON[17194]: pam_unix(cron:session): session closed for user root Apr 6 15:55:01 tecmint CRON[17195]: pam_unix(cron:session): session closed for user root ...
동일한 cat
명령어와 마찬가지로 tac
은 텍스트 파일 조작에 뛰어난 역할을 하지만, 특히 이진 파일이나 첫 줄이 해당 프로그램을 실행시킬 것을 나타내는 파일에서는 피해야 합니다.
Source:
https://www.tecmint.com/learn-linux-cat-command-and-tac-command/