循环遍历文件夹和子文件夹中的文件名

时间:2015-11-29 20:07:19

标签: linux bash shell

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

1 个答案:

答案 0 :(得分:1)

试试这个:

   #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

相关问题