I'm having problems getting my rsync syntax right and I'm wondering if my scenario can actually be handled with rsync.
PURPOSE:
1) Copy over all files/directories containing files ending in ".rc" FROM sourceHDD/.config TO destination/HDD/.config; (*.rc is of type file, not of type directory)
2) Delete any directory not recursively containing a file "*.rc" (not only empty directories).
3) Delete any file not containing a file "*.rc".
SITUATION:
I have a directory /home/${USER}/workshop/sourceHDD/.config, its file directory structure:
Code:
cd /home/${USER}/workshop
mkdir -p ./sourceHDD/.config/{dir1/dir2,dir3,dir5}
touch ./sourceHDD/.config/{file{1,2}.{txt,rc},dir3/file4.{rc,txt},dir1/dir2/file3.{rc,txt},dir5/new.txt,blah.txt}
I have a second directory /home/${USER}/workshop/destinationHDD/.config of the following file directory structure:
Code:
mkdir -p ./destinationHDD/.config/{blubber,surplus/waste/fat}
touch ./destinationHDD/.config/{blubber/trash1.txt,surplus/waste/fat/trash2.txt}
_________________________________________
ATTEMPT 01:
cd /home/${USER}/workshop
COMMAND:
Code:
rsync -avPpm --include='*rc*' --include='*/' --exclude='*' --delete ./source/* ./destination
RESULT:
Code:
9 files to consider
cannot delete non-empty directory: .config/surplus/waste/fat
cannot delete non-empty directory: .config/surplus/waste/fat
cannot delete non-empty directory: .config/surplus/waste
cannot delete non-empty directory: .config/surplus/waste
cannot delete non-empty directory: .config/surplus
cannot delete non-empty directory: .config/blubber
.config/
.config/file1.rc
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=7/9)
.config/file2.rc
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=6/9)
.config/dir1/
.config/dir1/dir2/
.config/dir1/dir2/file3.rc
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=3/9)
.config/dir3/
.config/dir3/file4.rc
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=1/9)
sent 388 bytes received 466 bytes 1,708.00 bytes/sec
total size is 0 speedup is 0.00
Even though this command successfully completed the task of ONLY copying over *.rc files along with the directories containing them, it did not delete the blubber and surplus files.
Seeing the "cannot delete non-empty directory: .config/surplus/waste/fat" like error messages, I naturally think that perhaps the "-m" (prune empty directories) option is preventing the rsync from getting rid of the unwanted directories, thus the next attempt:
_________________________________________
ATTEMPT 02:
Code:
rsync -avPp --include='*rc*' --include='*/' --exclude='*' --delete ./sourceHDD/.config ./destinationHDD
sending incremental file list
cannot delete non-empty directory: .config/surplus/waste/fat
cannot delete non-empty directory: .config/surplus/waste/fat
cannot delete non-empty directory: .config/surplus/waste
cannot delete non-empty directory: .config/surplus/waste
cannot delete non-empty directory: .config/surplus
cannot delete non-empty directory: .config/blubber
.config/
.config/file1.rc
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=7/9)
.config/file2.rc
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=6/9)
.config/dir1/
.config/dir1/dir2/
.config/dir1/dir2/file3.rc
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=1/9)
.config/dir3/
.config/dir3/file4.rc
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=0/9)
.config/dir5/
sent 414 bytes received 478 bytes 1,784.00 bytes/sec
total size is 0 speedup is 0.00
Overall, this is not as successful as Attempt 01 since dir5 was copied over. And the "cannot delete non-empty directory: " error messages are still there. Unfortunately I think this could mean that the "--include='*/'" condition protects non-empty directories.
_________________________________________
ATTEMPT 03:
I then read the above link and scrolled down to "guettli"'s answer: '
Code:
find ./sourceHDD/.config -iname '*.rc' -print0 | rsync -0 -v --files-from=- . ./destinationHDD
AND
cd ./sourceHDD/.config
find . -iname '*.rc' -print0 | rsync -0 -v --files-from=- . /home/${USER}/workshop/destinationHDD/.config
but these attempts don't copy to the target directory.
other failures
Code:
find ./source -iname '*.rc' -print0 | rsync -avPpm ./source/ ./destination/
{fail: this copies everything (*.txt and *.rc) into destination directory.}
find ./source/ -iname '*.rc' -print0 | rsync -0 -v --files-from=- ./destination
{fail: rsync error}
Does anyone know how to run the command and then get rid of the non-empty directories not containing files with names containing "rc"?