cp (파일과 디렉토리를 복사한다.)
작성자 정보
- 웹관리자 작성
 - 작성일
 
컨텐츠 정보
- 8,204 조회
 - 0 추천
 - 목록
 
본문
cp
copy의 약어로서 파일이나 디렉토리 전체를 복사할때 사용하는 명령어이다. 
흔히 파일만 복사하는것으로 생각하고 있지만 이 명령어는 특정 옵션을 사용하면 지정한 디렉토리전체를 복사할 수 있다. 
사용법
       cp [options] source dest
       cp [options] source... directory
       Options:
       [-abdfilprsuvxPR]  [-S backup-suffix] [-V {numbered,existing,simple}] [--backup] [--no-deref
       erence] [--force] [--interactive] [--one-file-system] [--preserve]  [--recursive]  [--update]
       [--verbose]      [--suffix=backup-suffix]      [--version-control={numbered,existing,simple}]
       [--archive] [--parents] [--link] [--symbolic-link] [--help] [--version]
사용예 #1
특정파일 하나를 다른 이름으로 복사한 것이다. 
이경우 이름만 다를뿐 파일의 내용을 모두 동일하다.
[root@host1 commmand]# ls -l
합계 4
-rw-r--r--    1 root     root          132  9월 10 16:03 bible.txt
[root@host1 commmand]# 
[root@host1 commmand]# cp bible.txt imsi.txt
[root@host1 commmand]# 
[root@host1 commmand]# ls -l
합계 8
-rw-r--r--    1 root     root          132  9월 10 16:03 bible.txt
-rw-r--r--    1 root     root          132  9월 14 21:47 imsi.txt
[root@host1 commmand]# 
위의 예는 bible.txt라는 파일을 imsi.txt라는 파일로 복사한 것이다. 
두 파일의 이름은 서버 다르지만 두 파일의 내용은 모두 동일함을 꼭 기억하기 바란다. 
여기서 알아두어야하는 것은 mv와 cp 명령어와의 차이점이다. 
mv명령어는 복사하고 난 후에 원본파일이 지워지게 된다는 점이다. 
위의 명령어 대신 "mv bible.txt imsi.txt"를 했다면 bible.txt라는 이름의 파일은 존재하지 않게 되고 imxi.txt만 존재하게 될 것이다.
사용예 #2
이번에는 특정 디렉토리 전체를 복사하는 예를 보자.
디렉토리를 복사할 때에는 -r 또는 -R 옵션 중 하나를 반드시 사용해야한다. 
다음의 예를 보자.
[root@host1 commmand]# ls -l
합계 12
-rw-r--r--    1 root     root          132  9월 10 16:03 bible.txt
drwxr-xr-x    2 root     root         4096  9월 14 21:53 dir1
-rw-r--r--    1 root     root          132  9월 14 21:47 imsi.txt
[root@host1 commmand]# 
[root@host1 commmand]# cp -r dir1 dir2
[root@host1 commmand]# 
[root@host1 commmand]# ls -l
합계 16
-rw-r--r--    1 root     root          132  9월 10 16:03 bible.txt
drwxr-xr-x    2 root     root         4096  9월 14 21:53 dir1
drwxr-xr-x    2 root     root         4096  9월 14 21:55 dir2
-rw-r--r--    1 root     root          132  9월 14 21:47 imsi.txt
[root@host1 commmand]# 
위의 예는 dir1이라는 디렉토리를 dir2라는 이름으로 복사를 한 것이다.
위의 예에서 -r대신 -R을 사용해도 동일하게 복사가 된다. 
물론 dir1에 존재하는 파일들도 모두 그대로 복사된다.
저작권 :  수퍼유저코리아 (www.superuser.co.kr),   무단 재배포및 복사를 금합니다. 
관련자료
- 
			이전
 - 
			다음
 
								