Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude blacklisted files from ls?

Tags:

grep

bash

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.

like image 473
Aadit M Shah Avatar asked Jan 22 '26 12:01

Aadit M Shah


1 Answers

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.

like image 156
codeforester Avatar answered Jan 24 '26 05:01

codeforester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!