LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find and copy in the same directory (https://www.linuxquestions.org/questions/linux-newbie-8/find-and-copy-in-the-same-directory-829751/)

greenpoise 09-01-2010 10:18 AM

Find and copy in the same directory
 
I installed a new music application. It reads covers.jpg as the cover of the album, however, my covers files where named album. I dont want to rename, I want to make a copy of album.jpg and if possible as well rename it to covers.jpg. The file has to be in the same folder that it currently is. I have looked around to see how I can do this but have not been able to.


thanks

Udi 09-01-2010 10:24 AM

You can use the file browser to create a copy of the file by Ctrl+C Ctrl+V, then rename the new file to cover.jpg.

On the command line, you can copy album.jpg to cover.jpg with this command:

cp album.jpg cover.jpg

Hope it helps :)

greenpoise 09-01-2010 10:27 AM

thanks for the reply. I am looking for a more automated way of doing it. Using the find exec cp commands would prob do it, I just dont know the complete command for it.

rikijpn 09-01-2010 10:48 AM

I'm assuming you mean you have all those directories together (in the same root/parent dir), as in:
Code:

music/album1
music/album2
...
music/albumN

And want to change the paths like this:
Code:

album1/album.jpg  --> album1/covers.jpg
If this is the case, you can just use a "for" (in bash) sentence to copy every dir's album.jpg file to the same dir's covers.jpg file, like this:
Code:


#cd music
for dir in * ; do cp $dir/album.jpg $dir/covers.jpg ; done

It doesn't check for files in the dirs, and takes all the files in the current dir, so it will return errors if you have a path like "music/file.txt", but it won't delete anything^^.

tracertong 09-01-2010 11:23 AM

Or better still, wildcard it, as in (to use the example directory structure given above):

#cd music
for dir in album* ; do cp $dir/album.jpg $dir/covers.jpg ; done;

This stops it spidering every single subdirectory: only those that match the pattern 'album-something'.

greenpoise 09-01-2010 12:00 PM

It wants to work but it doesnt.

My structure is something like this:

/home/dan/Music/albumA
/home/dan/Music/albumB
/home/dan/Music/albumB/CD1
/home/dan/Music/albumB/CD2
/home/dan/Music/albumC


This is a what is like. It is giving me this errors:

