리눅스 cp 명령어 완벽 가이드

cp [옵션] [–T] 원본파일 대상파일

cp [옵션] 원본파일 디렉토리

cp [옵션] –t 디렉토리 원본파일

원본 파일을 대상 파일로 복사하거나, 여러 원본 파일을 대상 디렉토리로 복사합니다

(1). 주요 옵션

보충: 심볼릭 링크는 특수한 종류의 파일로, 다른 파일의 경로 이름(절대 경로 또는 상대 경로)을 포함합니다.

-a    –dR –preserve=all와 동일
--backup  각 기존 대상 파일에 대한 백업 생성
-b    --backup와 유사하지만 매개변수를 받지 않음
-d    --no-dereference –preserve=links와 동일
-f     대상 파일을 열 수 없으면 제거하고 재시도 (–n이 있을 경우 이 파라미터는 필요 없음)
-i     덮어쓰기 전에 확인 (이전 –n 매개변수를 무효화함)
-n    기존 파일을 덮어쓰지 마세요 (이전 –i 매개변수를 무효화함)
-P,--no-dereference    원본 파일의 심볼릭 링크를 따르지 않음
--preserve     지정된 속성 유지 (기본: 모드, 소유권, 타임스탬프), 가능하면 추가 속성 유지: 환경, 링크, xattr(파일 시스템 확장 속성) 등
-R,-r,--recursive   디렉토리와 하위 디렉토리의 모든 내용을 재귀적으로 복사
-s    파일을 복사하지 않고 심볼릭 링크만 생성
-t     모든 매개변수로 지정된 원본 파일/디렉토리를 대상 디렉토리로 복사
-T    대상 디렉토리를 일반 파일로 취급
-v     수행 중인 작업 설명 (명령 실행 작업을 상세하게 표시)

(2). 실용 예제

보충: mkdir는 디렉토리를 생성하는 데 사용됩니다; ll은 ls -l과 유사합니다.

문서를 다른 폴더로 복사하며, 해당 폴더에 동일한 이름의 문서가 없는 경우

[centos@localhost ~]$ mkdir 복사테스트
[centos@localhost ~]$ ls -l
총합 4
drwxr-xr-x. 2 centos centos 4096 4월  3 12:34 복사테스트
[centos@localhost ~]$ echo "이것은 테스트 파일입니다!" > 텍스트파일.txt
[centos@localhost ~]$ ls -l
총합 8
drwxr-xr-x. 2 centos centos 4096 4월  3 12:34 복사테스트
-rw-r--r--. 1 centos centos   28 4월  3 13:03 텍스트파일.txt
[centos@localhost ~]$ cp 텍스트파일.txt 복사테스트
[centos@localhost ~]$ ls -l ./복사테스트
총합 4
-rw-r--r--. 1 centos centos 28 4월  3 13:05 텍스트파일.txt
[centos@localhost ~]$ cat 텍스트파일.txt
이것은 테스트 파일입니다!
[centos@localhost ~]$ ls -l
총합 8
drwxr-xr-x. 2 centos centos 4096 4월  3 13:05 복사테스트
-rw-r--r--. 1 centos centos   28 4월  3 13:03 텍스트파일.txt

문서를 다른 폴더로 복사하며, 해당 폴더에 동일한 이름의 문서가 있으므로 덮어쓸지 묻습니다.

[centos@localhost ~]$ ls -l
총합 8
drwxr-xr-x. 2 centos centos 4096 4월  3 13:05 복사테스트
-rw-r--r--. 1 centos centos   28 4월  3 13:03 텍스트파일.txt
[centos@localhost ~]$ echo "텍스트를 수정했습니다!" > 텍스트파일.txt
[centos@localhost ~]$ cp 텍스트파일.txt 복사테스트
cp: '복사테스트/텍스트파일.txt'을(를) 덮어쓰시겠습니까? y
[centos@localhost ~]$ cat ./복사테스트/텍스트파일.txt
텍스트를 수정했습니다!

새디렉토리를 Dir1 디렉토리에 복사(Dir1 폴더가 존재할 경우); 새디렉토리의 모든 내용을 새로 생성된 Dir2 디렉토리에 복사(Dir2가 존재하지 않음)

