Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Conditional file copy

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
sunrat
Administrator
Administrator
Posts: 6476
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

Conditional file copy

#1 Post by sunrat »

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
dilberts_left_nut
Administrator
Administrator
Posts: 5346
Joined: 2009-10-05 07:54
Location: enzed
Has thanked: 13 times
Been thanked: 66 times

Re: Conditional file copy

#2 Post by dilberts_left_nut »

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
sunrat
Administrator
Administrator
Posts: 6476
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

Re: Conditional file copy

#3 Post by sunrat »

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
dilberts_left_nut
Administrator
Administrator
Posts: 5346
Joined: 2009-10-05 07:54
Location: enzed
Has thanked: 13 times
Been thanked: 66 times

Re: Conditional file copy

#4 Post by dilberts_left_nut »

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...

neuraleskimo
Posts: 195
Joined: 2019-03-12 23:26

Re: Conditional file copy

#5 Post by neuraleskimo »

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
sunrat
Administrator
Administrator
Posts: 6476
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

Re: Conditional file copy

#6 Post by sunrat »

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!

neuraleskimo
Posts: 195
Joined: 2019-03-12 23:26

Re: Conditional file copy

#7 Post by neuraleskimo »

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
sunrat
Administrator
Administrator
Posts: 6476
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

Re: Conditional file copy

#8 Post by sunrat »

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!

neuraleskimo
Posts: 195
Joined: 2019-03-12 23:26

Re: Conditional file copy

#9 Post by neuraleskimo »

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
sunrat
Administrator
Administrator
Posts: 6476
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

Re: Conditional file copy

#10 Post by sunrat »

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
dilberts_left_nut
Administrator
Administrator
Posts: 5346
Joined: 2009-10-05 07:54
Location: enzed
Has thanked: 13 times
Been thanked: 66 times

Re: Conditional file copy

#11 Post by dilberts_left_nut »

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
sunrat
Administrator
Administrator
Posts: 6476
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

Re: Conditional file copy

#12 Post by sunrat »

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
Administrator
Administrator
Posts: 6476
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 118 times
Been thanked: 474 times

Re: Conditional file copy

#13 Post by sunrat »

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!

Post Reply