Conditional file copy

Need help with C, C++, perl, python, etc?

Conditional file copy

Postby sunrat » 2019-04-10 02:27

I used SACAD script to download album covers for my music collection which was successful for over 1000 albums but failed for a lot of more obscure ones. In each directory the cover is saved as cover.png .
I want to copy a .directory file to each directory containing cover.png so the image will replace the directory default image in Dolphin. The .directory file works for that and contains -
Code: Select all
[Desktop Entry]
Encoding=UTF-8
Icon=./cover.png
Type=Directory


How can I copy that file to each directory containing cover.png but not to those that don't? I can use this to copy to all directories but not sure how to make it conditional.
Code: Select all
find . -type d -exec cp ~/temp/.directory {}/ \;


My amateurish bash bumblings came up with this but I want expert advice before trying it.
Code: Select all
find . -type d -exec "[ -f .cover.png ] && cp ~/temp/.directory {}/ \;"
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2758
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: Conditional file copy

Postby dilberts_left_nut » 2019-04-10 05:02

Code: Select all
find . -type f -name cover.png -print | while read d; do target=\"$(dirname "$d")\";echo cp ~/temp/.directory $target/; done

Test then remove the "echo".
AdrianTM wrote:There's no hacker in my grandma...
User avatar
dilberts_left_nut
 
Posts: 4994
Joined: 2009-10-05 07:54
Location: enzed

Re: Conditional file copy

Postby sunrat » 2019-04-10 23:23

Close but no cigar. I set up a nest of directories in ~ with cover.png in 2 of them:
Code: Select all
~/test$ find . -type d
.
./dir2
./dir1
./dir3
./dir3/subdir1


The first bit works:
Code: Select all
~/test$ find . -type f -name cover.png -print
./dir2/cover.png
./dir3/subdir1/cover.png


And with echo:
Code: Select all
~/test$ find . -type f -name cover.png -print | while read d; do target=\"$(dirname "$d")\";echo cp ~/temp/.directory $target/; done
cp /home/roger/temp/.directory "./dir2"/
cp /home/roger/temp/.directory "./dir3/subdir1"/


But remove echo:
Code: Select all
~/test$ find . -type f -name cover.png -print | while read d; do target=\"$(dirname "$d")\";cp ~/temp/.directory $target/; done
cp: cannot create regular file '"./dir2"/': No such file or directory
cp: cannot create regular file '"./dir3/subdir1"/': No such file or directory


This has me baffled. Running the exact cp command as output with echo works as expected. :?
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2758
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: Conditional file copy

Postby dilberts_left_nut » 2019-04-10 23:36

Hmm. I must admit I didn’t test actual copying, just a few contortions to output the right looking echo string...
Dunno why either and can’t currently test.
AdrianTM wrote:There's no hacker in my grandma...
User avatar
dilberts_left_nut
 
Posts: 4994
Joined: 2009-10-05 07:54
Location: enzed

Re: Conditional file copy

Postby neuraleskimo » 2019-04-10 23:56

Like @dilberts_left_nut, I don't have time to debug. However, I did notice that the error message includes embedded quotes. That is, the quotes are part of the filename (not good).
Code: Select all
cp: cannot create regular file '"./dir2"/': No such file or directory
cp: cannot create regular file '"./dir3/subdir1"/': No such file or directory

That means we should take a look at the quotes. Glancing at the script, the embedded double quotes looks problematic. I should have a little more time before bed and will take a look then unless someone else beats me to it.
Last edited by neuraleskimo on 2019-04-11 01:32, edited 1 time in total.
User avatar
neuraleskimo
 
Posts: 102
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA

Re: Conditional file copy

Postby sunrat » 2019-04-11 00:41

I was wondering about the quotes. However as I mentioned, running the output of the cp command as returned when using echo works:
Code: Select all
~/test$ cp /home/roger/temp/.directory "./dir3/subdir1"/

I see what you mean with the additional single quotes that probably are what's making it fail. No idea how to correct that.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2758
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: Conditional file copy

Postby neuraleskimo » 2019-04-11 01:31

sunrat wrote:I was wondering about the quotes. However as I mentioned, running the output of the cp command as returned when using echo works:
Code: Select all
~/test$ cp /home/roger/temp/.directory "./dir3/subdir1"/

I see what you mean with the additional single quotes that probably are what's making it fail. No idea how to correct that.

Try this:
Code: Select all
find . -type f -name cover.png -print | while read d; do cp ~/temp/.directory '$(dirname "$d")'/; done

BTW, I didn't try it, so you may need to fix some typos.
User avatar
neuraleskimo
 
Posts: 102
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA

Re: Conditional file copy

Postby sunrat » 2019-04-11 01:42

Still no joy:
Code: Select all
~/test$ find . -type f -name cover.png -print | while read d; do cp ~/temp/.directory '$(dirname "$d")'/; done
cp: cannot create regular file '$(dirname "$d")/': Not a directory
cp: cannot create regular file '$(dirname "$d")/': Not a directory


Not expanding the variable?:
Code: Select all
~/test$ find . -type f -name cover.png -print | while read d; do echo '$(dirname "$d")'/; done
$(dirname "$d")/
$(dirname "$d")/
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2758
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: Conditional file copy

Postby neuraleskimo » 2019-04-11 01:47

sunrat wrote:Still no joy:
Code: Select all
~/test$ find . -type f -name cover.png -print | while read d; do cp ~/temp/.directory '$(dirname "$d")'/; done
cp: cannot create regular file '$(dirname "$d")/': Not a directory
cp: cannot create regular file '$(dirname "$d")/': Not a directory


Not expanding the variable?:
Code: Select all
~/test$ find . -type f -name cover.png -print | while read d; do echo '$(dirname "$d")'/; done
$(dirname "$d")/
$(dirname "$d")/

Opps. Remove the single quotes. Too many languages floating around in my head.
User avatar
neuraleskimo
 
Posts: 102
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA

Re: Conditional file copy

Postby sunrat » 2019-04-11 01:53

Perfect, thanks neuraleskimo! 8)
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2758
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: Conditional file copy

Postby dilberts_left_nut » 2019-04-11 02:39

So it works?

I had added the extra code to include the quotes in the $target variable, but looks like the expansion of the quoted $d during accounts for any space where copy/paste from the echo output wouldn’t.

You didn’t say if your dirnames had spaces, bu my music library definitely does. ;)
AdrianTM wrote:There's no hacker in my grandma...
User avatar
dilberts_left_nut
 
Posts: 4994
Joined: 2009-10-05 07:54
Location: enzed

Re: Conditional file copy

Postby sunrat » 2019-04-11 03:22

You're right dln, I didn't test for spaces and they cause it to fail. Double quotes around the variable in neuraleskimo's example code seems to work OK:
Code: Select all
~/test$ find . -type f -name cover.png -print | while read d; do cp ~/temp/.directory "$(dirname "$d")"/; done


Combine the greatest minds on FDN and the greatest bash n00b (me) and we get there in the end. Thank you both!
Now to try it on my actual music collection. Soon...
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2758
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: Conditional file copy

Postby sunrat » 2019-04-17 13:17

Worked quite well on ~1100 albums along with SACAD. 8) I still had to to recheck manually as SACAD made a small number of dodgy album art matches but overall it saved a lot of time, even allowing for how long it took to work out the right command. And I learned a bit more about BASH on the way! Double win. :wink:
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2758
Joined: 2006-08-29 09:12
Location: Melbourne, Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable