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
  • 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 May 13 '15 at 14:16

2 Answers2

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
  • 2,243
  • 14
  • 28
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.

  • thank you for the answer. This works, but I still don't get why I can't silent the whole command? – Cher May 13 '15 at 13:56
  • 1
    This isn't a very good solution. Even if the source file is present, the copy operation could fail for various reasons. – Kenster May 13 '15 at 14:08
  • 1
    OP also used -r indicating the source may very well be a directory, not a file as tested by [ -f ... ]. – user May 13 '15 at 14:14
  • 1
    This 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 May 13 '15 at 16:05