相关疑难解决方法(0)

展平嵌套目录

这可能很简单,但我无法弄清楚。我有一个这样的目录结构(dir2 在 dir1 中):

/dir1
    /dir2 
       |
        --- file1
       |
        --- file2
Run Code Online (Sandbox Code Playgroud)

以这样的方式“扁平化”这个导演结构的最佳方法是在 dir1 而不是 dir2 中获取 file1 和 file2。

directory cp recursive rename files

91
推荐指数
5
解决办法
6万
查看次数

Bash:如何从命令的输出中一次读取一行?

我正在尝试使用 .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)

shell bash pipe find

63
推荐指数
3
解决办法
15万
查看次数

GNU 为某些 shell 查找并屏蔽 {} - 哪个?

GNU find 的手册页指出:

-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. 
Run Code Online (Sandbox Code Playgroud)

那是从人到find(GNU findutils)4.4.2。

现在我用 bash 和 dash 测试了这个,两者都不需要{}被屏蔽。这是一个简单的测试:

find /etc -name "hosts" -exec …
Run Code Online (Sandbox Code Playgroud)

shell find xargs quoting

37
推荐指数
3
解决办法
3846
查看次数

bash - 我可以做:find .... -exec this &amp;&amp; that?

有没有办法在逻辑上组合使用find-exec调用的两个 shell 命令?

例如,打印出所有包含字符串 foo 及其出现的.csv文件,我想这样做:

find . -iname \*.csv -exec grep foo {} && echo {} \;
Run Code Online (Sandbox Code Playgroud)

但是bash抱怨“缺少'-exec'的参数”

bash find

28
推荐指数
2
解决办法
2万
查看次数

是否可以在另一个“find -exec”中嵌套“find -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 命令中完成,使用它的许多参数的混合?

find text-processing filenames

17
推荐指数
1
解决办法
7750
查看次数

标签 统计

find ×4

bash ×2

shell ×2

cp ×1

directory ×1

filenames ×1

files ×1

pipe ×1

quoting ×1

recursive ×1

rename ×1

text-processing ×1

xargs ×1