遍历目录的 Bash 脚本
我有一个包含 XML 文件和其他目录的目录。所有其他目录都有 XML 文件和子目录等。
我需要编写一个脚本(可能是 bash),为每个目录运行 java XMLBeautifier 目录 ,并且由于我在 bash 脚本编写方面的技能有点垃圾,所以我会真的很感谢你的帮助。
I have a directory with XML files and other directories. All other directories have XML files and subdirectories, etc.
I need to write a script (bash probably) that for each directory runs java XMLBeautifier directory
and since my skills at bash scripting are a bit rubbish, I would really appreciate a bit of help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您必须获取目录,您可以使用:
只需将其通过管道传输到您的程序,如下所示:
另一种方法是使用
find
获取所有文件并将其通过管道传输到您的程序如下所示:这将从当前目录中获取所有
.xml
文件,并递归地遍历所有子目录。然后用xargs
将它们一一交给java XMLBeautifier
。If you have to get the directories, you can use:
just pipe this to your program like this:
Another approach would be to get all the files with
find
and pipe that to your program like this:This takes all
.xml
files from the current directory and recursively through all subdirectories. Then hands them one by one over withxargs
tojava XMLBeautifier
.Find 是一个很棒的工具...但是,如果您不确定文件名,但对这些 xml 文件包含的内容有一个模糊的概念,那么您可以使用 grep。
例如,如果您确定所有 xml 文件都包含短语“正确的 xml 文件”(您可以将此短语更改为您认为合适的内容),则在命令行中运行以下命令...
-I 选项搜索文件并在模式匹配时返回文件名
-R 选项递归到达您的目录
-w 确保给定的模式整体匹配,而不是单个单词匹配
希望这有帮助!
Find is an awesome tool ... however, if you are not sure of the file name but have a vague idea of what those xml file contains then you can use grep.
For instance, if you know for sure that all your xml files contains a phrase "correct xml file" (you can change this phrase to what you feel appropriate) then run the following at your command line ...
-I option searches the file and returns the file name when pattern is matched
-R option reaches your directory recursively
-w ensure that the pattern given matches on the whole and not single word individually
Hope this helps!