Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:23:51+00:00 2026-06-13T05:23:51+00:00

Need to compare two directories and check through every file in both directories for

  • 0

Need to compare two directories and check through every file in both directories for files of the same name, if the same name occurs you check to see if the files have the same content, if so print > file <. If the file has the same name but not the same content < file > If there is a file that’s not in directory 2 but in directory 1 <<< file1, and likewise >>> file2 for a file in dir 2 but not dir 1.
I have been having trouble, my code doesn’t even compare when I test to see if the files are equal in name.

#!/usr/bin/perl -w 
use File::Basename;

@files1 = `/usr/bin/find $ARGV[0] -print`;
chop @files1;
@files2 = `/usr/bin/find $ARGV[1] -print`;
chop @files2;

here: 
for ($i=1; @files1 >= $i; $i++) {

    for ($x=1; @files2 >= $x; $x++) {

        $file1 = basename($files1[$i]); 
        $file2 = basename($files2[$x]); 

        if ($file1 eq $file2) {

            shift @files1;
            shift @files2;

            $result = `/usr/bin/diff -q $files1[$i] $files2[$x]`;
            chop $result;

            if ($result eq "Files $files1[$i] and $files2[$x] differ") { 

                print "< $file1 >\n";
                next here;
            } 
            else { 

                print "> $file1 <\n";
            }
        }
        else  { 

            if ( !-e "$files1[$i]/$file2") { print ">>> $file2\n";}
            unless ( -e "$files2[$x]/$file1") { print "<<< $file1\n";}
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:23:52+00:00Added an answer on June 13, 2026 at 5:23 am

    Try using this :

    diff -aqr /dir1 /dir2
    

    or :

    #!/bin/bash
    
    for f;
        for g; do
            [[ "$f" != "$g" ]] &&
                cmp &>/dev/null "$f" "$g" || echo "$f is different of $g"
        done
    done
    

    USE this

    ./script dir1/* dir2/*
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two directories with the same list of files. I need to compare
I need to compare two text files (in different directories) to see if they
Im totally newbie in shell script. Im need compare file name in two directories
I need to compare two csv files, they both contain a unique ID column
We need a script that will compare two directories of files and for each
I need to compare two files based on datetime. I need to check whether
I need to compare two different files of the instance File in Java and
I need to compare two XML files with wildcard characters in those. I have
I need to compare two bitmaps. One bitmap is loaded from a file, the
When you need to compare two tables to see what the differences are, are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.