Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am doing clean up in my project. There are some modules in project which are being renamed or newly created or copied directly. In that process I have directly made copied renamed directory or created new directory. And now I want to delete old directory files. So I want to all files with their path which are having same name. (count > 2). There are 500+ of that type of files. That can be css, tpl, php or js files.


  1. Main/Games/troy.php
  2. Main/Games/Child Games/troy.php
  3. Main/Games/Sports/troy.php

If search is done on Main directory then search should return all 3 files and their paths.

So question is - How to find duplicate files i.e. with same name that exist in same parent directory?

I am using Windows SP2 and PHP. I need only search result no matter how it is provided. Anything like Command prompt, PHP logic, javascript logic, or any software giving results.

I have gone through some questions but they are not helpful to me.

  1. How to find duplicate files with same name but in different case that exist in same directory in Linux?
  2. how do i find all files with the same name in all subdirectories
share|improve this question

migrated from Nov 7 '12 at 13:08

This question came from our site for professional and enthusiast programmers.

why are the two linked ones not helpful to me and why do you have to do this with PHP? Why cant you just use the Windows Search Functions? Is that a programming question at all? – Gordon Nov 7 '12 at 11:52
The easiest way to match them would be to compare each files MD5. – CharliePrynn Nov 7 '12 at 11:53
@Gordon: Both are for linux. And having somewhat different requirement. Is there any Windows Search Functions for doing same? – Somnath Muluk Nov 7 '12 at 11:57
@CharliePrynn: I mean duplicate means files have same name only and no requirement of same content. – Somnath Muluk Nov 7 '12 at 11:58
2CharliePrynn files can be different, the only thing he asks for is for names to be equal. So md5 will not help – Salvador Dali Nov 7 '12 at 12:00
up vote 1 down vote accepted

Here is a function that recursively parses the specified directory tree and creates an associative array whose keys are the file names found in all subdirectories, and whose values are the corresponding paths. After running this function, all you have to do is iterate over the results and remove any file that has a higher appearance count than 1:

function scandir_recursive($dir, &$result = array()) {
    $dir = rtrim($dir, DIRECTORY_SEPARATOR);

    foreach (scandir($dir) as $node) {
        if ($node !== '.' and $node !== '..') {
            if (is_dir($dir . DIRECTORY_SEPARATOR . $node)) {
                scandir_recursive($dir . DIRECTORY_SEPARATOR . $node, $result);
            } else {
                $result[$node][] = $dir . DIRECTORY_SEPARATOR . $node;

    return $result;

share|improve this answer
Thanks.... it's returning all files with their existence in parent directory in array. Only require to check arrays count > 1.. – Somnath Muluk Nov 7 '12 at 12:49

please install any IDE like Netbeans or eclipse

there is an option called file search in that tab you have to specify the file name and then search it will provide you all the file of same names

or you can go for normal file search in windows

i don't know is there any way to get files in all directories using php

share|improve this answer
I can find duplicates of any file are there. I want to get list of files that are duplicate i.e have same name by giving only directory name. – Somnath Muluk Nov 7 '12 at 12:00
only one file be there in a folder of a single name – Akhilraj N S Nov 7 '12 at 12:05
There can be many files in sub-folders or that folder. @Akhilraj – Somnath Muluk Nov 7 '12 at 12:10

If you cannot find a way to do this with Windows tools alone and/or want to do that with PHP, you can do

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('/path/you/want/to/search/in')

$allFiles = array();

foreach ($iterator as $fileObject) {
    $fileName = $fileObject->getFilename();
    if (!isset($files[$fileName])) {
        $files[$fileName] = array();
    $files[$fileName][] = $fileObject->getPathname();

This will give you all files in and below that path keyed by their filename, e.g.

    'foo.txt' => array('/path/to/foo.txt'),
    'bar.txt' => array(

To get the dupes you just filter out all those arrays with just one element in them:

$dupes = array_filter(
    function(array $sameNamedFiles) {
        return count($sameNamedFiles) > 1;

The result will then be something like

    'bar.txt' => array(

If you want a "flat" array instead use

$dupes = array_reduce(
    function($dupes, array $sameNamedFiles) {
        if (count($sameNamedFiles) > 1) {
            $dupes = array_merge($dupes, $sameNamedFiles);
        return $dupes;

This will then result in something like


Note: the above is untested but should point you in the general direction.

share|improve this answer

You must log in to answer this question.

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