Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

cp

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?

like image 366
Village Avatar asked Aug 16 '14 06:08

Village


People also ask

How do I hide errors in bash?

To suppress error output in bash , append 2>/dev/null to the end of your command. This redirects filehandle 2 (STDERR) to /dev/null .

How copy file without using cp command in Linux?

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.

How do I hide errors in Linux?

> /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.

Does bash cp overwrite?

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.


1 Answers

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.

like image 84
jaypal singh Avatar answered Sep 18 '22 13:09

jaypal singh