Results 1 to 8 of 8

Thread: Delete everything in one directory not present in another

  1. #1
    Join Date
    Jan 2009
    Location
    Romania, Bucharest
    Posts
    795

    Default 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?
    openSUSE Tumbleweed x64, KDE Framework 5

  2. #2

    Default Re: Delete everything in one directory not present in another

    Quote Originally Posted by MirceaKitsune View Post
    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.
    Code:
    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
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  3. #3
    Join Date
    Aug 2010
    Location
    Chicago suburbs
    Posts
    12,869
    Blog Entries
    3

    Default Re: Delete everything in one directory not present in another

    Quote Originally Posted by MirceaKitsune View Post
    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?
    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:
    Code:
    rsync -a --delete /foo1/. /foo2/.
    But this will also copy/sychronize files that don't have the same date/file length/permissions.
    openSUSE Leap 15.1; KDE Plasma 5;
    testing Leap 15.2Alpha

  4. #4
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: Delete everything in one directory not present in another

    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)

  5. #5
    Join Date
    Nov 2013
    Location
    Kamloops, BC, Canada
    Posts
    3,974

    Default Re: Delete everything in one directory not present in another

    Quote Originally Posted by MirceaKitsune View Post
    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?
    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.
    -Gerry Makaro
    Fraser-Bell Info Tech
    Solving Tech Mysteries since the Olden Days!
    ~~
    If I helped you, consider clicking the Star at the bottom left of my post.

  6. #6
    Join Date
    Jan 2009
    Location
    Romania, Bucharest
    Posts
    795

    Default Re: Delete everything in one directory not present in another

    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.
    openSUSE Tumbleweed x64, KDE Framework 5

  7. #7
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: Delete everything in one directory not present in another

    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)

  8. #8
    Join Date
    Jan 2009
    Location
    Romania, Bucharest
    Posts
    795

    Default [SOLVED] Delete everything in one directory not present in another

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

    Code:
    rsync -rlpta --delete --verbose --stats "/src/" "/dst/"
    openSUSE Tumbleweed x64, KDE Framework 5

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •