bash 在bash中的每个子目录中创建所有文件的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16365938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create list of all files in every subdirectories in bash
提问by Maj0r
I trying to write script in bash which will loop over all subdirectories, starring from given path, and will create list with md5 sums of all files in current directory.
我尝试在 bash 中编写脚本,该脚本将循环遍历所有子目录,从给定路径开始,并将创建包含当前目录中所有文件的 md5 总和的列表。
I need something like ls -R, but I'm not sure how to start
我需要类似的东西ls -R,但我不知道如何开始
回答by jbr
回答by technosaurus
There is also a program called tree, but you can simulate it with only shell builtins:
还有一个名为 tree 的程序,但您可以仅使用 shell 内置程序来模拟它:
#!/bin/sh
DIR=${1:-`pwd`}
SPACING=${2:-|}
cd $DIR
for x in * ; do
[ -d "$DIR/$x" ] && echo "$SPACING\`-{$x" && ##代码## "$DIR/$x" "$SPACING " || \
echo "$SPACING $x : MD5=" && md5sum "$DIR/$x"
done
Note it requires a full path argument (or none for current directory)
请注意,它需要一个完整的路径参数(或当前目录不需要)
Its not as fast as find (though there are plenty of ways to speed it up that make the code more complicated to follow), but gives a graphical representation of the tree structure.
You can modify it to not follow symlinks by adding- && [ ! -L "$DIR/$x" ]or to only list directories: remove the || echo $SPACING $x
它不如 find 快(尽管有很多方法可以加快速度,使代码更复杂),但提供了树结构的图形表示。您可以通过添加&& [ ! -L "$DIR/$x" ]或仅列出目录来修改它以不遵循符号链接:删除|| echo $SPACING $x

