UNIX find

UNIX find

How to search files patterns and execute a command on each found item

You have a bunch of files maybe in different subdirectories. You want to search for a certain pattern and within the found items you want to grep the content. It works like this:

cbilz@MacBook-Air-M2 datamodel % ls -l update*                                           
-rw-r--r--@ 1 cbilz  staff  1070 Jul 28  2022 update_1.2.1.sql
-rw-r--r--@ 1 cbilz  staff  1016 Jul 28  2022 update_1.2.2.sql
-rw-r--r--@ 1 cbilz  staff  1824 Sep 21  2022 update_1.3.0.sql
-rw-r--r--@ 1 cbilz  staff  4205 Sep 21  2022 update_1.3.1.sql
-rw-r--r--@ 1 cbilz  staff  1017 Sep 21  2022 update_1.3.2.sql
-rw-r--r--@ 1 cbilz  staff   583 Jan 27  2023 update_1.3.3.sql
-rw-r--r--@ 1 cbilz  staff    35 Feb  2  2023 update_1.3.4.sql
-rw-r--r--  1 cbilz  staff  5730 Mar  4 14:36 update_1.4.0.sql
-rw-r--r--@ 1 cbilz  staff  2951 Feb 19  2023 update_1.4.1.sql
-rw-r--r--@ 1 cbilz  staff   822 Mar  1 11:39 update_1.4.2.sql
-rw-r--r--  1 cbilz  staff  1334 Mar  5 17:22 update_1.4.3.sql
-rw-r--r--  1 cbilz  staff  4201 Mar  9 06:43 update_1.5.0.sql
-rw-r--r--  1 cbilz  staff  2220 Mar 18 20:35 update_1.5.1.sql
-rw-r--r--  1 cbilz  staff  2245 Mar 12 11:56 update_1.5.2.sql
-rw-r--r--  1 cbilz  staff  7444 Mar 17 21:46 update_1.6.0.sql
-rw-r--r--  1 cbilz  staff  3270 Mar 25 12:06 update_1.6.1.sql
-rw-r--r--  1 cbilz  staff    96 Mar 25 17:34 update_1.6.2.sql
-rw-r--r--  1 cbilz  staff  3021 Apr  6 11:32 update_1.6.3.sql
-rw-r--r--@ 1 cbilz  staff    35 Jun 30 21:10 update_1.6.4.sql

cbilz@MacBook-Air-M2 datamodel % find ./ -name '*update*.sql' -exec grep -i -l amount_type {} \;
.//update_1.6.1.sql
.//update_1.6.0.sql
.//update_1.3.3.sql

The find command searches all the update files in the datamodel directory and grep the files found for the expression amount_type.
In a half of a second you know where the expression is contained without having to search the files manually.
This is just a very simple case to show the combination of find and grep.

But be prepared when your manager comes along with a task "... we have to analyze the logfiles until lunchtime, it's 11 am, and as I know there are more than 10.000 files in 500 subdirectories...."