Copy files with different extensions and same name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files with different extensions and same name
# 1  
Old 09-15-2015
Copy files with different extensions and same name

Hi,

i have two folders. Folder A has 1000+ files with just images named like :

Code:
01012015.png
01022015.png

etc. Folder B has much more files, part of them with same name as png files folder others not. Instead of folder A wich are only images, these are audio wav files.

I need to copy the second folder with just the files that have the same name, but dfferent extension. from example if in folder A i have 01012015.png and in folder B i have 01012015.wav , just to copy the second files inside the A folder , doing it manually will be too long to do it.

Any help will be welcome.

Regards
# 2  
Old 09-15-2015
Run this on your command line with appropriate modifications:

Code:
for f in /path/to/folderA; do filename=$(echo $f | sed -e 's/.*\///' -e 's/\..*//'); cp /path/to/folderB/${filename}.wav /path/to/folderA/; done

# 3  
Old 09-15-2015
What have you got so far? You could:-
  • List all the files less the .png in folder A into a file, say /tmp/folderA.lst
  • List all the files less the .wav in folder B into a file, say /tmp/folderB.lst
To list file numbers in folder A that are also in folder B, use:-
Code:
grep -f /tmp/folderA.lst /tmp/folderB.lst

To list file numbers in folder A that are not in folder B, use:-
Code:
grep -vf /tmp/folderA.lst /tmp/folderB.lst

THat would give the the appropriate list, and you can then do whatever is required from that.

Does that get you started?


Robin
# 4  
Old 09-16-2015
Hello,

thank you for your time. I've checked again and the names are different. In folder A I have 20150730_091113.wav-image.png , in the other folder I have 20150730_091113.wav . I've tried with your method, using *.lst files but they didn't match using grep -f . Maybe I should even all names or is there another solution?

Thanks in advance

regards

Last edited by rbatte1; 09-16-2015 at 06:12 AM.. Reason: Added ICODE tags for filenames and commands
# 5  
Old 09-16-2015
How did you generate the two files? You would need to be in the correct directory when you run the list command.

Can you paste a sample of the two files generated?



Robin
# 6  
Old 09-16-2015
Hello,

first i've renamed the images with this code :

Code:
for f in *.wav-image.png;do
mv -- "$f" "${f%.wav-image.png}.png"
done

so now i've got two folders . Folder A has png files with this extensions :

Code:
20150811_063330.png

Folder B has wav files with this extension :
Code:
20150811_063330.wav

i've tried your method creating .lst files using this command :

Code:
cat imagesdir/*  > listA.lst

and

Code:
cat wavsdir/*  > listB.lst


then

Code:
 grep -f listA.lst listB.lst


and it ends with a :

Code:
grep: Unmatched [ or [^

regards
# 7  
Old 09-16-2015
You would be better to create the files like this:-
Code:
cd imagesdir
ls | cut -f1 -d "." > ../listA.lst            # Important not to write to the current directory
cd ..

cd wavsdir
ls | cut -f1 -d "." > ../listB.lst            # Important not to write to the current directory
cd ..

What you have done is to write the contents of all the files into listA.lst & listB.lst respectively rather than the list of filenames.

The above approach has the cut in place to trim off the end of each file name, so you should end up with two files that list what would be the common parts of the names that you are looking to compare. This assumes that there are no files with a dot in the part you want to compare.

Using the grep commands would then list the common (or missing) files that you are interested in and you can build your required commands from in a loop, obviously adding back on the .png or .wav as appropriate.


Does that help?

Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mv all files with different extensions to a new name

Hello all! I want to move several files foo.aux foo.log foo.pdf foo.tex to bar_foo.aux bar_foo.pdf bar_foo.tex I am on tcsh % mv foo.* bar_!#:1 is not working. Thank you for your help marek (11 Replies)
Discussion started by: marek
11 Replies

2. Shell Programming and Scripting

Find duplicate files but with different extensions

Hi ! I wonder if anyone can help on this : I have a directory: /xyz that has the following files: chsLog.107.20130603.gz chsLog.115.20130603 chsLog.111.20130603.gz chsLog.107.20130603 chsLog.115.20130603.gz As you ca see there are two files that are the same but only with a minor... (10 Replies)
Discussion started by: fretagi
10 Replies

3. Shell Programming and Scripting

looping through files with different extensions

Hi all, I am trying to make a for loop invoking files with different extensions (*.ugrd and *.vgrd) and I cant just make it work. Cant figure out how to load the files so as to use them in subsequent commands like the ones in this pseudo code. the files are arranged such that in one date for... (8 Replies)
Discussion started by: ida1215
8 Replies

4. Shell Programming and Scripting

Searching for files with specific extensions

Hi, Could someone give me a hand with a search for files with two possible extensions, please. The requirement is simple - I need to issue a single ls command searching for files with the suffix of, say, *.txt and *.log. I've tried to use ls *.txt *.log which works if there are both... (4 Replies)
Discussion started by: neilmw
4 Replies

5. Shell Programming and Scripting

Rename files extensions

Hello ! i have a few files like ... setup.001 setup.002 setup.003 setup.004 // to setup.095 and i would like to rename those files to ... setup.r01 setup.r02 setup.r03 setup.r04 // to setup.r95 (7 Replies)
Discussion started by: Putazo
7 Replies

6. UNIX for Dummies Questions & Answers

changing extensions for multiple files at once

I copied some files to another folder, and I want to change them from .doc extensions to .txt extensions. I tried using the cp and mv commands, but it didn't work. Is it possible to change file extensions with these commands, and if so how do I do it? I tried using the * wildcard (say cp *.doc... (1 Reply)
Discussion started by: Straitsfan
1 Replies

7. Shell Programming and Scripting

Sorting Files according to their Extensions...

I am trying to write a Korne Shell Script wherein we have to sort files according to their extensions(for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and 7.ksh, 2.ksh, 59.ksh together) and move them to their respective directories viz. sh, csh and ksh... I think,... (1 Reply)
Discussion started by: marconi
1 Replies

8. Shell Programming and Scripting

Find files with 3 different extensions

Hi all, From one directory I need to fetch only files of type *.xls,*.csv,*.txt. I tried the find . -name '*.txt,*.csv,*.xls' -print. But it throws me error. Please do help me on this. Thanks Mahalakshmi.A (11 Replies)
Discussion started by: mahalakshmi
11 Replies

9. UNIX for Dummies Questions & Answers

List Files by Extensions

I have a unix directory with 500 plus files . When I do a ls -lR I can see ALL the files here . How can I sort this by the files extensions ? I can't enter ls -lR *.ext1 *.ext2 *.ext3 etc in case I miss out some files . (2 Replies)
Discussion started by: newbienix
2 Replies

10. UNIX for Dummies Questions & Answers

Command to select files with different extensions

I want to select files which have different extensions such as .cpp, .cs, .h I can select one of them as find . -name "*.cpp" but I want to select all of them in one command only. It should be pretty simple but I'm not able to get it. Any help with the command will be greatly appreciated. (1 Reply)
Discussion started by: MobileUser
1 Replies
Login or Register to Ask a Question