I'm looking to run a Linux command that will recursively compare two directories and output only the file names of what is different. This includes anything that is present in one directory and not the other or vice versa, and text differences.
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Error building gcc 4.8.3 from source: libstdc++.so
- Why should we check WIFEXITED after wait in order
- Null-terminated string, opening file for reading
If you want to get a list of files that are only in one directory and not their sub directories and only their file names:
If you want to recursively list all the files and directories that are different with their full paths:
This way you can apply different commands to all the files.
For example I could remove all the files and directories that are in dir1 but not dir2:
On my linux system to get just the filenames
From the diff man page:
Example command:
Example output (depends on locale):
You can also use rsync
The approach of running
diff -qr old/ new/
has one major drawback: it may miss files in newly created directories. E.g. in the example below the filedata/pages/playground/playground.txt
is not in the output ofdiff -qr old/ new/
whereas the directorydata/pages/playground/
is (search for playground.txt in your browser to quickly compare). I also posted the following solution on Unix & Linux Stack Exchange, but I'll copy it here as well:To create a list of new or modified files programmatically the best solution I could come up with is using rsync, sort, and uniq:
Let me explain with this example: we want to compare two dokuwiki releases to see which files were changed and which ones were newly created.
We fetch the tars with wget and extract them into the directories
old/
andnew/
:Running rsync one way might miss newly created files as the comparison of rsync and diff shows here:
yields the following output:
Running rsync only in one direction misses the newly created files and the other way round would miss deleted files, compare the output of diff:
yields the following output:
Running rsync both ways and sorting the output to remove duplicates reveals that the directory
data/pages/playground/
and the filedata/pages/playground/playground.txt
were missed initially:yields the following output:
rsync
is run with theses arguments:-r
to "recurse into directories",-c
to also compare files of identical size and only "skip based on checksum, not mod-time & size",-n
to "perform a trial run with no changes made", and--out-format="%n"
to "output updates using the specified FORMAT", which is "%n" here for the file name onlyThe output (list of files) of
rsync
in both directions is combined and sorted usingsort
, and this sorted list is then condensed by removing all duplicates withuniq