Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copying a directory structure via shell scripts

Tags:

regex

bash

shell

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.

like image 783
Registered User Avatar asked Aug 15 '26 14:08

Registered User


2 Answers

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.

like image 198
Kevin Avatar answered Aug 18 '26 10:08

Kevin


Try

find . -maxdepth 1 -type d | xargs -I X mkdir '/new/directory/X'

Sorry for the initial mistake.

like image 40
Ed Heal Avatar answered Aug 18 '26 12:08

Ed Heal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!