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 23 Nov 2014, 03:41
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 [4 Posts]  
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: 2715
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

_________________
Dpup 487 | Puppy Gallery | My photo gallery | mtPaint works
Back to top
View user's profile Send private message Visit poster's website MSN Messenger 
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: 2715
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;

_________________
Dpup 487 | Puppy Gallery | My photo gallery | mtPaint works
Back to top
View user's profile Send private message Visit poster's website MSN Messenger 
Display posts from previous:   Sort by:   
Page 1 of 1 [4 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


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