given two directory trees how to find which files are the same?
Well i found the answer myself. I had tried it before, but I thought it did not work.
diff -srq dir1/ dir2/ | grep identical
What -srq means? From diff --help :
-s --report-identical-files Report when two files are the same.
-r --recursive Recursively compare any subdirectories found.
-q --brief Output only whether files differ.
Given two directory trees, how can I find out which files differ by content?
Try:
diff --brief --recursive dir1/ dir2/
Or alternatively, with the short flags -qr
:
diff -qr dir1/ dir2/
If you also want to see differences for files that may not exist in either directory:
diff --brief --recursive --new-file dir1/ dir2/ # with long options
diff -qrN dir1/ dir2/ # with short flag aliases
Given two directory trees how to find which filenames are the same, considering only filenames satisfying a condition?
This is untested, but I'd try something like:
comm -12 <(cd dir1 && ls E*) <(cd dir2 && ls E*)
Basic idea:
Generate a list of filenames in
dir1
that satisfy our condition. This can be done withls E*
because we're only dealing with a flat list of files. For subdirectories and recursion we'd usefind
instead (e.g.find . -name 'E*' -type f
).Put the filenames in a canonical order (e.g. by sorting them). We don't have to do anything here because
E*
expands in sorted order anyway. Withfind
we might have to pipe the output intosort
first.Do the same thing to
dir2
.Only output lines that are common to both lists, which can be done with
comm -12
.comm
expects to be passed two filenames on the command line, so we use the<( ... )
bash feature to spawn a subprocess and connect its output to a named pipe; the name of the pipe can then be given tocomm
.
Diff files present in two different directories
You can use the diff
command for that:
diff -bur folder1/ folder2/
This will output a recursive diff that ignore spaces, with a unified context:
- b flag means ignoring whitespace
- u flag means a unified context (3 lines before and after)
- r flag means recursive
Find the files existing in one directory but not in the other
diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > difference1.txt
Explanation:
diff -r dir1 dir2
shows which files are only in dir1 and those only in dir2 and also the changes of the files present in both directories if any.diff -r dir1 dir2 | grep dir1
shows which files are only in dir1awk
to print only filename.
Related Topics
How to Count Number of Unique Values of a Field in a Tab-Delimited Text File
How to Speed Up Linux Kernel Compilation
How to Monitor Data on a Serial Port in Linux
How to Add My Own Software to a Buildroot Linux Package
In Linux, What Do All the Values in the "Top" Command Mean
How to Skip Saturday and Sunday in a Cron Expression
How to Open a "-" Dashed Filename Using Terminal
How to Determine the Current Ip from a Known MAC Address
Error When Trying to Run .Asm File on Nasm on Ubuntu
Installed Clang++3.6 on Ubuntu, Can't Select as Alternative
Get Current Time in Hours and Minutes
How to Set Up Autocompletion for Git Commands
How to Open Sublime Text 2 Files from the Command Line in Linux to a Tab, Not a New Window
Unix Command to List Files Containing String But *Not* Containing Another String
Setting Node_Env for Node.Js + Expressjs Application as a Daemon Under Ubuntu