Language & OS/Linux

[Linux] rsync 명령어 사용법

아리멤모장 2023. 10. 19. 15:30

실무하면서 로컬에서 파일을 복사하려면 cp를 사용했으나, 로컬에서 서버, 서버에서 서버, 서버에서 로컬로 파일을 복사할 때는 rsync를 많이 사용했었다. 오랜만에 하려고 하니까 매일 쓰던 것도 기억이 가물가물해서 메모해 두려고 한다!

 

rsync 명령어
  • 원격 또는 로컬 간에 파일이나 디렉토리를 복사하는 리눅스의 기본 내장 명령어
  • 로컬 to 로컬, 서버 to 서버, 로컬 to 서버, 서버 to 로컬 다 가능!
# < 원격에 디렉토리 복사>
# rsync [User]@[IP Address]:[File Name] [Path1:가져올 위치] [path2: 저장할 위치]

rsync -avzhP test-directory twpower@192.168.1.2:/home/s~~ ./


# 디렉토리 내 파일들과 하위 디렉토리들 복사
# rsync [Directory Name]/ [User]@[IP Address]:[Path]

rsync -avzhP test-directory/ twpower-private-server:~
rsync -avzhP test-directory/ twpower@192.168.1.2:~


# 원격에 있는 파일 로컬로 가져오기
# rsync [User]@[IP Address]:[File Name] [Path]

rsync -avzhP twpower-private-server:~/test.txt .
rsync -avzhP twpower@192.168.1.2:~/test.txt .

# 전송시 파일명 변경도 가능

rsync -avzhP twpower-private-server:~/test.txt ./test-renamed.txt
rsync -avzhP twpower@192.168.1.2:~test.txt ./test-renamed.txt

# 원격에 있는 디렉토리 로컬로 가져오기
# rsync [User]@[IP Address]:[Directory Name] [Path]
# 디렉토리 자체가 복사된다.

rsync -avzhP twpower-private-server:~/test-directory .
rsync -avzhP twpower@192.168.1.2:~/test-directory .


# 디렉토리 내 파일들과 하위 디렉토리들 복사
# rsync [User]@[IP Address]:[Directory Name]/ [Path]

rsync -avzhP twpower-private-server:~/test-directory/ .
rsync -avzhP twpower@192.168.1.2:~/test-directory/ .

# 만약 포트가 포함되어 있을 경우(기존 원격에서 쓰던 폴더명 그대로 사용하고 싶을 경우, 현재있는 디렉토리에 폴더명과 파일 그대로 생성)
# rsync -e 'ssh -p [포트번호]' -avzh [User]@[IP Address]:[Directory Name]/ [Path]

 rsync -e 'ssh -p 8888' -avzh srjung@bigsoft.iptime.org:/home/srjung/Fun-DataAnalysis ./