cat all files except a select few

Posted on

Problem :

If there some particular syntax so that I can cat all files in a directory, except for the select ones. For eg., suppose I have ~1200 text files containing some lines, about 3 or 4 lines per file, except there are a few files:

readme.md
contrib.rst
licence.txt

which are longer. I want to view the contents of all files except the 3 (or if possible, more) files mentioned above.

I tried the following command:

cat !{readme.md,contrib.rst,licence.txt}*.*

because I saw somewhere a usage of cat as follows:

cat [!t][!m][!p]*.sh

I thought that since, {a,b} means a set in bash-scripts, negating that should work. Of course I was wrong.

Solution :

Try this with bash’s extended pattern matching feature:

shopt -s extglob
cat !(readme.md|contrib.rst|licence.txt)

Try this with bash:

GLOBIGNORE="readme.md:contrib.rst:licence.txt"
cat *
unset GLOBIGNORE

What did you already try?

Are it always the same files which you want to skip.

In that case you could hardcode them in a command. E.g. something like
find ! -name readme.md -o ! -name contrib.rst -o ! -name licence.txt -exec cat {} +

Now if you want something which skips all files larger than 4 lines without knowing the file names beforehand then the solution will obviously get more complex. In that case please edit your post.

Without modifying the current shell setting it’s possible to execute each of the Cyrus proposal in a subshell

(GLOBIGNORE="readme.md:contrib.rst:licence.txt" ; cat *)

as well as

(shopt -s extglob; cat !(readme.md|contrib.rst|licence.txt))

Leave a Reply

Your email address will not be published. Required fields are marked *