1

I'm using rsync to load my mp3 player with music from a text file, and as such I'd like to delete extraneous files that aren't included in the "files-from" list. (Say if I no longer want a particular song on the music player and remove it from the playlists.)

However, simply using any of the delete options doesn't seem to work (--delete, --delete-during, etc.) I tried excluding everything else (--exclude="*.*") but that also did not remove extraneous files from the destination. The current command I'm using is.

rsync -vi --ignore-existing --files-from="~/include.txt" ~ ~/TestFolder/

At first I had used all the typical rsync options (-aR etc, but they seem to do nothing when syncing from a files-from list) so I removed them. And I'm just using verbose and itemize changes for testing purposes so I can see what's going on. I tried adding -r with --delete, but that doesn't work either. I also tried (--delete-excluded --exclude="*.*").

Any suggestions?

I suppose I could generate a list of files to delete then specifically delete them... but isn't that what rsync SHOULD do?

1 Answer 1

0

After a bit more searching, I found out that I can't do this on rsync (at least on the rsync that's installed on my mac.)

However, I did find a script to do essentially this. You'll have to mess around with paths and what not, but it does work.

From user "cpbills" over at https://stackoverflow.com/questions/1813907/rsync-delete-files-from-list-dest-does-not-delete-unwanted-files

#!/bin/bash                                                                 

playlist_path="/home/cpbills/.config/mpd/playlists"
playlist="${playlist_path}/${1}.m3u"
music_src="/home/cpbills/files/music"
music_dst="/mnt/sdcard/music/"

if [[ -e "$playlist" ]]; then
  # Remove old files
  find "$music_dst" -type f | while read file; do
    name="$(echo "$file" | sed -e "s!^$music_dst!!")"
    if ! grep -qF "$name" "$playlist"; then
      rm "$file"
    fi
  done

  # Remove empty directories
  find "$music_dst" -type d -exec rmdir {} \; 2>/dev/null

  rsync -vu \
      --inplace \
      --files-from="$playlist" \
      "$music_src" "$music_dst"
else
  printf "%s does not exist\n" "$playlist" 1>&2
  exit 1
fi

personally I doubled up on the "remove empty directories" line because as the script above sits, it'll only remove one layer of empty directories. So if you have /Folder1/Folder2/Folder3 and run the command, you'll still have /Folder1/Folder2/. If you run it twice, you'll have /Folder1/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .