How do I list every file in a directory except those with specified extensions?
Assuming one has GNU ls
, this is possibly the simplest way:
ls -I "*.txt" -I "*.pdf"
If you want to iterate across all the subdirectories:
ls -I "*.txt" -I "*.pdf" -R
Find supports -o
find . ! '(' -name '*.txt' -o -name '*.pdf' ')'
You need the parenthesis to make the precedence right. Find does a lot of stuff; I suggest reading through its manpage.
You can also do an or in grep
(but really, you should not parse the output of ls
)
ls | grep -Ev '\.(txt|pdf)$' | column
With bash
extended globbing (turn on with shopt -s extglob
), the glob !(*.txt|*.pdf)
should work. You can pass this glob directly to any command that takes file arguments, including but not limited to ls
.