I have some Laravel/Livewire files matching filename patterns like seen in below:

app/Livewire/String1Table.php
app/Livewire/String1s.php
app/Livewire/EditString1.php

app/Models/String2Table.php
app/Models/SubDir/SomeString2.php

resources/views/livewire/string1-table.blade.php
resources/views/livewire/string1s.blade.php
resources/views/livewire/edit-string1.blade.php

Now I want to git add only those files that contain String1 or string1 (apparently disregarding the case) and only those that are within the [L|l]ivewire folder (disregarding the case here too).

I know that git add supports the asterisk and some of regex, but does it support all the bash related pattern matches ? Any ideas on how can I achieve what I want? Hope the requirement is clear.

From the first 2 informative comments, I am wondering if this would be correct: git add *[Ll]ivewire/*String1[?s] ? I am not so well-versed with regex that much, that's why I wanna know if anyone can come up with a way or partial way. I don't wanna end up adding files incorrectly.

2

There are 2 best solutions below

1
Brian61354270 On BEST ANSWER

You can use find to locate the desired files:

$ find . -path '*/[Ll]ivewire/*' -name '*[Ss]tring1*'
./app/Livewire/String1s.php
./app/Livewire/EditString1.php
./app/Livewire/String1Table.php
./resources/views/livewire/string1-table.blade.php
./resources/views/livewire/edit-string1.blade.php
./resources/views/livewire/string1s.blade.php

Once you have a find command that matches the desired files, you can run git add on each file by passing an -exec option:

$ find . -path '*/[Ll]ivewire/*' -name '*[Ss]tring1*' -exec git add '{}' +
$ git status -s
A  app/Livewire/EditString1.php
A  app/Livewire/String1Table.php
A  app/Livewire/String1s.php
A  resources/views/livewire/edit-string1.blade.php
A  resources/views/livewire/string1-table.blade.php
A  resources/views/livewire/string1s.blade.php
?? app/Models/
0
pjh On

If you are running Bash 4.3 or later then this should work:

shopt -s dotglob globstar
git add **/[Ll]ivewire/*[Ss]tring1*.php
  • shopt -s ... enables some Bash settings that are required by the command:
    • dotglob enables globs to match files and directories that begin with .. find processes such files by default.
    • globstar enables the use of ** to match paths recursively through directory trees. This option was introduced with Bash 4.0 (released in 2009) but it is dangerous to use in versions before 4.3 (released in 2014) because it follows symlinks. See the globstar section in glob - Greg's Wiki.