본문 바로가기

Programming

[리눅스/터미널/스크립트] find 커맨드 사용시 특정 디렉토리를 제외시키는 방법

./

./dir1/file1

./dir2/file2

./dir3/file3

./dir4/file4

./dir5/file5

./dir6/file6

 

경로는 다르게 구성해도 되는데 저는 위와 같이 최대한 단순하게 조직한 다음 예제를 실습해 봤습니다.

 

 

find . -type f -not -path "./dir1/*" -not -path "./dir4/*" -exec cp '{}' ./tmp \;

 

-type f : 찾는 대상이 파일임을 의미

-not -path : 뒤에 나오는 path는 찾지 않는다.

-exec : find 커맨드 결과로 다음을 실행

'{}' : find 커맨드로 찾은 결과물

\; : exec 커맨드의 끝을 의미

 

 

그런데 이렇게 하면

cp: ‘./tmp/file1’ and ‘./tmp/file1’ are the same file
cp: ‘./tmp/file5’ and ‘./tmp/file5’ are the same file
cp: ‘./tmp/file4’ and ‘./tmp/file4’ are the same file

등등등

라는 에러 메시지가 뜰 것이다.

cp 명령어 사용시 보게 되는 메시지 '~~~ are the same file'

복사하려는 대상과 타겟이 동일할 때 발생한다.

ex ) cp ./file1 ./file1

그래서 위와 같은 에러 메시지는 왜 발생하냐면,

find 명령어가, 내가 find로 찾아 복사한 결과물들인 tmp 안의 file들까지 다시 한 번 찾아 ./tmp로 복사하기 때문

 

그래서

find . -type f -not -path "./dir1/*" -not -path "./dir4/*" -not -path "./tmp/*" -exec cp '{}' ./tmp \;

를 실행하면 아주 잘~~ 돌아간다.

'Programming' 카테고리의 다른 글

VSLI 에서 Compilation, Elaboration, Simulation의 의미  (0) 2020.09.09