48

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?

6 Answers 6

66

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.

Sign up to request clarification or add additional context in comments.

8 Comments

However, 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).
@nneonneo: ... aside from the race condition
@rici: yeah, yeah, there's a race condition. It's also still possible for it to report not-found if the destination doesn't exist. It isn't foolproof, but it is better if you only want to suppress one kind of error.
Thanks guys, valid points! Have up-voted so that OP gets a look at it before deciding on the answer.
@tc88: another process could delete the file, which might take place between the test and the action. On Unix, it is not a problem if the file is deleted during the copy, because the file's contents are not removed as long as the file is in use, so the "test before using" idiom creates a race condition. This is a common mistake. See en.m.wikipedia.org/wiki/Time_of_check_to_time_of_use
|
31

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 || : 

2 Comments

Can you explain the colon at the end?
The : 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.
8

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.

Comments

4
rsync -avzh --ignore-missing-args /path/to/source /path/to/destination 

ignore-missing-args: errors ignored are those related to source files not existing

2 Comments

+1 This is voted down, but rsync --ignore-missing-args does exactly what I wanted it to do that brought me to this question: recursive copy, ignoring missing source files.
Yes, it seems like for this case it's best to drop cp and do rsync.
3

Like for any error printed to STDERR, just redirect it to /dev/null:

cp a b 2> /dev/null 

Comments

2

Redirect:

cp ... 2>/dev/null 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.