How to Diff a Directory for Only Files of a Specific Type

How do you diff a directory for only files of a specific type?

You can specify -x more than once.

diff -x '*.foo' -x '*.bar' -x '*.baz' /destination/dir/1 /destination/dir/2

From the Comparing Directories section of info diff (on my system, I have to do info -f /usr/share/info/diff.info.gz):

To ignore some files while comparing directories, use the '-x
PATTERN' or '--exclude=PATTERN' option. This option ignores any files
or subdirectories whose base names match the shell pattern PATTERN.
Unlike in the shell, a period at the start of the base of a file name
matches a wildcard at the start of a pattern. You should enclose
PATTERN in quotes so that the shell does not expand it. For example,
the option -x '*.[ao]' ignores any file whose name ends with '.a' or
'.o'.

This option accumulates if you specify it more than once. For
example, using the options -x 'RCS' -x '*,v' ignores any file or
subdirectory whose base name is 'RCS' or ends with ',v'.

Only include files that match a given pattern in a recursive diff

Perhaps this is a bit indirect, but it ought to work. You can use find to get a list of files that don't match the pattern, and then "exclude" all those files:

find a b -type f ! -name 'crazy' -printf '%f\n' | diff -r a b -X -

The -X - will make diff read the patterns from stdin and exclude anything that matches. This should work provided your files don't have funny chars like * or ? in their names. The only downside is that your diff won't include the find command, so the listed diff command is not that useful.

(I've only tested it with GNU find and diff).

EDIT:

Since only non-GNU find doesn't have -printf, sed could be used as an alternative:

find a b -type f ! -name '*crazy*' -print | sed -e 's|.*/||' | diff -X - -r a b

That's also assuming that non-GNU diff has -X which I don't know.

diff to output only the file names

From the diff man page:

-q   Report only whether the files differ, not the details of the differences.

-r   When comparing directories, recursively compare any subdirectories found.

Example command:

diff -qr dir1 dir2

Example output (depends on locale):

$ ls dir1 dir2
dir1:
same-file different only-1

dir2:
same-file different only-2
$ diff -qr dir1 dir2
Files dir1/different and dir2/different differ
Only in dir1: only-1
Only in dir2: only-2

How do I diff only certain files?

patchutils provides filterdiff that can do this:

diff -ur old/ new/ | filterdiff -I filelist > patchfile

It is packaged for several linux distributions

diff compare directories by filename only

In PowerShell, if you want to know which file names are unique to /dir1, use a Compare-Object call, followed by reducing those file names to their base name (file name without extension), weeding out duplicates, and sorting via Sort-Object

Compare-Object -PassThru -Property Name (Get-ChildItem -File /dir1) (Get-ChildItem -File /dir2) |
Where-Object SideIndicator -eq '<=' |
ForEach-Object BaseName |
Sort-Object -Unique

Note: The assumption is that both Get-ChildItem calls return at least one file-info object, otherwise the Compare-Object call will fail - guard against that with if statements, if necessary.

How to compare two directories using diff while ignoring non-existing files?

It prints a bunch of lines like

Only in dir1/blah: blah

right? So just throw them away with grep.

LC_ALL=C diff ... | grep -v '^Only in'

The LC_ALL=C is to make sure that the standard "Only in" message will be printed, not any translation.

How can I grep recursively, but only in files with certain extensions?

Just use the --include parameter, like this:

grep -inr --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.example

That should do what you want.

To take the explanation from HoldOffHunger's answer below:

  • grep: command

  • -r: recursively

  • -i: ignore-case

  • -n: each output line is preceded by its relative line number in the file

  • --include \*.cpp: all *.cpp: C++ files (escape with \ just in case you have a directory with asterisks in the filenames)

  • ./: Start at current directory.

How can I diff two directories in bash recursively for only 1 file name?

You can try this :

find /develop -type f -name schema.json -exec bash -c\
'diff "$1" "/us-prod${1#/develop}"' _ {} \;


Related Topics



Leave a reply



Submit