1:获取目录中每个文件名的递归列表。然后搜索该目录中所有文件的内容以查找每个文件名。应返回特定文件名的每个出现的路径、文件名和行号。
2:获取目录中每个文件名的递归列表。然后搜索目录中所有文件的内容以查找每个文件名。应返回未在任何目录文件中找到的每个文件的路径和文件名。
我最终想使用脚本2在网站中查找并删除(实际上是将它们移动到另一个目录进行归档)未使用的文件。然后,我想使用脚本1查看每个出现并过滤任何重复的文件名。
我知道我可以使脚本2在运行时移动每个文件而不是作为第二步,但我想在执行任何操作之前确认脚本是否正确运行。确认其功能正确后,我会进行修改。
我目前正在IMBi系统的strqsh上进行测试。
我的测试文件夹结构如下:
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发行版上尝试这个,看看是否会得到不同的结果。”
“提前感谢任何见解。”
-L
的意义与“真正的”Unix衍生命令行 shell中完全不同,所以你就明白了。 - John Y