linux - find using regexp and echo matches

07
2014-07
  • Luiz Gustavo F. Gama

    I have about 50 users in /home/ directory and I have cloned a git repository to everyone:

    Executed at /home/ as root user:

    find . -maxdepth 1 -type d ! -name . -prune -exec git clone /shared/repos/project_xpto.git {}/www/xpto/ \;
    

    Now I need to set owners to these cloned repositories.

    I want to execute chown user_folder_name:development -R ./user/www/xpto/ for each cloned repo.

    Then I started with:

    find . -maxdepth 1 -regextype sed -regex "./\([A-Za-z0-9-_]\).*" -type d -exec echo {}/ \;
    

    And I want to envolve to:

    find . -maxdepth 1 -regextype sed -regex "./\([A-Za-z0-9-_]\).*" -type d -exec chown ${expr1}:development {}/www/xpto/ -R
    

    I know ${expr1} does not exists. I just wanna know how to return my first matched regexp pattern, then I will get just username, without dots and slashes, from each folder to set owner.

  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    Related Question

    linux - How to get list of files that don't match patterns in bash?
  • Tamás Szelei

    I have a file with joker character patterns:

    ./include/*

    ./src/*

    etc.

    From the current directory I would like to recursively get the list of files that do not match these patterns.


  • Related Answers
  • Brian Vandenberg
    find . -type f \! \( -path '*/include/*' -o -path '*/src/*' \)
    

    Breakdown:

    • \! is negating the group
    • \( ... \) is how to do groups of conditions for find
    • -o ORs conditions
    • Everything else should be self-explanatory.

    If you have a new enough version of find, you could enhance it with:

    find . -type f -regextype posix-egrep -regex \! -path '.*/(include|src)/.*'
    
  • Seasoned Advice (cooking)

    First approach, but not using a list of files, others feel free to improve on that:

    find . -type f -print | grep -v '.\/src\/*'