簡體   English   中英

查找文件並執行命令

[英]Find files and execute command

我有很多rar檔案在單個文件夾中構建,並希望腳本解壓縮它們。

我無法弄清楚它應該如何完成並需要一些幫助。

#!/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

我不熟悉bash腳本,我認為代碼錯誤不止一次。 但我想這應該是基本結構

您可以使用find命令:

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

find提供了exec選項,它將在找到的每個文件上執行該命令。

您不需要腳本。

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

此外,您可以將find的結果傳遞給unrar命令。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM