Linux 您如何仅针对特定类型的文件区分目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3775377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:36:53  来源:igfitidea点击:

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

linuxbash

提问by de_3

I have a question about the diff command if I want a recursive directory diff but only for a specific file type, how to do that?

我有一个关于 diff 命令的问题,如果我想要一个递归目录 diff 但只针对特定的文件类型,该怎么做?

I tried using the exclude option but can only use one pattern only:

我尝试使用 exclude 选项,但只能使用一种模式:

$ diff /destination/dir/1 /destination/dir/2 -r -x *.xml

with the command I can only exclude xml file type, even though there are files in the folder image type (png, gif, jpg), txt, php, etc

使用命令我只能排除 xml 文件类型,即使文件夹图像类型 ( png, gif, jpg), txt, php, 等中有文件

how to diff only certain file types.

如何仅区分某些文件类型。

采纳答案by Paused until further notice.

You can specify -xmore than once.

您可以指定-x多次。

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):

从比较目录部分info diff(在我的系统上,我必须这样做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'.

要在比较目录时忽略某些文件,请使用“-x PATTERN”或“--exclude=PATTERN”选项。此选项忽略基本名称与外壳模式 PATTERN 匹配的任何文件或子目录。与 shell 不同,文件名基址开头的句点与模式开头的通配符匹配。您应该将 PATTERN 括在引号中,以便 shell 不会扩展它。例如,选项-x '*.[ao]'忽略名称以 '.a' 或 '.o' 结尾的任何文件。

如果您多次指定此选项,则会累积。例如,使用选项-x 'RCS' -x '*,v' 将忽略基本名称为 'RCS' 或以 ',v' 结尾的任何文件或子目录。

回答by jamesbtate

Taken from ( a version of) the man page:

取自(一个版本)手册页:

-x PAT  --exclude=PAT
  Exclude files that match PAT.

-X FILE    --exclude-from=FILE
  Exclude files that match any pattern in FILE.

So it looks like -xonly accepts one pattern as you report but if you put all the patterns you want to exclude in a file (presumably one per line) you could use the second flag like so:

所以它看起来-x只接受你报告的一种模式,但如果你把所有你想排除的模式放在一个文件中(大概每行一个),你可以像这样使用第二个标志:

$ diff /destination/dir/1 /destination/dir/2 -r -X exclude.pats

where exclude.pats is:

其中 exclude.pats 是:

*.jpg
*.JPG
*.xml
*.XML
*.png
*.gif

回答by Rafiz

In case you find it convenient, you could use the following Makefile. Just run: "make patch"

如果您觉得方便,可以使用以下Makefile. 只需运行:“制作补丁”

#Makefile for patches

#Exlude following file endings
SUFFIX += o
SUFFIX += so
SUFFIX += exe
SUFFIX += pdf
SUFFIX += swp

#Exlude following folders
FOLDER += bin
FOLDER += lib
FOLDER += Image
FOLDER += models

OPTIONS = Naur

patch: 
    rm test.patch
    diff -$(OPTIONS) \
    $(foreach element, $(SUFFIX) , -x '*.$(element)') \
    $(foreach element, $(FOLDER) , -x '$(element)*') \
        org/ new/ > test.patch  

unpatch: 
    rm test.unpatch
    diff -$(OPTIONS) \
    $(foreach element, $(SUFFIX) , -x '*.$(element)') \
    $(foreach element, $(FOLDER) , -x '$(element)*') \
    new/ org/ > test.unpatch

回答by Jerry Miller

The lack of a complementary --include makes it necessary to use such convoluted heuristic patterns as

缺乏互补的 --include 使得有必要使用这种复杂的启发式模式,如

*.[A-Zb-ik-uw-z]*

to find (mostly) java files!

查找(主要是)java 文件!

回答by Alex

If you want to differ sources and keep it simple:

如果您想要不同的来源并保持简单:

diff -rqx "*.a" -x "*.o" -x "*.d" ./PATH1 ./PATH2 | grep "\.cpp " | grep "^Files"

Remove the last grep if you want to get the files which exist in only one of the paths.

如果您想获取仅存在于其中一个路径中的文件,请删除最后一个 grep。

回答by Sérgio

The lack of a complementary --include ... .

缺乏互补的 --include ... 。

We can do one workaround, a exclude file with all files but what we want include. So we create file1with a find all files which don't have extensions that we want include, sedcatch the filename and is just :

我们可以做一种解决方法,一个包含所有文件但我们想要包含的文件的排除文件。所以我们创建file1了一个 find 所有没有我们想要包含的扩展sed名的文件,捕获文件名,只是:

diff --exclude-from=file1  PATH1/ PATH2/

For example:

例如:

find  PATH1/ -type f | grep --text -vP "php$|html$" | sed 's/.*\///' | sort -u > file1 
diff PATH1/ PATH2/ -rq -X file1 

回答by Alex Harui

You can also use find with -exec to call diff:

您还可以使用带有 -exec 的 find 来调用 diff:

cd /destination/dir/1
find . -name *.xml -exec diff {} /destination/dir/2/{} \;

回答by Mikhail Golubitsky

I used the following command to find the diff of all *.tmplfiles between DIR1and DIR2. In my case this didn't yield any false positives, but it may for you, depending on the contents of your DIRS.

我使用以下命令查找和*.tmpl之间所有文件的差异。在我的情况下,这不会产生任何误报,但它可能适合您,具体取决于您的 DIRS 的内容。DIR1DIR2

diff --brief DIR1 DIR2 | grep tmpl

diff --brief DIR1 DIR2 | grep tmpl

回答by Cez

Whilst it does not avoid the actual diffof other files, if your goal is to produce a patch file, or similar then you can use filterdifffrom the patchutilspackage, e.g. to patch only your .pychanges:

虽然它不会避免diff其他文件的实际情况,但如果您的目标是生成补丁文件或类似文件,那么您可以filterdiffpatchutils包中使用,例如仅修补您的.py更改:

diff -ruNp /path/1 /path/2 | filterdiff -i "*.py" | tee /path/to/file.patch