Suppress Error Message of cp (copy)

Posted on May 4, 2012 · Posted in Blog, Linux, Unix

There are times when you don’t need to know if the command you fired threw an error or not. That’s exactly what I needed in one of the systems I maintain. The copy command should not throw an error if there’s no file to copy. So instead of writing a number of lines for validations, I just throw the message to /dev/null.


Here’s the code :

cp /path/to/file.txt /newpath/ 2> /dev/null

Source : existing codes :)