Reputation: 24363
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?
Upvotes: 45
Views: 54403
Reputation: 241701
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.
Upvotes: 8
Reputation: 1438
rsync -avzh --ignore-missing-args /path/to/source /path/to/destination
ignore-missing-args
: errors ignored are those related to source files not existing
Upvotes: 3
Reputation: 6674
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 || :
Upvotes: 31
Reputation: 77095
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.
Upvotes: 63
Reputation: 29811
Like for any error printed to STDERR, just redirect it to /dev/null
:
cp a b 2> /dev/null
Upvotes: 3