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

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.

  • thanks a lot it worked!! I actually wrongly understoud -f, I though it was for silent!! – Cher May 13 '15 at 14:12
  • @Cher And I thought 'F' was used to Pay respects. :( – Rishav Sep 23 '20 at 17:52
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.