#!/bin/bash
 #sh j

find . -name "*first*" && echo "file found" || echo "file not found"

read -p "Run command $foo? [yn]" answer

case "$answer" in
y*) find . -type f -exec rename 's/(.*)\/(.*)first(.*)/$1\/beginning_$2changed$3/' {} + ;;

n*) echo "not renamed" ;;
esac
fi

我希望脚本循环遍历文件夹和子文件夹,找到包含特定字符串的文件,然后有一个选项来重命名该文件,或者在选择后让它成为(即y/n选项)脚本应该继续查找。
还有一个问题是“语法错误意外标记'fi'”

最佳答案

试试这个:

   #bin/bash

   handle_file(){

            local file=$0
            local pattern=some_pattern
            if [[ $(grep -c ${pattern} ${file}) -gt 0 ]];
            then
                    ......................................
                    do anything you want with your ${file}
                    ......................................
            fi
    }
    export -f handle_file

    find . -type f -exec bash -c 'handle_file "$@"' {} \;

handle_file是一个将被调用为handle_function <filename>的函数,因此<filename>在函数内部可用作$0

关于linux - 遍历文件夹和子文件夹中的文件名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33987454/

10-11 23:20
查看更多