0

I wanted to search all the files starting with chrom in /var directory. So I executed this

$ ls -R | grep "^chrom"

To my astonishment, It showed this. Few starting lines look like this :

amit@ElMaverick:/var$ ls -R | grep "^chromium"

chromium-browser_55.0.2883.87-0ubuntu0.16.04.1263_amd64.deb
chromium-browser-l10n_55.0.2883.87-0ubuntu0.16.04.1263_all.deb
chromium-codecs-ffmpeg-extra_55.0.2883.87-0ubuntu0.16.04.1263_amd64.deb
ls: cannot open directory './cache/apt/archives/partial': Permission denied
ls: cannot open directory './cache/cups': Permission denied
ls: cannot open directory './cache/ldconfig': Permission denied
chromium-browser_chromium-browser.png
chromium-browser_chromium-browser.png
chromium-bsu_chromium-bsu.png
chromium-browser_chromium-browser.png
ls: cannot open directory './lib/apt/lists/partial': Permission denied
ls: cannot open directory './lib/bluetooth/78:0C:B8:46:10:D5': Permission denied
chromium-browser.conffiles
chromium-browser-l10n.list
chromium-browser-l10n.md5sums
chromium-browser.list
chromium-browser.md5sums
chromium-browser.postinst
chromium-browser.prerm
chromium-codecs-ffmpeg-extra.list
chromium-codecs-ffmpeg-extra.md5sums
ls: cannot open directory './lib/mysql': Permission denied
ls: cannot open directory './lib/mysql-files': Permission denied

I don't want to see these lines in the output.

ls: cannot open directory

So, I have 2 questions here :

  1. How to filter only those lines which have no error of any type ?
  2. Is there a way to supress the specific lines of output ?

Please provide a general approach for the problem, not just specific to this question.

1
  • A general approach is reliable only as far as programs reliably output "undesired" output to stderr; otherwise, you have to know what you don't want to see. Commented Feb 13, 2018 at 12:01

2 Answers 2

4
  1. Redirect the standard error stream of ls to /dev/null:

    ls -R 2>/dev/null | grep '^chrom'
    
  2. Do further grepping:

    ls -R 2>/dev/null | grep '^chrom' | grep -v 'some additional pattern'
    

You may also consider using find (Why *not* parse `ls`?):

find . -name 'chrom*' ! -name 'some additional pattern' 2>/dev/null

Note that find uses filename globbing patterns while grep uses regular expressions. With find you may also look specifically for files (with -type f) or for directories (-type d) etc., e.g.

find . -type f -name 'chrom*' ! -name 'some additional pattern' 2>/dev/null

Each part of the find command acts like a "test" against the found pathname. -type tests against the file type while -name tests against the final filename component of the path. The tests are logically AND-ed together (unless -o is used). An exclamation point inverts the sense of a test.

0
2

Better use it's better suites for this kind of tasks :

As simple user (because you don't have permission to read files as non root):

$ find /var -type f -name 'chrom*' 2>/dev/null

Or as root :

# find /var -type f -name 'chrom*'

And if you want to filter out a specific error in , use :

command 2> >(grep -v 'specific error')

Warning

Never parse ls output !

2
  • Still shows the same output including those error lines also. Commented Feb 13, 2018 at 11:53
  • Check my edited POST Commented Feb 13, 2018 at 11:55

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.