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.
To clone the directory structure (recursively) from
olddirtonewdir:If you want only the first level, add
-maxdepth 1to thefind.Explanation:
find, of course, recursively searches a directory and acts on the files it finds.olddirtells it to search in the directory named “olddir.”-type dspecifies that it should only act on directories.-printftells it to print the pattern that follows:newdiris the directory into which you want to clone the structure.%Pprints the file name without theolddirpart, and\0tells it to finish the name with a null character. This null character will allow us to pass any legal file name toxargs.xargsexecutes the command you give it using what it reads from stdin as arguments.-0tells 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.-ptellsmkdirto 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:
The first
printfprints the source (existing) file (%p), the second prints the destination file. Both end in\0as we did earlier. The new argument toxargs,-L2tellsxargsto 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 theprintfstatements and theL2are quite important. The-ntellscpnot to overwrite the destination if it exists, just in case one of us made a mistake.