I am currently looking ways to suppress error command in Linux, in particular, the command cp.

I do:

root@ubuntu:~$ cp /srv/ftp/201*/wha*/*.jj ~/.
cp: cannot stat `/srv/ftp/201*/wha*/*.jj': No such file or directory

How do I suppress the error message that gets printed on the screen? I.e., I don't want to see this error message in my monitor.

  • 1
    just FYI, best way to check for errors is exit codes. Piping STDERR to /dev/null is standard and will get rid of any visual indication of errors, but checking for a non zero return status will let you know if there was an error or not, at least that is how it is supposed to be... – Nathan McCoy Mar 23 '15 at 18:06
  • Your question is not clear. Do you want to hide all error messages from cp? Or do you want to have no error message if the wildcard doesn't match any files? Or some other criteria? – Gilles 'SO- stop being evil' Mar 23 '15 at 23:18

To suppress error output in bash, append 2>/dev/null to the end of your command. This redirects filehandle 2 (STDERR) to /dev/null. There are similar constructs in other shells, though the specific construct may vary slightly.

  • Wonder why I never thought of that x.x – Abdul Jul 20 '17 at 2:30

Redirect the error message (STDERR) to /dev/null:

root@ubuntu:~$ cp /srv/ftp/201*/wha*/*.jj ~/. 2>/dev/null


$ cp /srv/ftp/201*/wha*/*.jj ~/.  ##Error message gets printed
cp: cannot stat ‘/srv/ftp/201*/wha*/*.jj’: No such file or directory

$ cp /srv/ftp/201*/wha*/*.jj ~/. 2>/dev/null  ##No error message gets printed

Your question is not clear. The most sensible thing to do would be to not run cp at all when the wildcard doesn't match any file, rather than run cp and hide the error message.

To do that, if the shell is bash, set the nullglob option so that the wildcard pattern expands to nothing if it doesn't match any files. Then check whether the pattern expanded to anything, and don't call cp in that case.

shopt -s nullglob
if [[ ${#files[@]} -ne 0 ]]; then
  cp "${files[@]}" ~

In plain sh, test whether the glob was left unchanged, pointing to a non-existent file.

set -- /srv/ftp/201*/wha*/*.jj
if ! [ -e "$1" ] && ! [ -L "$1" ]; then
  cp "$@" ~

Add this in ~/.bashrc:

alias cp='cp 2> /dev/null'


source ~/.bashrc
  • 2
    Recommending that this be done permanently seems silly to me. It seems like a much better idea to just pipe stderr to /dev/null when the error output is unwanted. Or using a different name than cp. – HalosGhost Mar 24 '15 at 1:50
  • I agree with you but this is a solution for someone who doesn't want to see any error messages on his monitor for cp. – M122 Mar 24 '15 at 2:11

you can use either:
1 option: 2>/dev/null.
2 option: 2>&1
Besides you can use this at the end of your command it will suppress the error messages:

Example here-

$cp nofile.txt b.txt > log.txt 2>/dev/null

here u can't retrieve any info about the error message. Ex2:

$cp nofile.txt b.txt > log.txt 2>&1

here you can retrieve some info from the below log file:

$ cat log.txt
cp: cannot stat `nofile.txt': No such file or directory

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.