Linux cp command is not silent

2

I know with a -f cp should be silent but it's not! I do

cp -rf Functional Functionalssssssss

and if Functional does not exist, it says cannot stat 'Functional'... but I just don't want to see the error message!! I want to handle them myself

Cher

Posted 2015-05-13T13:44:07.373

Reputation: 191

--force option shouldnt be silent. http://man7.org/linux/man-pages/man1/cp.1.html to make silent the command just redirect output to /dev/null

– Francisco Tapia – 2015-05-13T14:16:13.623

Answers

8

The cannot stat... output is actually being send to stderr, not stdout. For the specific example you provide in the question, the following will suppress the error output by redirecting stderr to /dev/null:

cp -rf Functional Functionalssssssss 2>/dev/null

As well, at least for the version of cp on my Debian Linux server, -f is not a universal 'silence' flag. It's instead a synonym for --force, meaning that cp will silently obliterate any existing destination file before copying.

hBy2Py

Posted 2015-05-13T13:44:07.373

Reputation: 2 123

thanks a lot it worked!! I actually wrongly understoud -f, I though it was for silent!! – Cher – 2015-05-13T14:12:00.597

1

If you are using bash or sh(posix standard), [ -f file ] && cp file target is what you want.

This one will check if the file exists and copy it. Say goodbye to errors.

Tatsuyuki Ishi

Posted 2015-05-13T13:44:07.373

Reputation: 254

thank you for the answer. This works, but I still don't get why I can't silent the whole command? – Cher – 2015-05-13T13:56:04.690

1This isn't a very good solution. Even if the source file is present, the copy operation could fail for various reasons. – Kenster – 2015-05-13T14:08:24.473

1OP also used -r indicating the source may very well be a directory, not a file as tested by [ -f ... ]. – a CVn – 2015-05-13T14:14:47.870

1This has an inherent race condition: the file might be deleted in between the test and the cp. It probably doesn't matter in this case but similar constructs can and have produced catastrophic security holes and other bugs. – zwol – 2015-05-13T16:05:00.230