Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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. Or simply putting same thing: How to make checking of Folder A files if they exist in Folder B folders then delete from Folder B folders?

Idea of solution Maybe a command witch:

  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.

Cause duplication programs were bad:

  1. Long time until 1st deletion- its only when finished scanning
  2. And no possible to choose deleting in Folder B. Its possible only to keep latest and something else also but not by choosing in witch folder to keep.

Useless history: Files were copied from Recuva in Folder B and partly arranged but a lot of them are bad so 1st im thinking Folder B comparing if exist to thouse witch 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
share|improve this question

1 Answer 1

up vote 5 down vote accepted

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 
share|improve this answer
    
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 '13 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 '13 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 '13 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 '13 at 4:31
    
Searching packages.ubuntu.com for packages that contain the word "duplicate" in its description yielded fdupes. This should work in place of rdfind. –  unutbu Jan 13 '13 at 11:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.