我想在每个类似于ls -sR
输出的目录中获取文件1中但不在文件2中的丢失文件。我在下面显示了file1和file2的格式,在右边显示了预期的输出。I file1和file2存在desktop.ini
,但两个文件中的大小不同,因此显示在输出中。文件1和文件2的内容是在两个不同的设备中获得的,目前我的选择是使用这些文件进行比较。
file1.txt | file2.txt | Missing files in file1 but not in file2
==========================================================================================================
./AB/FTP: | ./AB/FTP: | ./AB/FTP:
4 FileZilla.lnk | 4 FileZilla.lnk | desktop.ini "different in size"
7 desktop.ini | 1 desktop.ini |
| | ./BX/MS Office:
./BX/MS Office: | ./BX/MS Office: | OneNote 2013.lnk
4 Excel 2013.lnk | 4 Excel 2013.lnk | Outlook 2013.lnk
4 OneNote 2013.lnk | 4 PowerPoint 2013.lnk |
4 Outlook 2013.lnk | 4 Word 2013.lnk | ./D/R/Web:
4 PowerPoint 2013.lnk | 1 desktop.ini | Google Chrome.lnk
4 Word 2013.lnk | | Internet Explorer.lnk
1 desktop.ini | | desktop.ini
| |
./D/R/Web: | |
4 Google Chrome.lnk | |
4 Internet Explorer.lnk | |
1 desktop.ini | |
我尝试过使用diff
,但它似乎不是diff命令所需的输入类型,或者我没有正确解释输出。
$ diff -u file1.txt file2.txt
--- file1.txt 2022-01-22 13:08:54.855275200 -0400
+++ file2.txt 2022-01-22 13:09:05.785816800 -0400
@@ -1,16 +1,9 @@
- ./AB/FTP:
-4 FileZilla.lnk
-7 desktop.ini
-
- ./BX/MS Office:
-4 Excel 2013.lnk
-4 OneNote 2013.lnk
-4 Outlook 2013.lnk
-4 PowerPoint 2013.lnk
-4 Word 2013.lnk
-1 desktop.ini
-
- ./D/R/Web:
-4 Google Chrome.lnk
-4 Internet Explorer.lnk
-1 desktop.ini
No newline at end of file
+ ./AB/FTP:
+4 FileZilla.lnk
+1 desktop.ini
+
+ ./BX/MS Office:
+4 Excel 2013.lnk
+4 PowerPoint 2013.lnk
+4 Word 2013.lnk
+1 desktop.ini
No newline at end of file
提前感谢您的帮助。
diff只提供逐行比较,当你搜索三维比较(目录、大小和文件名(时,你需要通过一些字典和循环来完成。如果文件列表太长(需要很长时间(,或者你确实想用其他编程语言编写复杂的代码,那就去吧。
第一步,我们需要按目录分离数据。
declare -a dicts1
while read line; do
if [[ $line =~ ^./.* ]]; then
currentdict="$(echo $line | sed 's/./_/g;s///_/g;s/s/_/g;s/:/_/g')"
declare -A $currentdict
dicts1=("${dicts1[@]}" "$currentdict");
else
filename=$(echo $line | awk '{print $2}')
filesize=$(echo $line | awk '{print $1}')
command="$currentdict[$filename]=$filesize"
eval $command
fi
done < $HOME/file1.txt
现在,我们为文件1中的每个目录提供了关联数组,重做文件2的代码并迭代所有字典:
for dictname in ${dicts1[@]};do
command="echo ${!$dictname[@]}"
for i in $(eval $command);do
c2="echo ${$dictname[$i]}"
size_value=$(eval $c2)
filename=$i
#INSERT CONDITIONS HERE...
done
done
然后,您可以在字典中搜索file2的精确键值对。
顺便说一下,您需要将文件名中的每个特殊字符都更改为可用于变量名的字符。字典名称的示例sed命令可以帮助您做到这一点。