How to use the command fdupes (with examples)
fdupes is a command-line tool that helps in finding duplicate files within a given set of directories. It is especially useful when you want to identify and remove duplicate files to save storage space. With fdupes, you can search for duplicates in a single directory, multiple directories, or even recursively through directories.
Use case 1: Search a single directory
Code:
fdupes path/to/directory
Motivation:
Searching a single directory is the simplest use case of fdupes. This command allows you to find duplicate files within a specific directory.
Explanation:
fdupes
: The command to run the fdupes program.path/to/directory
: The path to the directory you want to search for duplicates in.
Example output:
./path/to/directory/file1.txt
./path/to/directory/file2.txt
Use case 2: Search multiple directories
Code:
fdupes directory1 directory2
Motivation:
If you have multiple directories that you suspect may contain duplicate files, you can use this command to search across all of them at once.
Explanation:
fdupes
: The command to run the fdupes program.directory1
: The path to the first directory you want to search for duplicates in.directory2
: The path to the second directory you want to search for duplicates in.
Example output:
./directory1/file1.txt
./directory2/file1.txt
./directory2/file2.txt
Use case 3: Search a directory recursively
Code:
fdupes -r path/to/directory
Motivation:
When you want to search for duplicates not only in a single directory but also in its subdirectories, using the -r
option is necessary. This can be helpful when you have a large directory structure and want to find duplicate files scattered throughout it.
Explanation:
fdupes
: The command to run the fdupes program.-r
: The option to search the directory and its subdirectories recursively.path/to/directory
: The path to the directory you want to search for duplicates in.
Example output:
./path/to/directory/file1.txt
./path/to/directory/subdirectory/file1.txt
Use case 4: Search multiple directories, one recursively
Code:
fdupes directory1 -R directory2
Motivation:
This use case combines the ability to search multiple directories and search one of them recursively. It is helpful when you have one directory that you want to search in depth while scanning other directories in a non-recursive manner.
Explanation:
fdupes
: The command to run the fdupes program.directory1
: The path to the first directory you want to search for duplicates in.-R
: The option to search the second directory and its subdirectories recursively.directory2
: The path to the second directory you want to search for duplicates in.
Example output:
./directory1/file1.txt
./directory2/file1.txt
./directory2/subdirectory/file1.txt
Use case 5: Search recursively and replace duplicates with hardlinks
Code:
fdupes -rH path/to/directory
Motivation:
In some cases, instead of deleting duplicate files, you may want to replace them with hardlinks. This can be useful when you want to conserve storage space and maintain access to the file from multiple locations.
Explanation:
fdupes
: The command to run the fdupes program.-r
: The option to search the directory and its subdirectories recursively.-H
: The option to replace duplicates with hardlinks.path/to/directory
: The path to the directory you want to search for duplicates in.
Example output:
./path/to/directory/file1.txt
./path/to/directory/file2.txt
Use case 6: Search recursively for duplicates and display interactive prompt to pick which ones to keep, deleting the others
Code:
fdupes -rd path/to/directory
Motivation:
Sometimes, you may want to manually review the duplicate files and decide which ones to keep and which ones to delete. The -rd
option provides an interactive prompt that allows you to make this choice for each set of duplicates.
Explanation:
fdupes
: The command to run the fdupes program.-r
: The option to search the directory and its subdirectories recursively.-d
: The option to prompt for which duplicates to keep and delete.path/to/directory
: The path to the directory you want to search for duplicates in.
Example output:
/path/to/directory/file1.txt
/path/to/directory/file2.txt
Delete duplicates? [1/2/q] q
Use case 7: Search recursively and delete duplicates without prompting
Code:
fdupes -rdN path/to/directory
Motivation:
If you want to skip the interactive prompt and automatically delete duplicates, you can use the -N
option. This is useful when you are confident in the decision to delete duplicates without manual intervention.
Explanation:
fdupes
: The command to run the fdupes program.-r
: The option to search the directory and its subdirectories recursively.-d
: The option to skip the interactive prompt.-N
: The option to automatically delete duplicates.path/to/directory
: The path to the directory you want to search for duplicates in.
Example output:
/path/to/directory/file1.txt
/path/to/directory/file2.txt
Deleted file: ./path/to/directory/file2.txt
Conclusion:
The fdupes command is a versatile tool for finding and managing duplicate files. By understanding the different use cases and options available, you can effectively search for duplicates, replace them with hardlinks, or delete them as per your requirements.