Delete everything in one directory not present in another

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?

Hi,

This you might want to start with this thread.

https://forums.opensuse.org/showthread.php/498377-Find-duplicated-filenames

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:)

I’m not sure if this is useful.

When I want to do what you describe, it is typically because I want the two directories to have identical content.

So I use:


rsync -a --delete /foo1/. /foo2/.

But this will also copy/sychronize files that don’t have the same date/file length/permissions.

On 2014-06-02 01:46, nrickert wrote:

> 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)

Install Unison and use that. I use it all the time to keep 2 NTFS partitions synchronized, as well as to keep individual Linux paths synchronized.

It is superb for that purpose, and is in the openSUSE repositories.

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)

Solved. I set up a script with the following rsync command, which perfectly mirrors my directory like I want it:

rsync -rlpta --delete --verbose --stats "/src/" "/dst/"