Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find command search pattern

I have below 4 files

a_ROLLBACK2to3__test.sql,
a_1to2__test.sql,
a_2to3__test.sql,
a_2to2__test.sql

I want to write a find command to return the files a_1to2__test.sql, a_2to3__test.sql and a_2to2__test.sql, the file a_ROLLBACK2to3__test.sql should not be included in the search.

my find command looks like

find . -name "*_*to*__*.sql"

but this returns all files but I don’t want a_ROLLBACK2to3__test.sql.

basically the files with ROLLBACK after the first _ should not be included..

Can anyone help me to write the search pattern for my requirement?

Thanks

like image 362
Tech Tech Avatar asked Sep 12 '26 15:09

Tech Tech


2 Answers

Simply filter the results with grep:

find . -name '*_*to*__*.sql' | grep -v ROLLBACK

Or use the AND clause -a with negation !:

find . -name '*_*to*__*.sql' -a ! -name '*ROLLBACK*'

like image 83
Mr. Llama Avatar answered Sep 14 '26 07:09

Mr. Llama


You could simply look for the underscore followed by a digit:

find . -name '*_[0-9]*to*__*.sql'

or for an underscore not followed by R:

find . -name '*_[!R]*to*__*.sql'
like image 36
Jonathan Leffler Avatar answered Sep 14 '26 06:09

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!