I searched man cp, but can find no quiet option, so that cp does not report "No such file or directory".
How can I make cp not print this error if it attempts, but fails, to copy the file?
Well everyone has suggested that redirecting to /dev/null would prevent you from seeing the error, but here is another way. Test if the file exists and if it does, execute the cp command.
[[ -e f.txt ]] && cp f.txt ff.txt In this case, if the first test fails, then cp will never run and hence no error.
cp may still throw other kinds of errors in this case, e.g. access denied. (It is a great solution if you want to suppress only not-found errors).If you want to suppress just the error messages:
cp original.txt copy.txt 2>/dev/null If you want to suppress bot the error messages and the exit code use:
cp original.txt copy.txt 2>/dev/null || : : command is a shell builtin equivalent to the true command, a no-op that just returns success, i.e., returns 0. In this context it basically says "execute the copy, or, if fails, execute :", wich always succeed, making the whole command always succeed.The general solution is to redirect stderr to the bit bucket:
cp old_file new_file 2>>/dev/null Doing so will hide any bugs in your script, which means that it will silently fail in various circumstances. I use >> rather than > in the redirect in case it's necessary to use a log file instead.
rsync -avzh --ignore-missing-args /path/to/source /path/to/destination ignore-missing-args: errors ignored are those related to source files not existing