bash 查找文件并执行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26911213/
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 11:46:55  来源:igfitidea点击:

Find files and execute command

linuxbash

提问by Linuxxon

I have a lot of rar archives structured in individual folders and would like to script unpacking them all.

我有很多 rar 档案结构在单独的文件夹中,并且想要脚本解压它们。

I'm having trouble figuring out how it should be done and need some help.

我无法弄清楚应该如何完成并且需要一些帮助。

#!/bin/bash
## For all inodes
for i in pwd; do
    ## If it's a directory
    if [ -d "$i" ] then
        cd $i

        ## Find ".rar" file
        for [f in *.rar]; do
            ./bin/unrar x "$f" # Run unrar command on filename
            cd ..
        done
    done
done

I am not familiar with bash scripting and I assume the code is wrong more than once. But I guess this should be the basic structure

我不熟悉 bash 脚本,我认为代码不止一次出错。但我想这应该是基本结构

回答by hek2mgl

You can use the findcommand:

您可以使用以下find命令:

find -name '*.rar' -exec unrar x {} \;

findoffers the option execwhich will execute that command on every file that was found.

find提供exec将在找到的每个文件上执行该命令的选项。

回答by jherran

You don't need a script.

你不需要脚本。

find . -name "*.rar" -exec unrar x {} \;

Additionally, you could pass the results of find to unrarcommand.

此外,您可以将 find 的结果传递给unrar命令。

find . -name "*.rar" | xargs unrar x