shell – 递归搜索IBMi IFS上目录中每个文件的目录
我正在尝试编写两个(编辑:
shell)脚本,并且遇到了一些困难.我将解释目的,然后提供脚本和当前输出.
1:递归获取目录中每个文件名的列表.然后在该目录中搜索每个文件名的所有文件的内容.应返回每次出现的特定文件名的路径,文件名和行号. 2:递归获取目录中每个文件名的列表.然后在目录中搜索每个文件名的所有文件的内容.应该返回目录中任何文件中找不到的每个文件的路径和文件名. 我最终想要使用脚本2来查找和删除(实际上将它们移动到另一个目录以进行存档)网站中未使用的文件.然后我想使用脚本1来查看每个事件并过滤任何重复的文件名. 我知道我可以让脚本2在运行时移动每个文件而不是第二步,但我想在执行任何操作之前确认脚本功能正确.我确认它运行正常后我会修改它. 我目前正在strqsh的IMBi系统上测试它. 我的测试文件夹结构是: scriptTest ---subDir1 ------file4.txt ------file5.txt ------file6.txt ---subDir2 ------file1.txt ------file7.txt ------file8.txt ------file9.txt ---file1.txt ---file2.txt ---file3.txt 我在其中一些包含现有文件名的文件中有文本. 这是我目前的脚本1: #!/bin/bash files=`find /www/Test/htdocs/DLTest/scriptTest/ ! -type d -exec basename {} ;` for i in $files do grep -rin $i "/www/Test/htdocs/DLTest/scriptTest" >> testReport.txt; done 现在它正常运行,除了提供具有匹配项的文件的路径. grep默认不返回文件路径吗? 我在脚本2的距离稍远一点: #!/bin/bash files=`find /www/Test/htdocs/DLTest/scriptTest/ ! -type d` for i in $files do #split $i on '/' and store into an array IFS='/' read -a array <<< "$i" #get last element of the array echo "${array[-1]}" #perform a grep similar to script 2 and store it into a variable filename="grep -rin $i "/www/Test/htdocs/DLTest/scriptTest" >> testReport.txt;" #Check if the variable has anything in it if [ $filename = "" ] #if not then output $i for the full path of the current needle. then echo $i; fi done 我不知道如何将字符串$i拆分为数组.我一直在第6行得到错误 001-0059 Syntax error on line 6: token redirection not expected. 我打算在实际的Linux发行版上尝试这个,看看我是否得到了不同的结果. 我很欣赏任何先进的见解. 解决方法
介绍
这不是一个完整的解决方案,因为我不是100%确定我理解你要做的事情.但是,以下内容包含一些解决方案,您可以将它们拼接在一起以执行您想要的操作. 创建测试工具 cd /tmp mkdir -p scriptTest/subDir{1,2} mkdir -p scriptTest/subDir1/file{4,5,6}.txt mkdir -p scriptTest/subDir2/file{1,8,8}.txt touch scriptTest/file{1,2,3}.txt 查找和删除重复项 在最一般意义上,您可以使用find的-exec标志或Bash循环来对文件运行grep或其他比较.但是,如果您要做的只是删除重复项,那么您可能只是更好地使用fdupes或duff实用程序来识别(并选择性地删除)具有重复内容的文件. 例如,假设测试语料库中的所有.txt文件都是零长度重复项,请考虑以下duff和fdupes示例 达夫 Duff有更多选项,但不会直接删除文件.你可能需要使用像duff -e0 * |这样的命令xargs -0 rm删除重复项.要使用默认比较查找重复项: $duff -r scriptTest/ 8 files in cluster 1 (0 bytes,digest da39a3ee5e6b4b0d3255bfef95601890afd80709) scriptTest/file1.txt scriptTest/file2.txt scriptTest/file3.txt scriptTest/subDir1/file4.txt scriptTest/subDir1/file5.txt scriptTest/subDir1/file6.txt scriptTest/subDir2/file1.txt scriptTest/subDir2/file8.txt fdupes 该实用程序提供了以各种方式直接删除重复项的功能.一种方法是调用fdupes. – 一旦你确信你已经准备好继续下去,就会 – 删除 – 无效.但是,要查找重复列表: $fdupes -R scriptTest/ scriptTest/subDir1/file4.txt scriptTest/subDir1/file5.txt scriptTest/subDir1/file6.txt scriptTest/subDir2/file1.txt scriptTest/subDir2/file8.txt scriptTest/file1.txt scriptTest/file2.txt scriptTest/file3.txt 获取所有文件的列表,包括非重复项 $find scriptTest -name *.txt scriptTest/file1.txt scriptTest/file2.txt scriptTest/file3.txt scriptTest/subDir1/file4.txt scriptTest/subDir1/file5.txt scriptTest/subDir1/file6.txt scriptTest/subDir2/file1.txt scriptTest/subDir2/file8.txt 然后,您可以使用find的-exec {}功能对每个文件执行操作,或者只使用支持–recursive –files-with-matches标记的grep来查找具有匹配内容的文件. 将查找结果作为数组传递给Bash循环 或者,如果您确定文件名中没有空格,则还可以使用Bash数组将文件存储到可以在Bash for循环中迭代的变量中.例如: files=$(find scriptTest -name *.txt) for file in "${files[@]}"; do : # do something with each "$file" done 像这样的循环通常较慢,但如果您正在做一些复杂的事情,可能会为您提供额外的灵活性.因人而异. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- .net中窗体之间的数据交换总结
- 二、Axis2的简单WebService示例
- twitter-bootstrap – Bootstrap 4响应式导航栏垂直
- Building a Recipe Search Site with Angular and Elastics
- angularjs – 使用ui.bootstrap导致轮播的问题
- angular2 路由
- angularjs – 是否可以使用ng-include而不使用Web服务器?
- bash – ImageMagick:是否可以删除“裁剪标记”?
- Shell脚本比较两个网段是否存在包含与被包含的关系
- angularjs – 重新加载角度app后没有index.html