티스토리 뷰

반응형

리눅스에서 현재 디렉토리의 하위 디렉토리까지를 모두 검색하며 특정 조건에 맞는 파일을 복사하는 법을 알아보겠습니다.먼저 생각할 수 있는 방법은 cp 명령어의 --recursive 옵션을 떠올릴 수 있습니다.

$ cp --h

...

-R, -r, --recursive          copy directories recursively

...

$ cp -r /src_dir/*.log /dest_dir


위의 옵션을 사용하면 /src_dir 아래의 모든 디렉토리를 재귀적으로(recursively) 탐색하며 확장자가 log인 파일을 /dest_dir 로 복사(copy)합니다.

하지만 위의 방법은 /src_dir 디렉토리의 하위 디렉토리 구조까지 모두 복사하지 않고 조건에 맞는 모든 파일을 /dest_dir로 복사합니다.

그렇다면 src_dir의 하위 디렉토리 구조까지 /dest_dir로 복사하기 위해서는 어떤 방법이 있을까요??
다음과 같이 find와 cpio(copy in and out)의 조합을 이용하여 하위 디렉토리 구조까지 복사를 할 수 있습니다.

$ cpio --h

...

-p, --pass-through         Run in copy-pass mode

-d, --make-directories     Create leading directories where needed

-m, --preserve-modification-time

                             Retain previous file modification times when

                             creating files

...


$ find /src_dir -name "*.log" | cpio -pdm /dest_dir

 

참고 : http://www.computerhope.com/unix/ucpio.htm

 

Linux cpio command help and examples

Linux cpio command help and information with cpio examples, syntax, related commands, and how to use the cpio command from the command line.

www.computerhope.com

 

반응형

'Linux' 카테고리의 다른 글

리눅스에서 유저 생성하기  (0) 2022.12.10
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함