프로그래밍/Unix

유닉스 복사, 삭제, 이동

미냐님 2020. 3. 23. 00:50
728x90

파일 복사



cp 명령

  • 파일이나 디렉토리를 복사
  • 형식 : cp [옵션] 파일명1/디렉토리명1 파일명2/디렉토리명2
  • 옵션
    • -i : 파일명2가 존재할 경우 덮어쓸 것인지 물어봄
    • -r :디렉토리를 복사할 때 지정
  • 사용 예 :
    p file1 file2
    cp f1 f2 f3 dir1
    cp –r dir1 dir2

파일을 파일로 복사

  • 첫 번째 인자와 두 번째 인자 모두 파일명 사용
  • 두 번째 인자의 파일명이 존재하지 않으면 새로 생성됨
  • 이미 존재하면 원래의 파일에 덮어 씌워짐
  • /etc 디렉토리에 있는 파일을 현재 디렉토리의 etc_hosts라는 파일로 복사
$ cp /etc/hosts etc_hosts
$ ls
etc_hosts
$
  • 현재 디렉토리에서 파일 복사
$ cp etc_hosts tmp_hosts
$ ls
etc_hosts tmp_hosts
$

파일을 디렉토리로 복사

  • 첫 번째 인자는 파일명, 두 번째 인자는 디렉토리명 사용
  • 파일을 현재 디렉토리에 복사
$ cp /etc/hosts .
$ ls
etc_hosts hosts tmp_hosts
$
  • 현재 디렉토리가 아닌 다른 디렉토리로 복사
$ mkdir temp
$ cp hosts temp
$ ls temp
hosts
$
  • 파일명을 바꿔서 복사
$ cp hosts temp/hosts1
$ ls temp
hosts hosts1
$

여러 개의 파일을 디렉토리에 복사

  • 첫 번째 인자의 자리에 파일명 여러 개 사용
  • 마지막 인자는 반드시 디렉토리명이어야 함
$ cp etc_hosts tmp_hosts temp
$ ls temp
etc_hosts hosts hosts1 tmp_hosts
$

-i 옵션 사용

  • 두 번째 인자에 지정한 파일명이 이미 존재하면 겹쳐서 복사할 것인지 물어봄
  • “y”나 “n”으로 대답
$ cp –i tmp_hosts hosts
cp: overwrite hosts? n
$

디렉토리를 복사

  • -r 옵션 사용하지 않을 경우 오류 발생
    • 유닉스 버전에 따라 오류 메시지는 다르게 출력됨
    • “cp: temp: is a directory“라는 오류 메시지가 출력되기도 함
$ cp temp tmp
cp: omitting directory ‘temp’
$
  • -r 옵션 사용하여 복사할 경우 디렉토리 내에 있던 파일이나 하위 디렉토리 모두 복사
$ cp –r temp tmp
$ ls –F
etc_hosts hosts temp/ tmp/ tmp_hosts
$ ls tmp
hosts hosts1
$
  • 두 번째 인자인 tmp 디렉토리가 원래 없었으므로 원본 디렉토리와 같은 위치에 새로 생성됨
  • 두 번째 인자가 기존에 있던 디렉토리 이름이면 원본 디렉토리는 두 번째 인자로 지정한 디렉토리 아래에 같은 이름으로 복사됨

 

파일 삭제

 

###rm 명령

  • 파일을 삭제
  • 형식 : rm [옵션] 파일명/디렉토리명
  • 옵션
    • -i : 지정한 파일을 정말 삭제할 것인지 확인
    • -r : 디렉토리를 삭제할 때 지정
  • 사용 예 :
    • rm file
rm –r dir

기본 삭제

  • 인자를 파일로 지정하면 해당파일 삭제
  • 유닉스에는 ‘휴지통’ 기능이 없어, 삭제된 파일은 복구할 수 없음
$ rm hosts
$

-i 옵션을 사용한 파일 삭제

  • 지정한 파일을 정말 삭제할 것인지 물어봄
    • y를 입력하면 삭제되고, n을 입력하면 삭제되지 않음
$ rm –i hosts1
rm: remove regular file hosts1? y
$

디렉토리 삭제

  • -r 옵션을 사용하면 디렉토리가 비어 있는지 여부와 상관없이 디렉토리 및 그 아래 모든 파일 삭제
  • -r 옵션 없이 삭제
$ rm tmp
rm: cannot remove ‘tmp’: Is a directory
$
  • rmdir 명령으로 삭제
$ rmdir tmp
rmdir: failed to remove ‘tmp’: Directory not empty
$ ls tmp
hosts hosts1 text
$
  • -r 옵션으로 삭제
$ rm –r tmp
$ ls –F
etc_hosts hosts temp/ tmp_hosts
$
  • -ri 옵션으로 삭제
$ rm –ri temp
rm: descend into directory temp? y
rm: descend into directory temp/tmp? y
rm: remove regular file temp/tmp/text1? y
rm: remove directory temp/tmp? y
rm: remove regular file temp/hosts1? y
rm: remove regular file temp/text? y
rm: remove regular file temp/hosts? y
rm: remove directory temp? n
$

 

파일 이동



mv(move) 명령

  • 파일의 이름을 바꾸거나 다른 디렉토리로 이동
  • 형식 : mv [옵션] 파일명1/디렉토리명1 파일명2/디렉토리명2
  • 옵션
    • -i : 파일명2/디렉토리명2가 존재하면 덮어쓸 것인지 물어봄
  • 사용 예 :
mv file1 file2
mv file1 dir1
mv file1 file2 file3 dir1

파일을 파일로 이동

  • 첫 번째 인자와 두 번째 인자를 모두 파일명으로 지정
  • 결과적으로는 이름 변경
  • 두 번째 인자로 지정한 파일이 존재하면 내용 덮어씀
$ mv hosts text
$ ls –F
etc_hosts temp/ text tmp_hosts
$

파일을 디렉토리로 이동

  • 두 번째 인자로 디렉토리 지정
$ mv text temp
$ ls –F
etc_hosts temp/ tmp_hosts
$
  • 두 번째 인자로 디렉토리명과 파일명을 모두 지정하면 지정한 디렉토리로 파일명 바꿔서 이동
$ mv tmp_hosts temp/hosts
$ ls –F
etc_hosts temp/
$ ls temp
hosts text
$

여러 개의 파일을 디렉토리로 이동

  • 첫 번째 인자의 자리에 파일명 여러 개 지정하면 두 번째 인자를 반드시 디렉토리로 해야 함
  • 마지막 지정한 디렉토리로 파일이 모두 이동
$ mv temp/hosts temp/text .
$ ls –F
etc_hosts hosts temp/ text
$

-i 옵션을 사용한 이동

  • 두 번째 인자에 지정한 파일명이 기존에 있는 파일일 경우 덮어쓸 것인지 물어봄
$ mv –i etc_hosts hosts
mv: overwrite ‘hosts’? n
$

디렉토리를 디렉토리로 이동

  • 두 인자 모두 디렉토리명으로 지정
  • 첫 번째 인자인 디렉토리가 두 번째 인자 디렉토리 아래로 이동
  • 두 번째 인자가 새로운 이름이면 디렉토리 이름 변경
$ cp –r temp tmp
$ ls –F
etc_hosts hosts temp/ text tmp/
$ mv tmp temp
$ ls -F
etc_hosts hosts temp/ text
$ ls –F temp
etc_hosts hosts text tmp/
$
728x90