简体   繁体   English

比较两个文件夹上具有相似名称的文件内容

[英]Compare files content with similar names on two folders

I have two folders (I'll use database names as example): 我有两个文件夹(我将使用数据库名称作为示例):

  • MongoFolder/ MongoFolder /
  • CassandraFolder/ CassandraFolder /

These two folders have similar files inside like: 这两个文件夹内部具有相似的文件,例如:

  • MongoFolder/ MongoFolder /

    • MongoFile MongoFile
    • MongoStatus MongoStatus
    • MongoConfiguration MongoConfiguration
    • MongoPlugin MongoPlugin
  • CassandraFolder/ CassandraFolder /

    • CassandraFile CassandraFile
    • CassandraStatus CassandraStatus
    • CassandraConfiguration CassandraConfiguration

Those files have content also very similar, only changing the name of the database for example, so they all have code or configuration only changing the name Mongo for Cassandra . 这些文件的内容也非常相似,例如仅更改数据库的名称,因此它们都具有仅更改Mongo for Cassandra的名称的代码或配置。

How can I compare this two folders, so the result is the files missing from one to the other (for example the file CassandraPlugin for the CassandraFolder ) and also that the contents of the files alike, have to be similar, only changing the database name. 我如何比较这两个文件夹,所以结果是一个文件到另一个文件丢失(例如, CassandraFolder的文件CassandraPlugin ),而且文件的内容必须相似,只是更改数据库名称。

This will give you the names of the missing files (minus the database name): 这将为您提供丢失文件的名称(减去数据库名称):

find MongoFolder/ CassandraFolder/ | \
      sed -e s/Mongo//g -e s/Cassandra//g | sort | uniq -u

Output: 输出:

Folder/Plugin

the following provides a full diff, including missing files and changed content: 以下提供了完整的差异,包括丢失的文件和更改的内容:

cp -r CassandraFolder cmpFolder
# rename files
find cmpFolder -name "Cassandra*" -print | while read file; do
    mongoName=`echo "$file" | sed 's/Cassandra/Mongo/'`
    mv "$file" "$mongoName"
done
# fix content
find cmpFolder -type f -exec perl -pi -e 's/Cassandra/Mongo/g' {} \;

# inspect result
diff -r MongoFolder cmpFolder # or use a gui tool like kdiff3

I haven't tested this though, feel free fix bugs or to ask if something specific is unclear. 我还没有测试过,可以免费修复错误,或者询问是否有不清楚的具体问题。 Instead of mv you can also use rename but that's different on different flavours of linux. 除了mv您还可以使用rename但是在不同版本的linux上是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM