|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
something like Code:
cd $DIR2
for FILE in *
do
[[ -f $DIR1/$FILE ]] || cp $FILE $DIR1
done |
|
#4
|
|||
|
|||
|
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:
---------- Post updated at 04:07 AM ---------- Previous update was at 04:06 AM ---------- can please also tell me where this will copy based on equal file names in both directories?? |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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
doneLast edited by Leion; 06-13-2012 at 05:10 AM.. Reason: corrected directory |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
---------- 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 | ||
|
![]() |
| Tags |
| directories |
| Thread Tools | Search this Thread |
| 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 |
|
|