74

How can I suppress error messages for a shell command?

For example, if there are only jpg files in a directory, running ls *.zip gives an error message:

   $ ls *.zip
   ls: cannot access '*.zip': No such file or directory

Is there an option to suppress such error messages? I want to use this command in a Bash script, but I want to hide all errors.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter
  • 1,224
  • 3
  • 16
  • 28
  • 9
    If you are using `ls` to get a list of file names to process, [don't](http://mywiki.wooledge.org/ParsingLs). – chepner Sep 03 '15 at 15:35
  • 2
    You could use `shopt -s nullglob` so that the glob becomes null. Of course that would work very well with ls, since it would return all files in the directory. It *would* work though if you use something like `for file in *.zip` – user000001 Sep 03 '15 at 15:36
  • 2
    to suppress error message just redirect *stderr* to `/dev/null`, as in `ls *.zip 2>/dev/null` – Luis Colorado Sep 04 '15 at 06:16
  • @chepner Shellcheck for bash recommends to use ls instead of grep. – Kalib Zen Dec 18 '20 at 03:03
  • @KalibZen Where? I'm talking about avoiding things like something like `for f in $(ls ...)` or `ls | while read f; do`. – chepner Dec 18 '20 at 15:14
  • I think there is nothing wrong with OP code. he can do `ab=$(ls input.txt/*findMystring*)` and he can loop that `$ab` but using this is not recommended: `ls input.txt | grep 'mystring'` https://github.com/koalaman/shellcheck/wiki/SC2010 – Kalib Zen Dec 18 '20 at 21:35

5 Answers5

112

Most Unix commands, including ls, will write regular output to standard output and error messages to standard error, so you can use Bash redirection to throw away the error messages while leaving the regular output in place:

ls *.zip 2> /dev/null
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AJefferiss
  • 1,628
  • 1
  • 13
  • 17
16
$ ls *.zip 2>/dev/null

will redirect any error messages on stderr to /dev/null (i.e. you won't see them)

Note the return value (given by $?) will still reflect that an error occurred.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
11

To suppress error messages and also return the exit status zero, append || true. For example:

$ ls *.zip && echo hello
ls: cannot access *.zip: No such file or directory
$ ls *.zip 2>/dev/null && echo hello
$ ls *.zip 2>/dev/null || true && echo hello
hello

$ touch x.zip
$ ls *.zip 2>/dev/null || true && echo hello
x.zip
hello
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
A-C
  • 131
  • 1
  • 4
0

I attempted ls -R [existing file] and got an immediate error. ls: cannot access 'existing file': No such file or directory

So, I used the following:

ls -R 2>dev/null | grep -i [existing file]*

ls -R 2>dev/null | grep -i text*

Or, in your case:

ls -R 2>dev/null | grep -i *.zip

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 04 '22 at 22:02
0

My solution with a raspberry pi3 with buster.

ls -R 2>/dev/null | grep -i [existing file]*

2>/dev/null is very usefull with Bash script to avoid useless warnings or errors.

Do not forget slash caracter

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 09 '23 at 04:50