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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:20:48  来源:igfitidea点击:

create list of all files in every subdirectories in bash

linuxbashlistloopssubdirectory

提问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

There is a very easy way of doing this with find:

使用find有一种非常简单的方法可以做到这一点:

find . -type f -exec md5 {} \;

The command finds all files (-type f), and executes the command md5 on each file (-exec md5 {} \;).

该命令查找所有文件 ( -type f),并对每个文件 ( -exec md5 {} \;)执行命令 md5 。

回答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