(SOLVED)How to sync files to a USB stick, INCLUDING DELETING

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
ericramos1990

(SOLVED)How to sync files to a USB stick, INCLUDING DELETING

Post by ericramos1990 »

Hey guys!

I have a question regarding a script I have to copy all of my music to my phone.

Code: Select all

cp -ruv /home/eric/music/* '/media/MyAndroid/Internal storage/music'
However, there is one additional thing I want to do.

If I delete some songs from my computer, how can I get this script to simply delete them from my phone as well?

I know I can do rm
'/media/MyAndroid/Internal storage/music'
and then
cp -ruv /home/eric/music/* '/media/MyAndroid/Internal storage/music'

But that will take an unneeded amount of time.

Please let me know if you don't understand what I am trying to ask, thanks guys I hope you have an answer!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
ericramos1990

Re: How to update files to a USB stick, INCLUDING DELETING

Post by ericramos1990 »

Bumpiddy bump? O.o
ericramos1990

Re: How to update files to a USB stick, INCLUDING DELETING

Post by ericramos1990 »

Final bump sorry guys
Habitual

Re: How to update files to a USB stick, INCLUDING DELETING

Post by Habitual »

ericramos1990 wrote:Hey guys!
I have a question regarding a script I have to copy all of my music to my phone.

Code: Select all

cp -ruv /home/eric/music/* '/media/MyAndroid/Internal storage/music'
However, there is one additional thing I want to do.
If I delete some songs from my computer, how can I get this script to simply delete them from my phone as well?
Use rsync with --delete option. Anything NOT found in /home/eric/music will be deleted from "Internal storage/music.

Code: Select all

rsync -avz /home/eric/music "/media/MyAndroid/Internal storage" --no-perms --delete
do a test...

Code: Select all

touch /home/eric/music/myfake.mp3
now rsync it with

Code: Select all

rsync -avz /home/eric/music "/media/MyAndroid/Internal storage" --no-perms
one of the very first files shown to be copied should be

Code: Select all

sending incremental file list
./
myfake,mp3
Now check for it with:

Code: Select all

find `pwd` "/media/MyAndroid/Internal storage" -name myfake.mp3 -type f
and that should spit out

Code: Select all

/media/MyAndroid/Internal storage/music/myfake.mp3
Now we are cooking with gas so, now delete the /home/eric/music/myfake.mp3 and use this from now on:

Code: Select all

rsync -avz /home/eric/music "/media/MyAndroid/Internal storage" --no-perms --delete
and everything NOT present in /home/eric/music will NOT be present in /media/MyAndroid/Internal storage after doing so.

adding an n to -avz (-avzn) will do a "dry-run".
so try that too before committing to using the --delete option of rysnc.

If you get nervous or stuck,. shoot me a PM and we'll go over it.
ericramos1990

Re: How to update files to a USB stick, INCLUDING DELETING

Post by ericramos1990 »

Habitual, thank you sssoo much!

You honestly had to simply say the word "rsync" and I would have been perfectly fine researching it.

So thanks a lot for taking the time to make a whole tutorial on it I really appreciate it!

I tried it, and it is exactly what I wanted, I will immediately change it to solved!

I hope this helps others as well!
Habitual

Re: (SOLVED)How to sync files to a USB stick, INCLUDING DELE

Post by Habitual »

Glad it worked out.
Cammo

Re: (SOLVED)How to sync files to a USB stick, INCLUDING DELE

Post by Cammo »

Hey Habitual, I know this is an old post, but thanks for the tutorial - helped me out too.
Habitual

Re: (SOLVED)How to sync files to a USB stick, INCLUDING DELE

Post by Habitual »

Cammo wrote:Hey Habitual, I know this is an old post, but thanks for the tutorial - helped me out too.
The mechanics of rsync haven't changed in ages, Glad this info helped you out.
Locked

Return to “Scripts & Bash”