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

  • SEARCH
  • Home
  • 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 6072223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:08:10+00:00 2026-05-23T10:08:10+00:00

I’m writing a bash script which needs to, for one step, get a list

  • 0

I’m writing a bash script which needs to, for one step, get a list of directories (variable) in a target directory (which may also contain files), and then expand them out as parameters to a python script.

Example:

/stuff/a dir/
/stuff/b other/
/stuff/c/

And I need to, within a bash script, call:

script.py "a dir/" "b other/" "c/"

or alternately, escaped spaces:

script.py a\ dir/ b\ other/ c/

I need the script to be called exactly once for directory ‘stuff’.

Is there a straightforward way to do this kind of thing? I’ve been googling around and the best I’ve managed to figure out requires me to know how many directories there are.

  • 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-23T10:08:11+00:00Added an answer on May 23, 2026 at 10:08 am

    This is a job for find.

    find /stuff -type d -exec script.py {} +
    

    When you use -exec the curly braces {} are replaced with the names of the matching files, and + indicates the end of the command (in case you want to tell find to take additional actions). This is the ideal way to execute a command using find as it will handle file names with unusual characters (such as whitespace) correctly.

    find is quite flexible, especially if you have the GNU version typically bundled with Linux distros.

    # Don't recurse into subdirectories.
    find /stuff -maxdepth 1 -type d -exec script.py {} +
    
    # Pass in a/, b/, c/ instead of /stuff/a/, /stuff/b/, /stuff/c/.
    find /stuff -type d -printf '%P\0' | xargs -0 script.py
    

    In the second example notice the careful use of \0 and xargs -0 to use the NUL character to delimit file names. It might seem odd but this allows the command to work even if you do something really weird like use newlines \n in your directory names.


    Alternatively, you could do this using only shell builtins. I don’t recommend this, but for educational value, here’s how:

    # Start with an empty array.
    DIRS=()
    
    # For each file in /stuff/...
    for FILE in /stuff/*; do
        # If the file is a directory add it to the array. ("&&" is shorthand for
        # if/then.)
        [[ -d $FILE ]] && DIRS+=("$FILE")
    
        # (Normally variable expansions should have double quotes to preserve
        # whitespace; thanks to bash magic we don't them inside double brackets.
        # [[ ]] has special parsing rules.)
    done
    
    # Pass directories to script. The `"${array[@]}"` syntax is an unfortunately
    # verbose way of expanding an array into separate strings. The double quotes
    # and the `[@]` ensure that whitespace is preserved correctly.
    script.py "${DIRS[@]}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have a text area in my form which accepts all possible characters from
I am writing an app with both english and french support. The app requests
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.