Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7634239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:07:11+00:00 2026-05-31T07:07:11+00:00

In a bash script I would like to parse the names of all subdirectories

  • 0

In a bash script I would like to parse the names of all subdirectories and find all subdirectories that have a matching string, but I do not want subdirectories of a match. I am interested in automating construction of my $PATH and $PYTHONPATH variables based on directory structure.

Here’s an example:
Let’s say I want to go through my ~/dev and ~/bin folders and find all subdirectories with bin/ which holds programs that I will want to run at the shell. I can get a list with

$ ls -lR $HOME/bin $HOME/dev |grep "\/"  | grep "bin:"
/Users/dat5h/bin:
/Users/dat5h/bin/project/bin:
...

These can all be appended to $PATH and have all available scripts ready to run.

BUT, let’s say I was searching for directories with python modules and packages to add to $PYTHONPATH. I could conceivably look for all directories that start with /py-. So, I try:

$ ls -lR $HOME/bin $HOME/dev |grep "\/"  | grep "/py-" 
/Users/dat5h/bin/py-test:
/Users/dat5h/bin/py-test/test-package:
/Users/dat5h/bin/py-test/test-package/nested-test:
...

My thinking is that I would not want to put package directories and subdirectories into the path. I’m pretty sure that would be strange, but I am actually new to python so suggestions would be helpful. How would I go about constructing a test case to only get directories with py-* but non of the subsequent subdirectories?

I tried:

$ ls -lR $HOME/bin $HOME/dev |grep "\/"  | egrep "/py-.*[^/]:"

But this doesn’t get the job done either. Maybe a better regex? Any help would be greatly appreciated!


SOLUTION

The solution I ended up satisfied with was the find suggested below with a cutoms regex:

find $HOME/bin $HOME/dev -type d -regex ".*\/py\(\w\|-\w\)*"

This will find all subdirectories of ~/bin and ~/dev that are some variant of “pySOMETHING”, “py-SOMETHING”, “pySOME_THING_ELSE”, or “py-SOME_THING_ELSE” but does not grab any subdirectories of those unless they also match this string. This ensures that I can have some simple naming convention for all of my directories with python modules/packages and import them this way without accidentally being able to import nested packages without the hierarchy.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:07:12+00:00Added an answer on May 31, 2026 at 7:07 am

    Does this:

    find -type d  -regex ".*py.*bin.*" 
    

    give you some start?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to do following: I have a bash script that calls an
I would like to reference a variable that I have in a bash script
I need to write a bash script , and would like it to parse
I have a Bash shell script in which I would like to pause execution
I want to make a bash script that works like a makefile. It would
I am learning bash. I would like to do a simple script that, when
Inside my bash script, I would like to parse zero, one or two parameters
I would like to write a bash script or a few shell commands that
In a bash script, I would like to put the following code that assigns
I would love some help with a Bash script loop that will show all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.