A little something slightly outside my bash scripting skills; I want to make a script which deletes everything in one path not present in another path. It must be fully recursive, so it applies to all files / folders / subfolders inside the two paths. For example, if I have:
The command would delete /foo2/bar2.txt because the file isn’t also present in /foo1, but keep /foo2/bar1.txt because it is. Does anyone know of a shell script that takes both base folders as arguments and does this?
Try the comm utility which is explained in that thread too but in your case replace ‘.jpg’ with '.txt’ if what you are after are files with .txt extension. You can remove the -iname '.jpg*’ part if you just want to list all files and not just a certain type of file. Once you have come up with the list of files that you are after then it is easy to do the removing/deleting part. Also find(1) is recursive in nature unless you tell it not to:)
Hmmm… I didn’t know rsync works that easily with local folders too, since I thought it’s a tool for syncing special web repositories only. I do actually need this as part of mirroring a folder between drives, which I currently do with the command ‘cp -urpv’ but that doesn’t delete files I removed from the original. I shall try the command nrickert mentioned first in this case. Thanks.
On 2014-06-02 14:36, MirceaKitsune wrote:
> Hmmm… I didn’t know rsync works that easily with local folders too,
> since I thought it’s a tool for syncing special web repositories only. I
> do actually need this as part of mirroring a folder between drives,
> which I currently do with the command ‘cp -urpv’ but that doesn’t delete
> files I removed from the original. I shall try the command nrickert
> mentioned first in this case. Thanks.
For keeping two or more folders in sync, unison is the best tool, as
rsync is one direction only.
unison compares both sides, and decide what files to copy in one
direction, what in the other direction, what to remove in any side - and
it asks you for confirmation.
It is perfect for keeping in sync work folders in desktop and laptop,
Cheers / Saludos,
Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)