-exec를 사용하여 find 명령 조건과 일치하는 모든 파일을 복사합니다.

-exec를 사용하여 find 명령 조건과 일치하는 모든 파일을 복사합니다.

.out다음은 및 .class파일을 제외한 모든 파일을 이라는 하위 디렉터리에 복사하려는 시도입니다 homework21/. Note는 homework21제가 작업 중인 폴더의 하위 디렉터리입니다.

me@ice4:~/pset2$ find . -type f ! '(' -name '*.out' -o -name '*.class' ')'
./PartB.txt
./DooBee.java
./Prob8.java
./Diamond.java
./Prob10.java
./Prob11.java
./Drawing.java
./typescript
me@ice4:~/pset2$ find . -type f ! '(' -name '*.out' -o -name '*.class' ')' -exec cp -r homework21/. {} \;
cp: cannot overwrite non-directory `./PartB.txt' with directory `homework21/.'
cp: cannot overwrite non-directory `./DooBee.java' with directory `homework21/.'
cp: cannot overwrite non-directory `./Prob8.java' with directory `homework21/.'
cp: cannot overwrite non-directory `./Diamond.java' with directory `homework21/.'
cp: cannot overwrite non-directory `./Prob10.java' with directory `homework21/.'
cp: cannot overwrite non-directory `./Prob11.java' with directory `homework21/.'
cp: cannot overwrite non-directory `./Drawing.java' with directory `homework21/.'
cp: cannot overwrite non-directory `./typescript' with directory `homework21/.'
me@ice4:~/pset2$ ls homework21/
me@ice4:~/pset2$ find . -type f ! '(' -name '*.out' -o -name '*.class' ')' -exec cp homework21/. {} \;
cp: omitting directory `homework21/.'
cp: omitting directory `homework21/.'
cp: omitting directory `homework21/.'
cp: omitting directory `homework21/.'
cp: omitting directory `homework21/.'
cp: omitting directory `homework21/.'
cp: omitting directory `homework21/.'
cp: omitting directory `homework21/.'
me@ice4:~/pset2$ ls homework21/
me@ice4:~/pset2$ find . -type f ! '(' -name '*.out' -o -name '*.class' ')' -exec cp homework21/ {} \;
cp: omitting directory `homework21/'
cp: omitting directory `homework21/'
cp: omitting directory `homework21/'
cp: omitting directory `homework21/'
cp: omitting directory `homework21/'
cp: omitting directory `homework21/'
cp: omitting directory `homework21/'
cp: omitting directory `homework21/'
me@ice4:~/pset2$ find . -type f ! '(' -name '*.out' -o -name '*.class' ')' -exec cp -r homework21/ {} \;
cp: cannot overwrite non-directory `./PartB.txt' with directory `homework21/'
cp: cannot overwrite non-directory `./DooBee.java' with directory `homework21/'
cp: cannot overwrite non-directory `./Prob8.java' with directory `homework21/'
cp: cannot overwrite non-directory `./Diamond.java' with directory `homework21/'
cp: cannot overwrite non-directory `./Prob10.java' with directory `homework21/'
cp: cannot overwrite non-directory `./Prob11.java' with directory `homework21/'
cp: cannot overwrite non-directory `./Drawing.java' with directory `homework21/'
cp: cannot overwrite non-directory `./typescript' with directory `homework21/'

왜 이것이 작동하지 않습니까?

답변1

cp 명령의 구문은 다음과 같습니다.

cp source target

따라서 다음 find명령을 사용하십시오.

find . -type f ! '(' -name '*.out' -o -name '*.class' ')' -exec cp -v {} homework21/ \;

디렉터리가 아닌 파일만 복사하므로 -rin 플래그는 필요 하지 않습니다. 내용이 길었다고 cp덧붙였습니다 .-vcp

관련 정보