使用 -exec 复制与 find 命令条件匹配的所有文件

使用 -exec 复制与 find 命令条件匹配的所有文件

以下是我尝试将除.out.class文件之外的所有文件复制到标题为 的子目录中homework21/。 Notehomework21是我正在其中工作的文件夹的子目录。

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/ \;

请注意,-r标志 incp不是必需的,因为您只是复制文件而不是目录。我补充-v说这cp很冗长。

相关内容