cat 명령어

cat 명령어는 리눅스 시스템에서 파일의 내용을 출력하거나, 여러 파일을 연결하여 하나의 파일로 만드는 유틸리티입니다. “concatenate”의 줄임말로, 파일 연결 및 내용 출력에 사용됩니다.

기본 사용법

파일 내용 출력

가장 기본적인 사용법으로, 파일의 내용을 터미널에 출력합니다.

hojin@DESKTOP-11LMH3B:~$ cat example.txt
Hello, World!
This is a sample text file.
Welcome to Linux.

여러 파일 출력

여러 파일의 내용을 연속으로 출력할 수 있습니다.

hojin@DESKTOP-11LMH3B:~$ cat file1.txt file2.txt file3.txt
Content of file1
Content of file2
Content of file3

주요 옵션

-n 옵션

행 번호를 추가하여 출력합니다.

hojin@DESKTOP-11LMH3B:~$ cat -n example.txt
     1	Hello, World!
     2	This is a sample text file.
     3	Welcome to Linux.

-b 옵션

빈 줄을 제외하고 행 번호를 추가합니다.

hojin@DESKTOP-11LMH3B:~$ cat -b example.txt
     1	Hello, World!

     2	This is a sample text file.
     3	Welcome to Linux.

-s 옵션

연속된 빈 줄을 하나로 압축합니다.

hojin@DESKTOP-11LMH3B:~$ cat -s example.txt
Hello, World!

This is a sample text file.
Welcome to Linux.

-A 옵션

모든 문자를 표시합니다 (탭, 개행, 제어 문자 등).

hojin@DESKTOP-11LMH3B:~$ cat -A example.txt
Hello, World!$
This is a sample text file.$
Welcome to Linux.$

-T 옵션

탭 문자를 ^I로 표시합니다.

hojin@DESKTOP-11LMH3B:~$ cat -T example.txt
Hello,^IWorld!
This is a^Isample text file.
Welcome to^ILinux.

-E 옵션

행 끝에 $ 기호를 추가합니다.

hojin@DESKTOP-11LMH3B:~$ cat -E example.txt
Hello, World!$
This is a sample text file.$
Welcome to Linux.$

파일 생성

새로운 파일 생성

cat 명령어를 사용하여 새로운 파일을 생성할 수 있습니다.

hojin@DESKTOP-11LMH3B:~$ cat > newfile.txt
This is the first line.
This is the second line.
This is the third line.
Ctrl+D

파일에 내용 추가

기존 파일에 내용을 추가할 수 있습니다.

hojin@DESKTOP-11LMH3B:~$ cat >> existing.txt
This line will be appended to the existing file.
Ctrl+D

파일 연결

여러 파일을 하나로 연결

여러 파일의 내용을 하나의 파일로 연결할 수 있습니다.

# 파일들을 연결하여 새 파일 생성
hojin@DESKTOP-11LMH3B:~$ cat file1.txt file2.txt file3.txt > combined.txt

# 연결된 파일 확인
hojin@DESKTOP-11LMH3B:~$ cat combined.txt
Content from file1
Content from file2
Content from file3

파일에 다른 파일 추가

hojin@DESKTOP-11LMH3B:~$ cat file2.txt >> file1.txt

실용적인 예제

파일 내용 확인

# 파일의 처음 몇 줄만 확인 (head 명령어 대신)
hojin@DESKTOP-11LMH3B:~$ cat example.txt | head -5

# 파일의 마지막 몇 줄만 확인 (tail 명령어 대신)
hojin@DESKTOP-11LMH3B:~$ cat example.txt | tail -5

텍스트 검색과 조합

# 특정 패턴이 포함된 줄만 출력
hojin@DESKTOP-11LMH3B:~$ cat example.txt | grep "Linux"

# 파일 내용을 정렬
hojin@DESKTOP-11LMH3B:~$ cat example.txt | sort

# 파일 내용의 단어 수 계산
hojin@DESKTOP-11LMH3B:~$ cat example.txt | wc -w

파일 비교

# 두 파일의 내용을 비교
hojin@DESKTOP-11LMH3B:~$ diff <(cat file1.txt) <(cat file2.txt)

고급 사용법

히어독(Here Document) 사용

스크립트에서 cat을 사용하여 여러 줄의 텍스트를 생성할 수 있습니다.

#!/bin/bash
# 설정 파일 생성
cat > config.txt << EOF
[Database]
host=localhost
port=3306
user=root
password=mypassword

[Application]
name=myapp
version=1.0.0
EOF

조건부 파일 생성

#!/bin/bash
# 파일이 존재하지 않을 때만 생성
if [ ! -f "log.txt" ]; then
    cat > log.txt << EOF
=== Application Log ===
Created: $(date)
User: $(whoami)
EOF
fi

파일 내용 검증

#!/bin/bash
# 파일이 비어있는지 확인
if [ -s "data.txt" ]; then
    echo "파일이 비어있지 않습니다:"
    cat data.txt
else
    echo "파일이 비어있습니다."
fi

문제 해결

큰 파일 처리

매우 큰 파일을 cat으로 출력하면 터미널이 복잡해질 수 있습니다.

# 파일의 처음 10줄만 확인
hojin@DESKTOP-11LMH3B:~$ cat large_file.txt | head -10

# 파일의 마지막 10줄만 확인
hojin@DESKTOP-11LMH3B:~$ cat large_file.txt | tail -10

# 페이지 단위로 확인 (less 사용)
hojin@DESKTOP-11LMH3B:~$ cat large_file.txt | less

바이너리 파일 처리

바이너리 파일을 cat으로 출력하면 터미널이 깨질 수 있습니다.

# 바이너리 파일인지 확인
hojin@DESKTOP-11LMH3B:~$ file binary_file.bin

# 바이너리 파일은 hexdump 사용
hojin@DESKTOP-11LMH3B:~$ hexdump -C binary_file.bin | head -20

권한 문제

파일에 읽기 권한이 없는 경우 오류가 발생할 수 있습니다.

# 파일 권한 확인
hojin@DESKTOP-11LMH3B:~$ ls -l example.txt

# 권한 변경
hojin@DESKTOP-11LMH3B:~$ chmod +r example.txt

활용 팁

로그 파일 모니터링

#!/bin/bash
# 실시간 로그 모니터링
tail -f /var/log/syslog | while read line; do
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $line"
done

파일 백업

#!/bin/bash
# 파일 백업 스크립트
backup_dir="/backup/$(date +%Y%m%d)"
mkdir -p "$backup_dir"

# 여러 파일을 하나로 백업
cat file1.txt file2.txt file3.txt > "$backup_dir/combined_backup.txt"
echo "백업 완료: $backup_dir/combined_backup.txt"

설정 파일 관리

#!/bin/bash
# 설정 파일 템플릿 생성
cat > config_template.conf << 'EOF'
# Application Configuration
[General]
name=myapp
version=1.0.0

[Database]
host=${DB_HOST}
port=${DB_PORT}
user=${DB_USER}
password=${DB_PASSWORD}

[Logging]
level=INFO
file=/var/log/myapp.log
EOF

cat 명령어는 리눅스에서 가장 기본적이고 유용한 명령어 중 하나입니다. 파일 내용 확인, 파일 생성, 파일 연결 등 다양한 용도로 활용할 수 있습니다.

서브목차