Search code examples
bashgrepls

How to exclude blacklisted files from ls?


I have a blacklist file:

$ cat blacklist
Iran
Iraq
Libya
Somalia
Sudan
Syria
Yemen

How do I exclude the files listed in this blacklist file from the output of ls? I've read the man pages and the closest thing is the --ignore option which unfortunately doesn't read a file. I also thought of piping the output of ls to grep and using the --invert-match option to ignore all the files in the blacklist file but I don't know how to do so.


Solution

  • If you have to use ls, you could do this:

    ls | grep -vFxf blacklist
    
    • -v to invert selection
    • -F to treat lines from file blacklist as strings, not patterns
    • -x to match the whole line
    • -f to reads from blacklist for patterns / strings to match

    Note that the above solution works for all cases except where file names have newlines in them.