How do I suppress error messages from cp?

98,677

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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
Share:
98,677

Related videos on Youtube

andy_ttse
Author by

andy_ttse

Updated on September 18, 2022

Comments

  • andy_ttse
    andy_ttse almost 2 years

    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.

    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' over 9 years
      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?
  • Jakob Bennemann
    Jakob Bennemann over 9 years
    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.
  • M122
    M122 over 9 years
    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.
  • Honinbo Shusaku
    Honinbo Shusaku almost 7 years
    Wonder why I never thought of that x.x
  • NessBird
    NessBird over 4 years
    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.
  • nonopolarity
    nonopolarity over 3 years
    @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
  • Paloha
    Paloha over 2 years
    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).