2

Considering the following directory structure:

dir1
  [domain.com] foo dir1
    subdir1
  subdir2
    FooFile
dir2
  [domain.com] foo dir2
  subdir3
    [domain.com] foo dir3
      subdir4
      subdir5
    subdir6
  subdir7
[domain.com] foo dir4
  subdir8

I'd like to list all dirs with names that contain [domain.com]. Desired output:

[domain.com] foo dir1
[domain.com] foo dir2
[domain.com] foo dir3
[domain.com] foo dir4

I'm having a hard time putting this together. The closest I have come:

find . -type d -regextype posix-egrep -regex ".*/(\[domain\.com\]).+" -exec basename {} \;

But this outputs subdirectories as well:

[domain.com] foo dir3
subdir5
subdir4
[domain.com] foo dir2
[domain.com] foo dir4
subdir8
[domain.com] foo dir1
subdir1
3

Since it looks like you have GNU find, this should be enough:

find . -type d -name '\[domain.com\]*' -printf '%f\n'

GNU find's -printf action can print just the basename of the matched file with %f.

The problem with your regex is that it allows for anything after [domain.com] - even a /, and hence matches subdirectories. ".*/(\[domain\.com\])[^/]+" might fix that issue.

  • Both solutions work for me. Thank you for the insights and clear explanation. – Daktari Apr 3 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.