5

Summary:

  • Folder A has only many Excelent files
  • Folder B has many folders of mixed Excelent/Good/Bad files

How can I delete files in Folder B folders only in case they will be in Folder A.

In other words, how to check if Folder A files exist in Folder B folders then delete from Folder B folders?

Idea of solution Maybe a command which:

  1. Checks part of alphabet like all starting with A
  2. Executing deletion on files found in Folder B subfolders
  3. Repeat 1. + one alphabet up.

Reasons why other duplication programs were bad:

  1. Long time until 1st deletion - it's only when finished scanning
  2. And no possibility to choose deleting in Folder B. It's possible only to keep latest and something else also, but not by choosing in which folder to keep.

Useless history: Files were copied from Recuva in Folder B and partly arranged, but a lot of them are bad. So first, I'm thinking Folder B comparing if exist to those which recovered again, but now just Excelent recovered in Folder A by Recuva so most of Excelent will be just in Folder A.

Example file tree:

.
├── A
│   ├── 1.png
│   ├── 2.png
│   └── Excellent
│       ├── e1.png
│       └── e2.png
└── B
    ├── 1.png
    ├── 2.png
    ├── Bad
    │   ├── 1.png
    │   ├── 2.png
    │   ├── e1.png
    │   └── e2.png
    └── Excellent
        ├── e1.png
        └── e2.png

1 Answer 1

6

Below are two solutions, depending on how we define "duplicate":

  • Files with the same relative path, or
  • Files with the same content but not necessarily the same name

If by "duplicate" we mean two files which share the same relative path, then you could use find and xargs to remove the duplicates. For example, suppose you have

~/tmp% tree A
A
└── Excellent
    ├── bar
    ├── baz
    └── foo
~/tmp% tree B
B
├── Bad
│   └── quux
├── Excellent
│   ├── bar
│   ├── baz
│   └── foo
└── Good

Then

find /home/unutbu/tmp/A  -depth -type f -print0 | xargs -0 -I{} bash -c 'rm "/home/unutbu/tmp/B${1#*A}"' - {}

results in

~/tmp% tree B
B
├── Bad
│   └── quux
├── Excellent
└── Good

Or, if by "duplicate" we mean two files share the same content, though perhaps not the same filename, then you could use rdfind:

sudo apt-get install rdfind

If we have this directory structure:

~/tmp% tree A
A
└── Excellent
    ├── bar
    ├── baz
    └── foo

1 directory, 3 files
~/tmp% tree B
B
├── Bad
│   └── quux
├── Excellent
│   ├── barbar
│   ├── bazbaz
│   └── foofoo
└── Good

where barbar has the same content as bar, and similarly for bazbaz and foofoo, then

rdfind -deleteduplicates true A B

results in

~/tmp% tree B
B
├── Bad
│   └── quux
├── Excellent
└── Good

Alternate solution in case your version of Ubuntu does not include rdfind:

You could instead use fdupes:

sudo apt-get install fdupes
fdupes --recurse --delete --noprompt A B 
13
  • Maybe its couse im on Ubuntu 10.04 but 1st solution isnt working. For testing i made folder Good and B. In Good is 1 file. In B is 2 subfolders both have 2 files. Command i did kangarooo@kangarooo-laptop:~/000 Test Dups$ find Good/ -depth -type f -print0 | xargs -0 -I{} bash -c 'rm "B${1#*A}"' - {} rm: cannot remove `BGood/vlcsnap-2012-12-11-01h36m32s101.png': No such file or directory And other programm isnt in 10.04 ill try when ill get to some 12.04 but is there alternative to 10.04 also?
    – Kangarooo
    Jan 13, 2013 at 2:31
  • In your command, change B${1#*A} to B${1#*Good}. This is a bash expression which removes Good from the filename and replaces it with B. The tools used in this solution fairly common to all versions of unix, and should work fine in Ubuntu 10.04.
    – unutbu
    Jan 13, 2013 at 2:45
  • Ah yes i see now what was A in there. Ok it works but not if i post to Folder B. It worked if i put it to check subfolder. Is there some recursive option also? And packages.ubuntu.com/oneiric/rdfind isnt in 10.04 (lucid) but ill try to get on some 12.04. Ill use combination of both. 1st to delete exact names. Then more cleaning and then exact sizes. Exact sizes is better but since couse of not having exact sizes couse of Excelent/Bad/Good variations of same file then they will have different sizes. But ill do some backup and check test exact comparison command also.
    – Kangarooo
    Jan 13, 2013 at 4:24
  • WOW i just understood this is SUPER AMAZING command! It will check files one by one deleting if it exists in B folder. But missing is checking in recusive subfolders of B.
    – Kangarooo
    Jan 13, 2013 at 4:31
  • 1
    fdupes isn't really an alternative as it doesn't keep the files based on specified first path but on modification date as explained here
    – TNT
    Feb 21, 2016 at 6:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .