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:
/foo1/bar1.txt
/foo2/bar1.txt
/foo2/bar2.txt
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:)
> When I want to do what you describe, it is typically because I want the
> two directories to have identical content.
I use ‘mc’ and tell it to compare directories. Different files are
marked on both panes, so that you can decide to delete them, copy,
move… or invert the selection and act on those instead.
–
Cheers / Saludos,
Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)
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,
for instance.
–
Cheers / Saludos,
Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)