I want to copy all binaries from /sourcedir and its sub-dirs to /destdir. Basically, all files with: no extension, and all files with *.a, *.so, *.ko, and exclude from copy: Makefile and *.depend. Exclude file copy from the sub-dir named "excludeDir". The command should place all binaries in a single folder.

I have tried the following from bash:

find /my/sourcedir/ -mindepth 2 -type f -not -iname "excludeDir" -or "*.c" -or "*.h" -or "makefile" -print -exec cp {} /my/destdir \;

bash script yields the following error:

find: paths must precede expression: `*.c'

The part that is giving me trouble are the exclusions (files: *.h, *.c, Makefile, and the sub-directory: "excludeDir" )

Using mjb2kmn's advice the following command does well except for globbing.

find /opt/ppmac-exp/ -mindepth 2 -not -iname *.c -not -iname *.cpp -not -iname *.cc -not -iname *.cs -not -iname *.h -not -iname *.cfg -not -iname *.sh -not -iname *.layout -not -iname *.depend -not -iname Makefile -not -iname Makefile* -type f -print -exec cp {} /opt/build \;
  • If you mean -iname to apply to ".c"? If so, you need to use a complete expression. ... -iname "excludeDir" -or -iname ".c" -or ... Also the -print and -exec options might need to be before the -not since they always return true and might negate the match. – virullius Dec 4 '19 at 19:30
  • Correct mjb2kmn I want to exclude all *.c and *.h files from the copy process. Additionally it would be nice but not essential to exclude the excludeDir from the sub-directories that files are copied from. – masher Dec 4 '19 at 20:05
  • Thank you that helped take care of the bash error, but now there is a globbing problem. – masher Dec 4 '19 at 20:50

... with the help of mjb2kmn and dash-o on stackoverflow, this worked and prevented globbing. Thank you all!

find /my/sourcedir/ -mindepth 2 -type f \( -not -iname "excludeDir" -not -iname '*.c' -not -iname '*.h' -not -iname '.ssh' -not -iname "Makefile" \) -exec cp {} /my/destdir \;

I realize the original poster asked about doing this in find.

What you are doing is fairly complicated to express easily in find.

If you have perl on the system, you might do better using find2perl. It takes your syntax, and writes a little perl script for you that you can just run; or edit to add more checks and such like. See https://perldoc.perl.org/5.8.8/find2perl.html

Or, simply define the names of what you want to copy in the Makefile, and make a target that does the copy directly as a part of the makefile...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.