cp 명령어 옵션 및 실행 시 일괄 덮어쓰기 설정 | yes, \(역슬래쉬) 2가지 설정

cp 명령어는 Copy의 약자로 파일 및 디렉토리를 복사합니다.

기본 사용법은

cp -[옵션] <복사할 파일 또는 디렉토리> <목적지 경로 또는 파일 또는 디렉토리리 >

간단하게 복사할 대상과 새로운 이름 등을 설정하여 사용할 수 있습니다.

CP 명령어

cp 명령어를 실행할 때 같은 이름의 대상 파일이 있을 때는 사용자에게 묻지 않고 덮어쓰기가 되는데, 이를 방지하기 위해 alias “cp=’cp -i”로 설정되어 있습니다.

✅ 기본 사용 방법

🔲 파일 및 디렉토리

◻️ 파일 → 파일로 복사

cp file1 file2
# 파일 > 파일 복사 
cp file1 file2
# 파일 > 디렉토리 내 복사
cp file1 dir1/
# 2개 파일 > 디렉토리 내 복사
cp file1 file2 dir1/

◻️ 파일 → 디렉토리로 복사

cp file1 dir1/
ll dir1/
total 0
-rw-r--r-- 1 root root 0 Jun  8 14:11 file1

file1이 dir1/ 안에 복사

◻️ 여러 파일 → 디렉토리로 복사

cp file1 file2 dir1/
ll dir1/
total 0
-rw-r--r-- 1 root root 0 Jun  8 14:11 file1
-rw-r--r-- 1 root root 0 Jun  8 14:15 file2

◻️ 디렉토리 디렉토리

 testuser]# cp dir1/ dir2/
cp: -r not specified; omitting directory 'dir1/'

디렉토리 복사는 -r 또는 -R, 혹은 -a 옵션을 써야 합니다.

  • -r/-R : 디렉토리와 하위 내용 재귀 복사
  • -a : 속성 유지하며 재귀 복사 (내부에 -r 포함)

✅ cp 명령어 옵션

✅ 명령어 예시

🔲 -a 옵션: 원본파일 속성 유지 & 하위 디렉토리(&파일) 복사

원본 파일을 속성을 유지하면서 하위 디렉토리 복사를 할 경우에 [-a]옵션만 사용해도 기능적으로 충분하지만 [-r]이나 [-R]옵션을 같이 사용하는 경우는 여러 환경(스크립트, 자동화 툴 등)에서 -r을 명시적으로 요구하거나 -a만 지원하지 않는 경우가 있을 수 있기 때문입니다.

 testuser]# cp -a dir1 dir2
 testuser]# ll dir*
dir1:
total 0
-rw-r--r-- 1 root root 0 Jun  8 14:11 file1
-rw-r--r-- 1 root root 0 Jun  8 14:15 file2

dir2:
total 0
-rw-r--r-- 1 root root 0 Jun  8 14:11 file1
-rw-r--r-- 1 root root 0 Jun  8 14:15 file2
 testuser]# 

[-d : 심볼릭 링크 자체를 복사]+[-R : 디렉토리 재귀 복사]+[-preserve=all]

-preserve=all 이 포함하는 항목 목록

설명

mode

파일 권한 (rwx 등)

ownership

소유자(user)와 그룹(group)

timestamps

마지막 수정 시간 등의 타임스탬프

links

하드링크 개수 (같은 inode 공유 유지)

context

SELinux 보안 컨텍스트

xattr

확장 속성 (파일에 부가적으로 저장되는 메타데이터)

all

위의 모든 항목을 한 번에 포함하는 축약형

🔲 -u 옵션: 추가 및 변경된 파일만 복사

  • 대상 경로에 동일한 파일이 있고, 원본이 더 새로울 경우에만 복사
  • 백업이나 동기화 시 자주 사용
 testuser]# cp -u file1 file2
 testuser]# ll file*
-rw-r--r-- 1 root root 0 Jun  8 14:11 file1
-rw-r--r-- 1 root root 0 Jun  8 14:30 file2

✅ cp 명령어 일괄 덮어쓰기 설정 2가지 방법

cp 명령어 사용 중에 ‘overwrite’를 물어보는데 기본 cp 명령에 alias 설정이 cp -i 설정이 되어 있기 떄문입니다.

cp -a
cp: overwrite 'messages_ori'? y
~]# alias cp
alias cp='cp -i'

파일이 한 두 개일 경우에는 괜찮지만 여러 파일을 복사할 경우 번거로워질 수 있습니다.

이럴 경우 아래와 같은 옵션을 사용하여 명령을 수행할 수 있습니다.

yes | cp -ar /home/ ./

자동으로 덮어쓰기를 물어보지 않고 복사가 됩니다. ‘yes |’ 를 앞에 넣어서 사용하거나 역슬러쉬[\]를 넣어서 사용하는 방법입니다.

\cp -ar /home/ ./

✅ 스크립트로 crontab 자동 백업 설정

cp 명령어를 /home 디렉토리에 대한 자동 백업 스크립트로 설정한 예시입니다.

 shell-scripts]# vi home_backup_daily_weekly_monthly.sh
 #!/bin/bash

SRC="/home"
DEST="/backup/home"
DATE=$(date +%Y-%m-%d)

DAY=$(date +%u)
DAY_OF_MONTH=$(date +%d)

mkdir -p "$DEST/daily" "$DEST/weekly" "$DEST/monthly"

echo "$(date) 백업 시작" >> /var/log/home_backup.log 2>&1

cp -aRu "$SRC" "$DEST/daily/home_$DATE"
echo "$(date) 일별 백업 완료" >> /var/log/home_backup.log 2>&1

if [ "$DAY" -eq 7 ]; then
    cp -aRu "$SRC" "$DEST/weekly/home_$DATE"
    echo "$(date) 주별 백업 완료" >> /var/log/home_backup.log 2>&1
fi

if [ "$DAY_OF_MONTH" -eq 01 ]; then
    cp -aRu "$SRC" "$DEST/monthly/home_$DATE"
    echo "$(date) 월별 백업 완료" >> /var/log/home_backup.log 2>&1
fi

 shell-scripts]# chmod 700 home_backup_daily_weekly_monthly.sh 
crontab -e
# home 백업 
05 5 * * * /root/shell-scripts/home_backup_daily_weekly_monthly.sh
# home 백업 테스트
* *  * * * /root/shell-scripts/home_backup_daily_weekly_monthly.sh >> /var/log/home_backup.log 2>&1
Jun  8 16:04:04 ip-172-26-14-231 CROND[307137]: (root) CMDEND (/root/shell-scripts/home_backup_daily_weekly_monthly.sh >> /var/log/home_backup.log 2>&1)
 shell-scripts]# cat /var/log/home_backup.log 
Sun Jun  8 04:03:02 PM KST 2025 백업 시작
Sun Jun  8 04:03:03 PM KST 2025 일별 백업 완료
Sun Jun  8 04:03:04 PM KST 2025 주별 백업 완료
리눅스(Linux)

Similar Posts