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.
If you're talking about the error message, you can suppress that by sending it to the bit bucket:
If you want to suppress the exit code and the error message:
You're looking for something along the lines of
(Unfortunately, the
-f
option is not the droid you're looking for.)If you want to match a glob, that won't work; use
find
instead, e.g.:Old question, but might still be relevant for others.
If you don't need to use cp, you could try with rsync.
To copy all files from a source to a destination directory, run:
Rsync comes with most Unix-like systems such as Linux, Mac OS X or FreeBSD.
Piping the result to true ensures that the command will always succeed. I have tried this on Linux but not on any Mac OS:
You could force the correct error status. With a function:
Given the following:
Regular copy will return an error. It will return an exit status of 1.
If we use the cpalways() function above, any errors will be hidden:
This works
$(cp ./src/*/*.h ./aaa | true)