这可能很简单,但我无法弄清楚。我有一个这样的目录结构(dir2 在 dir1 中):
/dir1
/dir2
|
--- file1
|
--- file2
Run Code Online (Sandbox Code Playgroud)
以这样的方式“扁平化”这个导演结构的最佳方法是在 dir1 而不是 dir2 中获取 file1 和 file2。
我正在尝试使用 .bash 文件读取 bash 中命令的输出while loop。
while read -r line
do
echo "$line"
done <<< $(find . -type f)
Run Code Online (Sandbox Code Playgroud)
我得到的输出
ranveer@ranveer:~/tmp$ bash test.sh
./test.py ./test1.py ./out1 ./test.sh ./out ./out2 ./hello
ranveer@ranveer:~/tmp$
Run Code Online (Sandbox Code Playgroud)
在这之后我试过了
$(find . -type f) |
while read -r line
do
echo "$line"
done
Run Code Online (Sandbox Code Playgroud)
但它产生了一个错误test.sh: line 5: ./test.py: Permission denied。
那么,我如何逐行阅读它,因为我认为目前它正在一次吞食整行。
所需输出:
./test.py
./test1.py
./out1
./test.sh
./out
./out2
./hello
Run Code Online (Sandbox Code Playgroud) GNU find 的手册页指出:
Run Code Online (Sandbox Code Playgroud)-exec command ; [...] The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a `\') or quoted to protect them from expansion by the shell.
那是从人到find(GNU findutils)4.4.2。
现在我用 bash 和 dash 测试了这个,两者都不需要{}被屏蔽。这是一个简单的测试:
find /etc -name "hosts" -exec …Run Code Online (Sandbox Code Playgroud) 有没有办法在逻辑上组合使用find-exec调用的两个 shell 命令?
例如,打印出所有包含字符串 foo 及其出现的.csv文件,我想这样做:
find . -iname \*.csv -exec grep foo {} && echo {} \;
Run Code Online (Sandbox Code Playgroud)
但是bash抱怨“缺少'-exec'的参数”
像下面这样的东西就是我所追求的,但我的代码不起作用,无论我如何逃避{}和+ ;
find ./ -maxdepth 1 -type d -name '.*' -exec \
find {} -maxdepth 1 -type f -name '*.ini' -exec \
md5sum \{\} \\; \;
Run Code Online (Sandbox Code Playgroud)
在看到这个Unix-&-Linux question 后,我发现下面的代码有效,但它并没有嵌套find,我怀疑有更好的方法来完成这项特定的工作。
find ./ -maxdepth 1 -type d -name '.*' \
-exec bash -c 'for x; do
find "$x" -maxdepth 1 -type f -name "*.ini" \
-exec md5sum \{\} \;; \
done' _ {} \+
Run Code Online (Sandbox Code Playgroud)
有什么方法可以find -exec在不需要调用 shell(如上)的情况下嵌套,以及所有古怪的引用和转义约束?
或者这可以直接在单个 find 命令中完成,使用它的许多参数的混合?