Raspberry Pi

Delete Duplicate Files on Raspberry Pi Using fdupes

Sometimes while working on a system, you don’t realize that you have duplicated files with the same content inside them. These duplicate files may be saved on your system as a result of downloading them multiple times. Thus, these files acquire space on the disk which is not good for systems like Raspberry Pi that have limited disk space. So in such cases, it is better to remove these files to free up the space on the system. However, finding duplicate files manually could be a hard task for anyone. To make things simple, there is a tool designed especially for this purpose called fdupes, which allows you to find and delete the duplicate files on the system.

Follow this article’s guidelines to delete duplicate files on Raspberry Pi through fdupes.

Delete Duplicate Files on Raspberry Pi Using fdupes

To delete the duplicate files on Raspberry Pi using fdupes, follow the below-mentioned instructions:

Step 1: Before installing fdupes on Raspberry Pi, it is recommended to update and upgrade the Raspberry Pi repository and for that run the below-written commands in terminal:

$ sudo apt update && sudo apt upgrade

Step 2: Then install fdupes by using the below-written command:

$ sudo apt install fdupes

Step 3: After the installation, fdupes is ready to be used to find the duplicate files. Just run the following command with the path of a directory in which you want to find the duplicate files:

Syntax

$ fdupes <directory-path>

Example

$ fdupes /home/pi/Documents

The above command searches the duplicate files inside the directory.

If there are so many sub-directories inside a directory; then to recursively search duplicate files, use the following command:

$ fdupes -r ~/Documents

In the above command, the first two files are from a similar directory with the same content inside the files. The third one with the same file name is from a subdirectory with the same content inside the file.

Step 4: If you want to delete the duplicate files in any directory, use the below-mentioned command:

Syntax

$ fdupes -d <directory path>

Example

$ fdupes -d /home/pi/Documents

Step 5: After entering the above command, the list of duplicated files will appear on the screen, and a preserve file text will display at the bottom of the screen with various options:

Note: The following is the Documents’ directory content before fdupes operation:

Step 6: For the file that you want to keep (preserve), type the number of that file in front of the preserve file options, the other will get deleted automatically:

Here, I am preserving file 1 which is file3.txt and deleting the other one:

Step 7: After entering the file number, the positive (+) sign will appear with the file that will be kept and a negative () sign will appear with the file that will get deleted:

Step 8: Then enter the word prune and the duplicated file will get deleted:

prune

Step 9: Once the file is deleted, you can go back to the terminal by typing the exit command:

exit

Step 10: To confirm that the file is deleted, enter the ls command with the directory path and you will see that file which we have deleted will not be present in the list:

Syntax

$ ls <directory path>

Example

$ ls /home/pi/Documents

In this way, you can delete other duplicate files in the same way in any directory on the Raspberry Pi system.

Conclusion

fdupes can be used to find and delete the duplicate files in Raspberry Pi. Just install the fdupes from the Raspberry Pi repository using the apt command. Then use the fdupes command along with a directory name to find the duplicate files in any directory. To recursively search files in all the sub-directories fdupes is used along with -r flag and to delete the duplicated file use -d flag.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.