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?
To suppress error output in bash , append 2>/dev/null to the end of your command. This redirects filehandle 2 (STDERR) to /dev/null .
To move files, use the mv command (man mv), which is similar to the cp command, except that with mv the file is physically moved from one place to another, instead of being duplicated, as with cp.
> /dev/null throw away stdout. 1> /dev/null throw away stdout. 2> /dev/null throw away stderr. &> /dev/null throw away both stdout and stderr.
By default, cp will overwrite files without asking. If the destination file name already exists, its data is destroyed. If you want to be prompted for confirmation before files are overwritten, use the -i (interactive) option.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With