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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:45:11+00:00 2026-05-28T03:45:11+00:00

I have a directory structure in my machine as following bhagwat durga Sai Baba

  • 0

I have a directory structure in my machine as following

 bhagwat        durga    Sai Baba    vishnu sahastranam
  bhagwat geeta  hanuman  ramayan audio  shiv      vishnu

in this all of the above are directories which have blank spaces in their names.
i.e if you do an ls -l you will see following

drwx------ 1 deel deel        0 2011-09-12 21:34 bhagwat
drwx------ 1 deel deel     8192 2011-09-09 22:35 bhagwat geeta
drwx------ 1 deel deel     4096 2011-10-07 05:10 durga
drwx------ 1 deel deel        0 2011-10-29 09:23 hanuman
drwx------ 1 deel deel     8192 2011-09-30 22:48 ramayan audio
drwx------ 1 deel deel     4096 2011-09-18 12:16 Sai Baba
drwx------ 1 deel deel     4096 2011-09-26 19:19 shiv
drwx------ 1 deel deel     4096 2011-09-26 19:20 vishnu
drwx------ 1 deel deel     4096 2011-09-16 11:35 vishnu sahastranam

which has further sub folders in it.I DO NOT want to copy the files or directories.
I want to create a directory structure as above on some other location or device.Which is analogous to what exist on source directory.
Hence I am writing a script for the same as follows

#!/bin/bash
for i in `ls /media/New\ Volume/bhajans`;do
echo $i 
done

Now the problem with this approach is it results in following output

bhagwat
bhagwat
geeta
durga
hanuman
ramayan
audio
Sai
Baba
shiv
vishnu
vishnu
sahastranam

if you notice the output the blank space in a directory name are treated as separate directory.Which is not correct.So how can I get rid of this problem.I want to create same directory structure as present on source folder.

EDIT
I am using Ubuntu 11.04
UPDATE
Each of the sub directory which was cloned has a script named script.sh so not only I am cloning the directory structure.I am also copying the script.sh from parent directories to the cloned destinations.
Kevin and Jonathan thanks for your explanations and answers.

  • 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-28T03:45:12+00:00Added an answer on May 28, 2026 at 3:45 am

    To clone the directory structure (recursively) from olddir to newdir:

    find olddir -type d -printf "newdir/%P\0" | xargs -0 mkdir -p
    

    If you want only the first level, add -maxdepth 1 to the find.

    Explanation:

    find, of course, recursively searches a directory and acts on the files it finds. olddir tells it to search in the directory named “olddir.” -type d specifies that it should only act on directories. -printf tells it to print the pattern that follows: newdir is the directory into which you want to clone the structure. %P prints the file name without the olddir part, and \0 tells it to finish the name with a null character. This null character will allow us to pass any legal file name to xargs. xargs executes the command you give it using what it reads from stdin as arguments. -0 tells it to use the null byte (\0) to determine where the arguments (file names, in our case) should be separated. Otherwise it would use white space, and we know we don’t want it to do that. -p tells mkdir to make the parent directories if they don’t already exist.

    OK, copying each file over is a bit trickier, but I’m pretty sure this will work in all cases (all cases where a compatible [gnu] find is installed, at least) after you have cloned the structure with the command above:

    $ find olddir -name script.sh -printf "%p\0" -printf "newdir/%P\0" | xargs -0L2 cp -n
    

    The first printf prints the source (existing) file (%p), the second prints the destination file. Both end in \0 as we did earlier. The new argument to xargs, -L2 tells xargs to take two (2) arguments (files) at a time from its input and make each command run with just the two of them. So the order of the printf statements and the L2 are quite important. The -n tells cp not to overwrite the destination if it exists, just in case one of us made a mistake.

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

Sidebar

Related Questions

I have the following directory structure: Main.py A/ __init__.py B/ __init__.py C/ __init__.py The
I have the following directory structure: schema1 schema1.xsd schema1.xsdconfig schema2 schema2.xsd schema2.xsdconfig schema3 schema3.xsd
I have the following directory structure of a project: Folder project in Eclipse: --folder
Suppose I have the following directory structure: workspace/ __init__.py ys_manage/ __init__.py manage.py ys_utils/ __init__.py
My development machine is a linux host. I have a complicated directory structure (like
I have directory structure like so: |-- ROOT |-- Project1 | |-- application |
I have a directory structure that looks like this: /a/f.xml /b/f.xml /c/f.xml /d/f.xml What
I have a directory structure of directories and files that I want to render
I have a directory structure as below: / | +--- /lib | libsomething.a |
I have a directory structure as follows Client_Site/ some_folder1/ some_folder2/ some_folder3/ Lots of files

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.