Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 26 Oct 2014, 00:06
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Merge directories in bash?
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
ITAmember

Joined: 13 Sep 2009
Posts: 168
Location: The middle of a cornfield

PostPosted: Sat 14 Nov 2009, 18:34    Post_subject:  Merge directories in bash?  

It's not as easy as cp -r, some of the directories already exist and it won't let me merge them. I currently have this

Code:
# ${1} is source, ${2} is dest
copydir () {
   for FILE in `ls ${1}`;
   do
      OUTDIR=${2}/${FILE}
      FILE=`echo ${1}/${FILE}`
      
      cp -rf ${FILE} ${OUTDIR}
      
      if [ -d  ${FILE} ] ;
      then
         copydir ${FILE} ${OUTDIR}
      fi
   done;
}


but it goes into a infinite recursion. Any tips?
Back to top
View user's profile Send_private_message 
dejan555


Joined: 30 Nov 2008
Posts: 2691
Location: Montenegro

PostPosted: Sat 14 Nov 2009, 18:58    Post_subject:  

You want to merge all files from one directory to another?
Just use same method used for installing sfs's (and pets probably):
Let's say you have 2 directories dir1 and dir2:
Code:

cd dir1
cp -a * ../dir2


That easy, and if you don't want to overwrite already existing files in dir2 you can use:

Code:

cd dir1
yes n | cp -ai * ../dir2

_________________


Back to top
View user's profile Send_private_message Visit_website MSNM 
ITAmember

Joined: 13 Sep 2009
Posts: 168
Location: The middle of a cornfield

PostPosted: Sat 14 Nov 2009, 19:07    Post_subject:  

I wish cp --help would've told me that... Thanks. Smile
Back to top
View user's profile Send_private_message 
dejan555


Joined: 30 Nov 2008
Posts: 2691
Location: Montenegro

PostPosted: Sat 14 Nov 2009, 19:16    Post_subject:  

Now, let's say you have multiple directories to merge, like 5 of them for example, I'd make this kind of dir structure:

./parentdir:
subdir1
subdir2

./parentdir/subdir1:
dir1
dir2
dir3
dir4
dir5

./parentdir/subdir2:

Now, to copy all files from dir1-dir5 to subdir2 and then erase dirs I use this command:

Code:
cd parentdir/subdir1
for i in *; do cd $i; yes n | cp -ai * ../../subdir2/; cd ..; rm -rf $i; done;

_________________


Back to top
View user's profile Send_private_message Visit_website MSNM 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0434s ][ Queries: 12 (0.0035s) ][ GZIP on ]