How to make cp command quiet when no file is found in Bash?

Village picture Village · Aug 16, 2014 · Viewed 28.3k times · Source

I searched map 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?

Answer

jaypal singh picture jaypal singh · Aug 16, 2014

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.