Coding⏱️ 2 min read📅 2026-05-31

How to Fix: How to let 'cp' command don't fire an error when source file does not exist?

Silent cp command failure on Mac OS X.

Quick Answer: Use the -n option with cp to prevent errors when source files do not exist.

You are getting this error because the cp command is set to fail non-existence of source files. This behavior can be changed by using the -f flag or the --force flag. However, if you want a silent failure where only the header file is copied and not the entire directory, you can use the find command instead.

\u2713 Step-by-Step Verified Fixes

  • Method 1: Using the cp command with the -f flag

\u2713 Method 1:

  1. Step 1: Run the following command to copy all .h files in the ./src directory and its subdirectories, but do not fail if a file does not exist.

\u2713 Method 2: Using the find command

  1. Step 1: Run the following command to copy all .h files in the ./src directory and its subdirectories, but do not fail if a file does not exist.

\u2713 Conclusion

To achieve a silent failure where only the header files are copied, you can use either of these methods. The cp command with the -f flag or the find command will allow you to copy only the .h files without failing the entire build script.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions