Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-13-2012
Registered User
 
Join Date: May 2011
Posts: 24
Thanks: 6
Thanked 0 Times in 0 Posts
Copy files with same name but different extension from 2 different directory

Hi all,

i have 2 directory of files, the first directory(ext1directory) contain files of extension .ext1 and the second directory(allextdirectory) contains files of multiple extensions (.ext1,.ext2,.ext3,..)

so i want to copy the files from directory 2(allextdirectory) that have the same name as the files in directory 1(ext1directory) but only the files with different extension(ext2)

how this can be done in a shell script?

thank you.
Sponsored Links
    #2  
Old 06-13-2012
Registered User
 
Join Date: Dec 2008
Posts: 65
Thanks: 3
Thanked 9 Times in 9 Posts
How many extensions do you have?

Code:
cd allextdirectory
cp *.ext2 *.ext3 *.ext4 ext1directory

Alternative

Code:
for i in $(ls allextdirectory| grep -v ext1)
do
cp $i ext1directory
done


Last edited by Leion; 06-13-2012 at 05:01 AM.. Reason: Add on
Sponsored Links
    #3  
Old 06-13-2012
hergp's Avatar
hergp hergp is offline Forum Advisor  
Problem Eliminator
 
Join Date: Jan 2010
Location: Vienna, Austria
Posts: 621
Thanks: 10
Thanked 124 Times in 115 Posts
something like

Code:
cd $DIR2
for FILE in *
do
    [[ -f $DIR1/$FILE ]] || cp $FILE $DIR1
done

    #4  
Old 06-13-2012
Registered User
 
Join Date: May 2011
Posts: 24
Thanks: 6
Thanked 0 Times in 0 Posts
thank you,
in the first directory i have only 1 extension (ext1)

and the second directory i have 3 extensions(ext1,ext2,ext3)
@Leion the copy should be made based on the files that have the same name and not everything .. is what you wrote will do the trick ??

---------- Post updated at 04:06 AM ---------- Previous update was at 03:59 AM ----------

Quote:
Originally Posted by Leion View Post
How many extensions do you have?

Code:
cd allextdirectory
cp *.ext2 *.ext3 *.ext4 ext1directory

Alternative

Code:
for i in $(ls allextdirectory| grep -v ext1)
do
cp $i ext1directory
done

can you please tell me where in this code you copy based on the same file name in the 2 directories?? because i did not understand it ..

---------- Post updated at 04:07 AM ---------- Previous update was at 04:06 AM ----------

Quote:
Originally Posted by hergp View Post
something like

Code:
cd $DIR2
for FILE in *
do
    [[ -f $DIR1/$FILE ]] || cp $FILE $DIR1
done

can please also tell me where this will copy based on equal file names in both directories??
Sponsored Links
    #5  
Old 06-13-2012
Registered User
 
Join Date: Dec 2008
Posts: 65
Thanks: 3
Thanked 9 Times in 9 Posts
I did not read the qn fully..
try the code below.. remove the echo once verified this is what you need..


Code:
for i in $(ls ext1directory)
do
ii=$(i%ext1)
   for j in $(ls allextdirectory/${ii}* | grep -v ${i})
   do
   echo cp $j ext1directory
   done
done


Last edited by Leion; 06-13-2012 at 05:10 AM.. Reason: corrected directory
Sponsored Links
    #6  
Old 06-13-2012
Registered User
 
Join Date: May 2011
Posts: 24
Thanks: 6
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Leion View Post
I did not read the qn fully..
try the code below.. remove the echo once verified this is what you need..


Code:
for i in $(ls ext1directory)
do
ii=$(i%ext1)
   for j in $(ls allextdirectory/${ii}* | grep -v ${i})
   do
   echo cp $j ext1directory
   done
done

this is copying everything from the allextdirectory, not only the equal names.. besides i want to copy only the files that have same names and ext2. i don't want to copy other extensions (even if the files have exact names)
Sponsored Links
    #7  
Old 06-13-2012
Registered User
 
Join Date: Dec 2008
Posts: 65
Thanks: 3
Thanked 9 Times in 9 Posts
Quote:
Originally Posted by shelladdict View Post
this is copying everything from the allextdirectory, not only the equal names.. besides i want to copy only the files that have same names and ext2. i don't want to copy other extensions (even if the files have exact names)
no.. the ii variable will get the filename without extension. and the grep -v part will filter away the original filename before copy..

---------- Post updated at 05:26 PM ---------- Previous update was at 05:23 PM ----------

Just copy ext2 and no other extensions.

Code:
for i in $(ls ext1directory)
do
ii=$(i%ext1)
cp allextdirectory/${ii}ext2 ext1directory
done

Sponsored Links
Closed Thread

Tags
directories

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
copy all files matching the request and change the extension at the same time vacuity93 UNIX for Dummies Questions & Answers 16 12-24-2011 04:10 AM
copy files with new extension in same directory dheian Shell Programming and Scripting 7 05-25-2010 05:35 AM
Checking if the files in a directory have a txt extension pantelis Shell Programming and Scripting 15 05-06-2010 03:08 PM
Copy files from a directory by ftp hippa77 UNIX for Dummies Questions & Answers 3 10-06-2007 02:26 PM
copy files from one directory to another directory zip_zip UNIX for Dummies Questions & Answers 5 09-14-2003 06:16 PM



All times are GMT -4. The time now is 08:09 AM.