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 3671150
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:27:06+00:00 2026-05-19T02:27:06+00:00

I have a loop in my script, which iterates over an array – myArray

  • 0

I have a loop in my script, which iterates over an array – myArray and I need to do some copying of files/directories on each iteration. The array can be like this –

myArray=('ajax' 'style/prod_styles' `path/to/some_file.php` 'templates' 'uploadify')

Taking this array, for elements without a /, I need to copy the entire folders and their contents – e.g. ajax, templates, uploadify. But for those having slashes – like style/prod_styles (Note that there can be multiple slashes), I need to copy only the last element (e.g. for /path/to/some/folder I need to copy only folder and its contents) and in case the parent folders are not existing in the destination (e.g. path, to, some are the parent folders), I need to just create those folders and then copy the last element (folder).

Earlier I guessed it would be easy to do an explode (like PHP’s explode()) with / inside the loop and then recursively start from the parent (path directory as per the above example) check if its child directory exists, if not create it, till we are done with the parent of the file/directory to be copied, then do the final copy.

If, however there is something simpler possible in bash to do this, please let me know.

Thanks,
Sandeepan

  • 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-19T02:27:06+00:00Added an answer on May 19, 2026 at 2:27 am

    Unless I misunderstood your question, you probably don’t need to use an explode-like facility to achieve what you want.

    Example:

    # dir to copy to    
    DESTINATION='/path/to/copy/to/.'
    
    # dir to copy from
    SOURCE='.' 
    
    # list of dirs to copy
    myArray=('ajax' 'style/prod_styles' 'templates' 'uploadify')
    
    # for each directory in myArray ...
    for d in "${myArray[@]}"
    do
        if [ -f "$d" ]; then # it this is a regular file
    
            # create base directory
            mkdir -p $DESTINATION/$(dirname "$d")
    
            # copy the file
            cp "$SOURCE/$d" $DESTINATION/$(dirname "$d")
    
        elif [ -d "$d" ]; then # it is a directory
    
            # create directory (including parent) if it doesn't exist
            # - this does nothing if directory exists
            mkdir -p "$DESTINATION/$d"
    
            # recursive copy
            cp -r "$SOURCE/$d/"* "$DESTINATION/$d/."
    
        else 
    
            # write warning to stderr. do nothing with this entry
            echo "WARNING: invalid entry $d." >&2
    
        fi
    done
    

    Update:

    Updated sample code to maintains relative path within destination. So styles/prod_styles will be copied to $DESTINATION/styles/prod_styles but all other stuff within styles/ are not copied.

    Note that some additional checks need to be added if one cannot be sure that:

    • path specified in DESTINATION exists
    • not all values in myArray are valid directories (not files)

    Update 2:

    Example code updated to handle regular files as well as directory.

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

Sidebar

Related Questions

I have made an each statement which iterates through each class item found. At
I have a for loop in action script which I'm trying to use to
I have a long running php script which is basically an infinite loop listening
i have a input tag which is non editable, but some times i need
I have two SQL scripts which get called within a loop that accept a
I have a loop that reads each line in a file using getline() :
If I have a for loop which is nested within another, how can I
If you have a for loop such as: For Each s As String In
In Java 5 and above you have the foreach loop, which works magically on
I am using PHP and I need to script something like below: I have

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.