How to let ‘cp’ command don’t fire an error when source file does not exist

copyshell

I'm using Mac OS X.
I'm trying to copying some files with cp command for a build script like this.

cp ./src/*/*.h ./aaa

But this command fires an error if there is no .h file in ./src directory.
How to make the command don't fire the error? (silent failure) The error makes build result fail, but I just want to copy when only there are some header file.

Best Answer

If you're talking about the error message, you can suppress that by sending it to the bit bucket:

cp ./src/*/*.h ./aaa 2>/dev/null

If you want to suppress the exit code and the error message:

cp ./src/*/*.h ./aaa 2>/dev/null || :