[centos@localhost ~]$ ls -l
총합 0
[centos@localhost ~]$ mkdir 새디렉토리
[centos@localhost ~]$ touch ./새디렉토리/{문서1.txt,문서2.txt}
[centos@localhost ~]$ ls -l
총합 4
drwxr-xr-x. 2 centos centos 4096 4월  3 15:28 새디렉토리
[centos@localhost ~]$ mkdir 대상디렉토리1
[centos@localhost ~]$ cp 새디렉토리 대상디렉토리1
cp: 디렉토리 '새디렉토리'를 건너뜁니다
[centos@localhost ~]$ cp -a 새디렉토리 대상디렉토리1  //대상디렉토리1이 존재하는 경우
[centos@localhost ~]$ cp -a 새디렉토리 대상디렉토리2  //대상디렉토리2가 존재하지 않는 경우
[centos@localhost ~]$ ls -l
총합 12
drwxr-xr-x. 3 centos centos 4096 4월  3 15:29 대상디렉토리1
drwxr-xr-x. 2 centos centos 4096 4월  3 15:28 대상디렉토리2
drwxr-xr-x. 2 centos centos 4096 4월  3 15:28 새디렉토리
[centos@localhost ~]$ ls ./대상디렉토리1  //존재하는 경우의 결과
새디렉토리
[centos@localhost ~]$ ls ./대상디렉토리2  //존재하지 않는 경우의 결과
문서1.txt  문서2.txt

문서1.txt에 대한 바로가기(t1_link) 생성

[centos@localhost ~]$ cp –s 새디렉토리 새디렉토리링크  //-s만으로는 디렉토리의 바로가기를 생성할 수 없음
cp: 디렉토리 '새디렉토리'를 건너뜁니다
[centos@localhost ~]$ cd 새디렉토리
[centos@localhost 새디렉토리]$ ls -l
총합 0
-rw-r--r--. 1 centos centos 0 4월  3 15:28 문서1.txt
-rw-r--r--. 1 centos centos 0 4월  3 15:28 문서2.txt
[centos@localhost 새디렉토리]$ cp –s 문서1.txt 문서1링크
[centos@localhost 새디렉토리]$ ls -l
총합 0
lrwxrwxrwx. 1 centos centos  9 4월  4 08:33 문서1링크 -> 문서1.txt
-rw-r--r--. 1 centos centos 0 4월  3 15:28 문서1.txt
-rw-r--r--. 1 centos centos 0 4월  3 15:28 문서2.txt

디렉토리 바로가기 생성

[centos@localhost ~]$ cp –as 새디렉토리 새디렉토리링크
cp: '새디렉토리링크/문서1링크': 현재 디렉토리에서 상대 경로 심볼릭 링크만 생성할 수 있습니다
cp: '새디렉토리링크/문서1.txt': 현재 디렉토리에서 상대 경로 심볼릭 링크만 생성할 수 있습니다
cp: '새디렉토리링크/문서2.txt': 현재 디렉토리에서 상대 경로 심볼릭 링크만 생성할 수 있습니다
[centos@localhost ~]$ ls -l
총합 16
drwxr-xr-x. 3 centos centos 4096 4월  3 15:29 대상디렉토리1
drwxr-xr-x. 2 centos centos 4096 4월  3 15:28 대상디렉토리2
drwxr-xr-x. 2 centos centos 4096 4월  3 15:28 새디렉토리
drwxr-xr-x. 2 centos centos 4096 4월  3 15:28 새디렉토리링크

접미사를 추가하여 복사본 생성

[centos@localhost ~]$ touch 텍스트파일
[centos@localhost ~]$ cp 텍스트파일{,.txt}
[centos@localhost ~]$ ls -l
총합 16
drwxr-xr-x. 3 centos centos 4096 4월  3 15:29 대상디렉토리1
drwxr-xr-x. 2 centos centos 4096 4월  3 15:28 대상디렉토리2
-rw-r--r--. 1 centos centos    0 4월  4 10:42 텍스트파일
-rw-r--r--. 1 centos centos    0 4월  4 10:42 텍스트파일.txt
drwxr-xr-x. 2 centos centos 4096 4월  4 15:28 새디렉토리
drwxr-xr-x. 2 centos centos 4096 4월  4 15:28 새디렉토리링크

참고: http://www.cnblogs.com/MenAngel/p/5468058.html

태그: 리눅스 명령어 파일복사 cp

6월 14일 21:57에 게시됨