cp: target `Project/cover.jpg' is not a directory
cp: target `(CD2)/cover.jpg' is not a directory
cp: target `CD2/cover.jpg' is not a directory
cp: target `Vavavoom/cover.jpg' is not a directory
cp: target `Contraband/cover.jpg' is not a directory

Thanks alot for the help!

crts 09-01-2010 12:12 PM

Quote:

Originally Posted by greenpoise (Post 4084936)
I installed a new music application. It reads covers.jpg as the cover of the album, however, my covers files where named album. I dont want to rename, I want to make a copy of album.jpg and if possible as well rename it to covers.jpg. The file has to be in the same folder that it currently is. I have looked around to see how I can do this but have not been able to.


thanks

Hi,

is this what you are looking for?
Code:

find /path/to/dir -type f -name album.jpg -execdir cp album.jpg covers.jpg \;

rikijpn 09-01-2010 12:19 PM

It would be great if you could return the output of a quick "ls" too^^.
I can only guess, you have spaces in your directory names. Also having your locale in another language (do "locale" to check?) may have something to do.
Anyway, if it's only that you have spaces in the dir names, make a script (save this to text to "script.sh" or something) with the following:
Code:

IFS='
'

for dir in * ; do cp $dir/album.jpg $dir/covers.jpg ; done

This sets the internal field separator (IFS) only to newlines (return key), not including spaces as default. I think the parenthesis shouldn't be a problem...

Wow, crts's code is better... find is amazing.

greenpoise 09-01-2010 12:28 PM

Quote:

Originally Posted by rikijpn (Post 4085061)
It would be great if you could return the output of a quick "ls" too^^.
I can only guess, you have spaces in your directory names. Also having your locale in another language (do "locale" to check?) may have something to do.
Anyway, if it's only that you have spaces in the dir names, make a script (save this to text to "script.sh" or something) with the following:
Code:

IFS='
'

for dir in * ; do cp $dir/album.jpg $dir/covers.jpg ; done

This sets the internal field separator (IFS) only to newlines (return key), not including spaces as default. I think the parenthesis shouldn't be a problem...

Wow, crts's code is better... find is amazing.


Bingo!! that was it, spaces between . Thanks alot! very helpful. No way I could have come up with this.


Thanks again all of you

greenpoise 09-01-2010 12:29 PM

wait a sec. it did not do all of them though.

crts 09-01-2010 12:41 PM

Quote:

Originally Posted by greenpoise (Post 4085077)
wait a sec. it did not do all of them though.

what was skipped? Show an example that was omitted and then we'll see how to fix it.

Have you tried the 'find' from post #7?

rikijpn 09-01-2010 12:46 PM

ahaha^^ that's too bad. You should really be a little more specific. Like instead of saying it didn't get all of them, telling which ones it didn't get, or even better, do an "find" in that dir and paste the output here, etc. It's kind of hard keeping guessing^^.

I can only guess, you maybe have some subdirectories in the same directory structure, so the code I gave you wouldn't work right (being that is only reading the files/dirs in the current directory). In that case the script would become kinda long/complicated/plain ugly.

crts's code is most likely to work in such a case too, did you try it?

greenpoise 09-01-2010 01:01 PM

post 7 throws this:

find: The current directory is included in the PATH environment variable, which is insecure in combination with the -execdir action of find. Please remove the current directory from your $PATH (that is, remove "." or leading or trailing colons)


I do have subdirectories..I tried posting my music library tree but it is too long. let me try a smaller list

greenpoise 09-01-2010 01:03 PM

this are two consecutive directories:

Quote:

|-- Dishwalla - Pet Your Friends
| |-- Dishwalla - Pet Your Friends - 01 - Pretty Babies.ogg
| |-- Dishwalla - Pet Your Friends - 02 - Haze.ogg
| |-- Dishwalla - Pet Your Friends - 03 - Counting Blue Cars.ogg
| |-- Dishwalla - Pet Your Friends - 04 - Explode.ogg
| |-- Dishwalla - Pet Your Friends - 05 - Charlie Brown's Parents.ogg
| |-- Dishwalla - Pet Your Friends - 06 - Give.ogg
| |-- Dishwalla - Pet Your Friends - 07 - Miss Emma Peel.ogg
| |-- Dishwalla - Pet Your Friends - 08 - Moisture.ogg
| |-- Dishwalla - Pet Your Friends - 09 - The Feeder.ogg
| |-- Dishwalla - Pet Your Friends - 10 - All She Can See.ogg
| |-- Dishwalla - Pet Your Friends - 11 - Only For so Long.ogg
| |-- Dishwalla - Pet Your Friends - 12 - Interview With St. Etienne.ogg
| |-- Dishwalla - Pet Your Friends.ogg.m3u
| |-- album.jpg
| `-- cover.jpg
|-- Eagles of Death Metal
| |-- Eagles Of Death Metal - Peace Love Death Metal
| | |-- Eagles Of Death Metal - Already Died.mp3
| | |-- Eagles Of Death Metal - Bad Dream Mama.mp3
| | |-- Eagles Of Death Metal - English Girl.mp3
| | |-- Eagles Of Death Metal - Flames Go Higher.mp3
| | |-- Eagles Of Death Metal - I Only Want You.mp3
| | |-- Eagles Of Death Metal - Kiss the Devil.mp3
| | |-- Eagles Of Death Metal - Midnight Creeper.mp3
| | |-- Eagles Of Death Metal - Miss Alissa.mp3
| | |-- Eagles Of Death Metal - San Berdoo Sunburn.mp3
| | |-- Eagles Of Death Metal - So Easy.mp3
| | |-- Eagles Of Death Metal - Speaking In Tongues.mp3
| | |-- Eagles Of Death Metal - Stacks O Money.mp3
| | |-- Eagles Of Death Metal - Stuck In The Metal.mp3
| | |-- Eagles Of Death Metal - Wastin My Time.mp3
| | |-- Eagles Of Death Metal - Whorehoppin (Shit, Goddamn).mp3
| | |-- album.jpg
| | `-- playlist.m3u

crts 09-01-2010 01:10 PM

Then try this
Code:

find /your/music/dir -type f -name album.jpg -exec sh -c 'cp "$1" $(dirname "$1")/covers.jpg' {} {} \;


All times are GMT -5. The time now is 04:56 PM.