LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Ignoring cp errors. (http://www.linuxquestions.org/questions/linux-newbie-8/ignoring-cp-errors-666215/)

0boolean1 08-29-2008 02:28 AM

Ignoring cp errors.
 
Hello

With rm using the -f flag will ignore any errors, e.g.

rm -f test

won't display an error message if test does not exist. However cp doesn't seem to have a similar option. The -f in cp seems to work differently. Is there a way to have cp hide or ignore error message?

Thanks

rocket357 08-29-2008 02:31 AM

$ cp thisFileDoesntExist hello_world
cp: thisFileDoesntExist: No such file or directory
$


to remove the error, just redirect stderr to /dev/null:

$ cp thisFileDoesntExist hello_world 2>/dev/null
$

JSkywalker 08-29-2008 04:10 AM

the '-f' option for cp has the follwing meaning:
Quote:

-f, --force
if an existing destination file cannot be opened, remove it and try again
and there's no switch what to do if the source does not exist, so you have to use the solution rocket357 priveded...

rocket357 08-29-2008 10:02 AM

To post a quick follow-up, there are three "file descriptors" you need to get to know: stdin(0), stdout(1), stderr(2). There's all kinds of nifty things you can do with stdout and stderr...you just have to give the file descriptor *number* (2 in my above post for "stderr").

The beauty of using 2>/dev/null (send all data on stderr to the bit bucket) is that it works with ANY command without touching the "normal" stdout data.

You can also combine stdout and stderr like such:

$ some_command >/dev/null 2>&1 # ">/dev/null" assumes stdout, "2>&1" redirects stderr to stdout (and hence, to /dev/null in this example).


All times are GMT -5. The time now is 09:32 AM.