how to compare two directory , the files are the same
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how to compare two directory , the files are the same
I have two directory , there are many files inside it , if I would like to compare the files if all files creation date , size are the same , would advise how to do it ?
I have two directory , there are many files inside it , if I would like to compare the files if all files creation date , size are the same , would advise how to do it ?
Simple solution #1:
Code:
cd <dir1>
ls -l > /tmp/dir1
cd <dir2>
ls -l > /tmp/dir2
diff /tmp/dir? | less
Another solution could be rsync in dry-run mode:
Code:
rsync -avn --del <dir1>/ <dir2> | less
Note the / after dir1 as you want the contents of that directory and not the dir itself.
As rsync compares files on date/time and size everything it wants to COPY is either newer in dir1 or non-existent in dir2, everything it wants to remove doesn't exist in dir1 but _does_ in dir2.
What I do not really recommend (but I have checked):
Code:
cd <dir1>
find . -type f -ls | sort > /tmp/dir1.txt
cd <dir2>
find . -type f -ls | sort > /tmp/dir2.txt
# and finally you can
diff /tmp/dir1.txt /tmp/dir2.txt
I have two directory , there are many files inside it , if I would like to compare the files if all files creation date , size are the same , would advise how to do it ?
AFAIK, it all depends on the filesystem eventually but generally creation dates are not systematic at all. Linux is rather used to deal with last access, modification and status change (permissions...) times. NB: rsync in dry-run doesn't seem to show the differences file by file. It just builds a file listing and general stats about the transfer to be run.
NB: rsync in dry-run doesn't seem to show the differences file by file. It just builds a file listing and general stats about the transfer to be run.
rsync doesn't compare files (unless you use the -c option, which makes it MUCH slower).
It only compares date/time and size of the two, which, I believe, is what the OP requested.
Comparing directory entries (with ls or find) goes on date/time and size too, you never compare the files themselves.
The one thing to watch out with rsync is that it doesn't treat both directories equivalent.
The first one is the "source" directory, which is treated as the master, the 2nd is "destination".
The difference is most striking in
a) the treatment of a trailing / (contents vs directory itself)
Only important for the source, trailing slash in destination is ignored
b) equal size files that do NOT have the same date/time
if "source" is newer, the file is copied, but if
"destination" is newer, the date/time OF that file is only adjusted, the files
are assumed to be equal, with a clock difference.
One final note: Linux file systems do NOT have a "creation" date, the one you normally see is the "last modified" one.
yes, it this. Just some comments: fd can be faster than find (which is more or less irrelevant here).
Code:
find . -type f -print0 | while IFS= read -r -d '' file; do
is more generic, can be important if you have "strange" chars in your filenames.
Also you might add some logging (something similar):
Code:
...
do
cmp "$f" /path/to/dir2/"$f" || echo "$f" is not the same
done >> logfile
and also you may try to use background processes if you want to speed it up.
Code:
function check()
{
cmp "$1" /path/to/dir2/"$1" || echo "$f" is not the same # << put this into background
}
cd /path/to/dir1 && find . -type f -exec check "{}" \;
and also you may try to use background processes if you want to speed it up.
Code:
function check()
{
cmp "$1" /path/to/dir2/"$1" || echo "$f" is not the same # << put this into background
}
cd /path/to/dir1 && find . -type f -exec check "{}" \;
Are you sure there is no need of final "&" somewhere in the function?
@pan64: ^ Additionally, does your suggestion really work from your side? find doesn't seem to manage in retrieving the shell function this way ("find: check: No such file or directory")... You probably need the following instead:
Code:
function check()
{
cmp "$1" /path/to/dir2/"$1" || echo "$f" is not the same # << put this into background
}
export -f check
cd /path/to/dir1 && find . -type f -exec bash -c 'check "$0"' "{}" \;
@pan64: ^ Additionally, does your suggestion really work from your side? find doesn't seem to manage in retrieving the shell function this way ("find: check: No such file or directory")... You probably need the following instead:
Code:
function check()
{
cmp "$1" /path/to/dir2/"$1" || echo "$f" is not the same # << put this into background
}
export -f check
cd /path/to/dir1 && find . -type f -exec bash -c 'check "$0"' "{}" \;
No, I did not check it and also I did not complete that. It was just an idea.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.