Skip to content
Advertisement

Exclude list of file extensions from find in bash shell

I want to write a cleanup routine for my make file that removes every thing except the necessary source files in my folder. For example, my folder contains files with the following extensions: .f .f90 .F90 .F03 .o .h .out .dat .txt .hdf .gif.

I know I can accomplish this with:

JavaScript

Using negation, I can do this:

JavaScript

But, when I try to do this:

JavaScript

I get an error:

JavaScript

(In this case, I would get: find: paths must exceed expression: *.f* )

Can you explain why this happens, and how to do what I am trying to do? I just hate writing -not -name every time I want to add a file extension to the list. Also, I want to find out why this is giving me an error so that I can learn Linux better.

Thanks!

Advertisement

Answer

JavaScript

is interpreted as

JavaScript

leading to the error.

Since these are single-letter extensions, you can collapse them to a single glob:

JavaScript

but if they are longer, you have to write out the globs

JavaScript

or

JavaScript

or switch to using regular expressions.

JavaScript

Note that regular expressions in find is not part of the POSIX standard and might not be available in all implementations.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement