m4r4v
Posted on July 21, 2020
Day 05
10 examples in 11 days
Delete
Today is the turn of the --delete option. I use this option a lot, when it comes to backups or to mantain source and destination files up to date.
Eventually you sync too much files and sometimes you just delete some files at source without removing them in destination. You know rsync does not remove any file at destination no matter if they do not exist at source, it just sync files/directories using blocks on files/directories that exist on both sides.
This is when --delete come handy. If you remove a file/s on soure and if you use this option, it will remove those files that doesn't exist in source at destination.
In our last article about backups I used ~/Users folder as source on which I had a file called file1.txt and we used it to sync and backup to our destination ~/Sync folder. I will create another file and will call it file2.txt and remove our file1.txt file.
iamgroot@laptop:~$ rm ~/Users/file1.txt && touch ~/Users/file2.txt && echo "Hello World" >> ~/Users/file2.txt && ls -F ~/Users/
file2.txt
Now, I will sync our source and destinations folders and check what we have in both folders
iamgroot@laptop:~$ ls -F ~/Users && ls -F ~/Sync
file2.txt
file1.txt file2.txt
As you can see, in our source folder we only have file2.txt and in our destination we still have file1.txt. Here is when we should use the --delete option. Using it, will remove files in destination missing at source
iamgroot@laptop:~$ rsync -vhr --delete ~/Users/ ~/Sync && ls -F ~/Users && ls -F ~/Sync
sending incremental file list
deleting file1.txt
file2.txt
sent 124 bytes received 48 bytes 344.00 bytes/sec
total size is 12 speedup is 0.07
file2.txt
file2.txt
Now we only have file2.txt in both folders. Nice!!
I do have to point out something. We do have few more options to play with:
--delete-before | receiver deletes before transfer, not during (this is the default) |
--delete-during | receiver deletes during the transfer |
--delete-delay | find deletions during, delete after |
--delete-after | receiver deletes after transfer, not during |
--delete-excluded | also delete excluded files from dest dirs |
Another great option that we must include in this article is the --remove-source-files which removes from source synchronized files. Be aware that this option don't remove directories. This option is very useful when doing backups (tar, zip files), since backups are usually compressed files we barely want to keep those files at source after synching. Let's try it out, I will create a folder called ~/Users/backup, then I will create some files inside ~/Users/backup/ and at the end I will back them up using tar. After all this I will perform a sync and will check if it gets removed
iamgroot@laptop:~$ mkdir ~/Users/backup && touch ~/Users/backup/tobackup{1..5}.txt && tar -cvzf ~/Users/backup.tar.gz ~/Users/backup/ && ls -F ~/Users
tar: Removing leading `/' from member names
/home/iamgroot/Users/backup/
/home/iamgroot/Users/backup/tobackup3.txt
/home/iamgroot/Users/backup/tobackup4.txt
/home/iamgroot/Users/backup/tobackup5.txt
/home/iamgroot/Users/backup/tobackup2.txt
/home/iamgroot/Users/backup/tobackup1.txt
backup/ backup.tar.gz file2.txt
Performing rsync with --remove-source-files option
iamgroot@laptop:~$ rsync -vhr --remove-source-files ~/Users/backup.tar.gz ~/Sync && ls -F ~/Users
sending incremental file list
backup.tar.gz
sent 292 bytes received 43 bytes 670.00 bytes/sec
total size is 196 speedup is 0.59
backup/ file2.txt
Ok, that'll be for today's example, thanks for reading!!!
Follow, ❤ or 🦄
Posted on July 21, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.