Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression to exclude filetypes from find

When using find command in linux, one can add a -regex flag that uses emacs regualr expressions to match.

I want find to look for all files except .jar files and .ear files. what would be the regular expression in this case?

Thanks

like image 801
Michael Avatar asked Jul 19 '11 09:07

Michael


People also ask

Can we use regular expression to exclude files?

Sometimes we may find that we want to exclude all files except some specific files from a specific domain: Edit Real Browser Check > Advanced > + Add Excluded File. From the dropdown menu, select 'All Except' and specify the domain or subdomain that you'd like to include.

How do you exclude from find?

We can exclude directories by using the help of “path“, “prune“, “o” and “print” switches with find command. The directory “bit” will be excluded from the find search!

What is difference [] and () in regex?

This answer is not useful. Show activity on this post. [] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

How do I exclude a file from a regular expression?

Enter a file extension. For example, to exclude your music files, enter mp3 or .mp3. Click Add. Create additional exclusions, if desired. Click Save. A regular expression (regex) is a search pattern that locates files and folders containing a specific sequence of characters by comparing that sequence to absolute file paths on your device.

How to exclude or include files in Unix?

The find command support standard UNIX regex to match, include or exclude files. You can write complex queries easily with regex while find command recursively descends the directory tree for each /file/to/path listed, evaluating an expression. Find command exclude or ignore files syntax. The syntax is as follows:

How do I use regular expressions to backup files?

Click Save. A regular expression (regex) is a search pattern that locates files and folders containing a specific sequence of characters by comparing that sequence to absolute file paths on your device. You can use the power of regular expressions to fine-tune and allow for more complex backup file exclusion rules.

How to exclude certain files and file types from a backup?

Certain files and file types can be globally excluded from a backup without having to modify the file selection by deselecting individual files and folders. These exclusions can be added either by file type or by using regular expressions ( regex ).


2 Answers

You don't need a regex here. You can use find with the -name and -not options:

find . -not -name "*.jar" -not -name "*.ear" 

A more concise (but less readable) version of the above is:

find . ! \( -name "*.jar" -o -name "*.ear" \) 
like image 139
dogbane Avatar answered Sep 20 '22 06:09

dogbane


EDIT: New approach:

Since POSIX regexes don't support lookaround, you need to negate the match result:

find . -not -regex ".*\.[je]ar" 

The previously posted answer uses lookbehind and thus won't work here, but here it is for completeness' sake:

.*(?<!\.[je]ar)$ 
like image 30
Tim Pietzcker Avatar answered Sep 21 '22 06:09

Tim Pietzcker