Register
It is currently Sun Apr 20, 2014 2:57 pm

Directory structure


All times are UTC - 6 hours


Post new topic Reply to topic  [ 11 posts ] 
Author Message
 PostPosted: Wed Nov 26, 2008 7:46 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Hi guys,

I need to copy a bunch of files into a directory, but keeping the directory structure within the include directory.. This is what I have so far.
find . -name '*h' -print | xargs -i -t cp ./{} include/

Thanks,
Ed.


Top
 Profile  
 PostPosted: Tue Dec 02, 2008 6:37 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Well i think you need to create the dir's first then...

you could do something like this in a script i guess:
Code:
#!/bin/bash
path=$1
pattern=$2
to_path=$3

for i in $(find $path -iname $pattern); do
    current_dir=$(echo $i | sed -e "s/^\(.*\)\/[A-Za-z0-9.]*$/\1/")
    [ -d $current_dir ] || mkdir -p $current_dir
    cp $i $to_path/
done


This should create all directories and copy all files found in $path where $pattern matches
(ps. It's untested since I don't have anything to test it with :P ds.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Dec 02, 2008 8:34 am   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Hmmm.. interesting.

Thanks.


Top
 Profile  
 PostPosted: Tue Dec 09, 2008 7:27 am   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
fredrik.eriksson wrote:
Well i think you need to create the dir's first then...

you could do something like this in a script i guess:
Code:
#!/bin/bash
path=$1
pattern=$2
to_path=$3

for i in $(find $path -iname $pattern); do
    current_dir=$(echo $i | sed -e "s/^\(.*\)\/[A-Za-z0-9.]*$/\1/")
    [ -d $current_dir ] || mkdir -p $current_dir
    cp $i $to_path/
done


This should create all directories and copy all files found in $path where $pattern matches
(ps. It's untested since I don't have anything to test it with :P ds.

Best regards
Fredrik Eriksson


Hmm... I did have something to test it with :D... but I get all these errors...

mkdir: `/home/james/google-breakpad/google-breakpad-read-only/src/client/minidump_file_writer-inl.h' exists but is not a directory
mkdir: `/home/james/google-breakpad/google-breakpad-read-only/src/client/minidump_file_writer.h' exists but is not a directory
mkdir: `/home/james/google-breakpad/google-breakpad-read-only/src/http_upload/include/http_upload.h' exists but is not a directory

Anyway if you know how to fix it, I would appreciated.


Top
 Profile  
 PostPosted: Tue Dec 09, 2008 11:55 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
The sed fails for some reason. Can't say I really tested it before I wrote it :P
Something is probably wrong with the regular expression. Most likely it's just that the last set (inside []) doesn't match all types... from a quick look i believe it's missing "_" atleast.

The regexp now tells sed to match 0 or more characters that is either A to Z, a to z, 0 to 9 and "."
This should also match "_" since your files apperantly has that... also "-" might be a good plan :)

So something like this [A-Za-z0-9._-], it's still quite greedy thou so if you figure out some more special characters you should add them.
Just so you know, \ ( ) [ ] needs to be escaped so the shell doesn't interpret them as operators for your regexp.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Dec 09, 2008 12:11 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Well.. but where it is actually failing.. is to create the directories.. it's somehow thinking that the files are the directories..

mkdir: `/home/egutarra/google-breakpad/google-breakpad-read-only/src/common/mac/externals/google-toolbox-for-mac/Foundation/GTMNSString+Replace.h' exists but is not a directory

It actually puts all the files in the directory I specify.. but it doesn't create the directory structure.

GTMObjectSingleton.h string_utilities.h
GTMPath.h string_utils-inl.h
GTMProgressMonitorInputStream.h symbol_supplier.h
GTMRegex.h system_info.h
GTMScriptRunner.h types.h


Top
 Profile  
 PostPosted: Tue Dec 09, 2008 12:14 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Thinking of which I just thought of something easier.. could I do a recursive RM that could erase all files except those that are *.h.. That would be an easier way to achieve the same thing more easily


Top
 Profile  
 PostPosted: Tue Dec 09, 2008 3:43 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Yes that's what I just explained... since the sed isn't removing some specific filenames (those which contain +_- and so on) the mkdir is trying to make a directory named identical to the file it's itterating.


Top
 Profile  
 PostPosted: Wed Dec 10, 2008 2:19 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
true, why didn't i think of that :P

Code:
#!/bin/bash
source=$1
destination=$2
cp -R $source $destination
find $destination -type f -not -iname \*.h -exec rm {} \;


Something like that might do the trick actually... i would edit the -exec part before you test run it thou.
(As usual I don't have a linux machine to test it on atm.)

You could test running just: find $destination -type f -not -iname \*.h
and see if you get and .h files in that search

Best regards
Fredrik Eriksson

edit: ofcourse you need to change $destination to where you want to do your search when you're not running it as a script :P


Top
 Profile  
 PostPosted: Wed Dec 10, 2008 7:38 am   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Thanks Frederick.. worked like a charm. It's now saved under my cool scripts :)

Do you know how I can erase the directories that did not contain any *.h files?


Top
 Profile  
 PostPosted: Wed Dec 10, 2008 7:51 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
That'll probably take some more doing... but I can recommend the "-type d" syntax for find to get only directories.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP