2

I want to delete the duplicate files from two directories. The structure of the directories and files are as follows

    nijat@nijat-virtual-machine:~/mansoor$ mkdir dir1
    nijat@nijat-virtual-machine:~/mansoor$ mkdir dir2
    nijat@nijat-virtual-machine:~/mansoor$ ls
    dir1  dir2
    nijat@nijat-virtual-machine:~/mansoor$ cd dir1
    nijat@nijat-virtual-machine:~/mansoor/dir1$ touch myfile.txt
    nijat@nijat-virtual-machine:~/mansoor/dir1$ ls
    myfile.txt
    nijat@nijat-virtual-machine:~/mansoor/dir1$ cd ..
    nijat@nijat-virtual-machine:~/mansoor$ cd dir2
    nijat@nijat-virtual-machine:~/mansoor/dir2$ touch myfile.txt
    nijat@nijat-virtual-machine:~/mansoor/dir2$ ls
    myfile.txt

I have tried to delete the duplicate file from both dir1 and dir2. For this I have written the following command.

nijat@nijat-virtual-machine:~/mansoor$ fdupes -d dir1 -r dir2
[1] dir1/myfile.txt                     
[2] dir2/myfile.txt

Set 1 of 1, preserve files [1 - 2, all]: all

   [+] dir1/myfile.txt
   [+] dir2/myfile.txt

nijat@nijat-virtual-machine:~/mansoor$ fdupes -d dir1 -r dir2
[1] dir1/myfile.txt                     
[2] dir2/myfile.txt

Set 1 of 1, preserve files [1 - 2, all]: 2

   [+] dir1/myfile.txt
   [-] dir2/myfile.txt

nijat@nijat-virtual-machine:~/mansoor$ cd dir1
nijat@nijat-virtual-machine:~/mansoor/dir1$ ls
myfile.txt
nijat@nijat-virtual-machine:~/mansoor/dir1$ cd ..
nijat@nijat-virtual-machine:~/mansoor$ cd dir2
nijat@nijat-virtual-machine:~/mansoor/dir2$ ls
nijat@nijat-virtual-machine:~/mansoor/dir2$ 

But the problem is that I don't know what option I have to select to delete both files from both directories.

Set 1 of 1, preserve files [1 - 2, all]:

I have tried different options, for example, I give all to delete all files but nothing happens. When I select 1 it deletes the file from dir1 but the file remains in the dir2 if I select 2 the file will be deleted from dir2 but it will not be deleted from dir1.

I also tried to delete both files from both directories without asking me. I use the following command

  nijat@nijat-virtual-machine:~/mansoor$ fdupes -dN dir1 -r dir2
                                        
   [+] dir1/myfile.txt
   [-] dir2/myfile.txt

nijat@nijat-virtual-machine:~/mansoor$ cd dir2
nijat@nijat-virtual-machine:~/mansoor/dir2$ ls
nijat@nijat-virtual-machine:~/mansoor/dir2$ cd ..
nijat@nijat-virtual-machine:~/mansoor$ cd dir1
nijat@nijat-virtual-machine:~/mansoor/dir1$ ls
myfile.txt

As you can see my file still exists in dir1. Please help me with this, I just want to delete both files from both directories at the same time without confirmation.

2 Answers 2

0

You are misunderstanding the prompt from fdupes: "preserve" means to keep that file.

The tool you are using is not intended to delete both/all duplicates (which makes sense - usually you don't want to lose information)

from man (1) fdupes:

-d --delete
         prompt user for files to preserve, deleting all others (see CAVEATS below)

-N --noprompt
         when  used together with --delete, 
         preserve the first file in each set of duplicates 
         and delete the others without prompting the user
1
  • can you tell me what I need to do to delete both file form both the directory if you know another method please help me Aug 9, 2020 at 19:03
0

I've never heard of a tool that deletes both files from both directories -- as guntbert said in his answer, the objective is not to lose information, while reducing space.

Buf your comments to the previous answer indicate that is what you want. Here's one way:

find dir1 dir2 -type f -print0 | xargs -0 md5sum | sort | uniq -w32 -D | cut -c35-

This command only produces a list of files; it does not actually remove them. Add | xargs -o -d '\n' rm (assuming Linux) to remove them. Please do test first of course.

Just out of curiosity, why would you want to delete only duplicate files (including originals) and not files that are not duplicates of anything else? Try as I might, I can't come up with a use case for this somewhat unusual requirement.

You must log in to answer this question.

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