59

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.

2
  • 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...
    – nmlq
    Commented Mar 23, 2015 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? Commented Mar 23, 2015 at 23:18

5 Answers 5

112

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.

2
  • Wonder why I never thought of that x.x Commented Jul 20, 2017 at 2:30
  • 5
    This might be also useful stackoverflow.com/questions/25931510 for those who want to suppress errors when assigning to a variable var=$(errorcommand 2>/dev/null).
    – Paloha
    Commented Nov 20, 2021 at 1:10
16

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

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

Example:

$ 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
5

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.

#!/bin/bash
shopt -s nullglob
files=(/srv/ftp/201*/wha*/*.jj)
if [[ ${#files[@]} -ne 0 ]]; then
  cp "${files[@]}" ~
fi

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 "$@" ~
fi
1
  • This is the technique necessary if you don't want cp to throw an error from within a shell script -- or at least, I'm not an expert, but this is the one that seems to work.
    – NessBird
    Commented Mar 13, 2020 at 23:42
1

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
0

Add this in ~/.bashrc:

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

then:

source ~/.bashrc
3
  • 4
    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
    Commented Mar 24, 2015 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
    Commented Mar 24, 2015 at 2:11
  • 1
    @M122 maybe the OP means "for this time", rather than permanently. If done as alias, then any copying that failed will be thought of as a success, which is not good. It is like to not write the error handling for cp. If it is for one time to copy a folder where there are files in there that don't have the read permission and we don't want error messages to mess up the screen, that's another matter Commented Dec 13, 2020 at 23:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.