Ls | grep regex, ignore files with .tab,

grepregex

I know some regex but I don't know how to get this to work.
I want to list all files that don't have .tab. and don't have ~ at the end of the filename and aren't a .o (or .bin)

I can list all the files that have .tab. by writing

ls | grep .tab.

However I cannot figure out how to make it ignore those files and list everything else. I tried

ls | grep [^(.tab.)]

but got an error and thought my final regex would be

ls | grep ([^(.tab.)]|.o$|.bin$)

But I've never done much regex so I am sure the final is wrong even if the syntax was right.

Best Answer

How I would do this would depend on the context. If this wasn't for a script, and I just wanted to see this, I would be okay with using ls:

ls -a | egrep -v '(tab|\.o$|\.bin$|~$)'

If in a script, I would use a loop:

for file in *; do
#file $file in .* *; do   -- To Include dot (hidden files)                     
   if ! egrep -q '(tab|\.o$|\.bin$|~$)' <<<$file; then
      echo $file
   fi
done

You could add another if test with -f if you only want regular files and not directories. If you want to do this recursively, you could use GNU find with a -regex test and posix-egrep. But take into account the fact that this regex will match the full pathname relative to the directory you searched in.

Another nice option is extended globs if you have support for it, see Dennis's answer.