Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rsync : Recursively sync all files while ignoring the directory structure

Tags:

I am trying to create a bash script for syncing music from my desktop to a mobile device. The desktop is the source.

Is there a way to make rsync recursively sync files but ignore the directory structure? If a file was deleted from the desktop, I want it to be deleted on the device as well.

The directory structure on my desktop is something like this.

    Artist1/
        Artist1/art1_track1.mp3
        Artist1/art1_track2.mp3
        Artist1/art1_track3.mp3
    Artist2/
        Artist2/art2_track1.mp3
        Artist2/art2_track2.mp3
        Artist2/art2_track3.mp3
    ...

The directory structure that I want on the device is:

    Music/
        art1_track1.mp3
        art1_track2.mp3
        art1_track3.mp3
        art2_track1.mp3
        art2_track2.mp3
        art2_track3.mp3
    ...
like image 210
Srinath Sridhar Avatar asked Feb 02 '13 10:02

Srinath Sridhar


People also ask

How do I ignore a folder in rsync?

Exclude Files and Directories from a List. When you need to exclude a large number of different files and directories, you can use the rsync --exclude-from flag. To do so, create a text file with the name of the files and directories you want to exclude. Then, pass the name of the file to the --exlude-from option.

Does rsync sync both ways?

rsync works in one direction, so we need to run it twice to sync directories in both directions.

How do I keep two directories in sync with rsync?

To sync the contents of dir1 to dir2 on the same system, you will run rsync and use the -r flag, which stands for “recursive” and is necessary for directory syncing: rsync -r dir1/ dir2.

Does rsync recursive?

The rsync tool can recursively navigate a directory structure and update a second location with any new/changed/removed files. It checks to see if files exist in the destination before sending them, saving bandwidth and time for everything it skips.


1 Answers

Simply:

rsync -a --delete --include=*.mp3 --exclude=* \
    pathToSongs/Theme*/Artist*/. destuser@desthost:Music/.

would do the job if you're path hierarchy has a fixed number of level.

WARNING: if two song file do have exactly same name, while on same destination directory, your backup will miss one of them!

If else, and for answering strictly to your ask ignoring the directory structure you could use bash's shopt -s globstar feature:

shopt -s globstar
rsync -a --delete --include=*.mp3 --exclude=* \
    pathToSongsRoot/**/. destuser@desthost:Music/.

At all, there is no need to fork to find command.

Recursively sync all files while ignoring the directory structure

For answering strictly to question, there must no be limited to an extension:

shopt -s globstar
rsync -d --delete sourceRoot/**/. destuser@desthost:destRoot/.

With this, directories will be copied too, but without content. All files and directories would be stored on same level at destRoot/.

WARNING: If some different files with same name exists in defferents directories, they would simply be overwrited on destination, durring rsync, for finaly storing randomly only one.

like image 142
F. Hauri Avatar answered Oct 14 '22 14:10

F. Hauri